The Artima Developer Community
Sponsored Link

Java Answers Forum
need help with concept of writing A Class

6 replies on 1 page. Most recent reply: Oct 20, 2005 4:58 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Trevor Hatcher

Posts: 10
Nickname: newbie05
Registered: Oct, 2005

need help with concept of writing A Class Posted: Oct 19, 2005 12:27 PM
Reply to this message Reply
Advertisement
given the superClass I can write driver programs to run correctly. I can even employ some intermediate techniques, given the time I have been programming.My problem occurred when I was asked to write a class for someone else's driver.
here is the Driver I was asked to write a class for.
public class TestSimpleEmployee
{
  public static void main( String[] args )
  {
    SimpleEmployee empOne = new SimpleEmployee( "Jack", 52000.00 );
    SimpleEmployee empTwo = new SimpleEmployee( "Jill", 53000.00 );
 
    System.out.println( empTwo.getName( ) + " annual " + empTwo.getAnnualSalary( ) );
 
    System.out.println( empTwo.getName( ) + " monthly " + empTwo.getMonthlySalary( ) );
 
    System.out.println( empOne.getName( ) + " annual " + empOne.getAnnualSalary( ) );
    System.out.println( empOne.getName( ) + " monthly " + empOne.getMonthlySalary( ) );
 
    empTwo.giveRaise( 0.05 );
    empOne.giveRaise( 0.10 );
    System.out.println( empTwo.toString( ) );
    System.out.println( empOne.toString( ) );
  }
}

here is my attempt to write the class for this driver.
public class SimpleEmployee
{
	//information to get
	private String Name;
	private double AnnualSalary;
	private double Monthly;
	//information to input
	private double raise;
 
public String EmployeeEntry(String getName, double AnnualSalary)
{ 	Name=getName;
	AnnualSalary= Salary;
}
 
public String getName( )
  {
    return Name;
  }
  public double getAnnualSalary( )
    {
      return Salary;
  	}
 
public double EmployeeRaise(double raise)
	{
raise = raise * Salary;
	return raise;
	}
public double MonthlySalary( double Monthly)
  	{
    Monthly = Salary/12;
    return Monthly;
	}
 
 public String toString( )
  {
 
    return Name +" " + Salary + raise;
  }
}

If anyone can show me a method/process/procedure for writing a class I would really appreciate it.

P.S. for those wondering I don't need the code of the class for this driver I need to know how to write my own class code.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: need help with concept of writing A Class Posted: Oct 19, 2005 10:48 PM
Reply to this message Reply
I really don't understand the question.

You don't know the structure of a class?
This would actually be a valid class, extending the class Object.
public class NewClass() {
}


You know, there are lots and lots of Java turorials around the net.

Start here:
http://java.sun.com/learning/tutorial/index.html
http://java.sun.com/docs/index.html

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: need help with concept of writing A Class Posted: Oct 20, 2005 1:31 AM
Reply to this message Reply
You should also familiarize yourself with static methods.
They come in handy when you need to incorporate Singleton
classes in your software architecture -- connection
pooling etc.

Google static methods Java Tutorial to add to it.
public class ClassA{
   ClassX var;
   public void method1(ClassX var){
      this.var = var;
      //do some operation on var
   }
   
   public ClassX method2(){
      return new ClassX();
   }
   
   public static void method3(ClassX var){
      //  perform some operation
   }
}
 
//  when using ClassA
ClassA varA = new ClassA();
ClassX varX = new ClassX();
varX.performSomeOperationToSetPropertiesInX();
 
varA.method1(varX);
 
// You can do this in your main class
// To perform method3 so as to alter another property in varX you
// can do:
 
ClassA.method3(varX);

The object varX now has undergone some property change.
I don't know if this actually makes sense, but that's my lame
attempt at a try.

Trevor Hatcher

Posts: 10
Nickname: newbie05
Registered: Oct, 2005

Re: need help with concept of writing A Class Posted: Oct 20, 2005 3:49 AM
Reply to this message Reply
Thanks for all of your help. I understand your confusion I didn't know exactly how to ask the question until someone responded. Now I can Say That my College prof wants us to write the main class to run a pre-written driver program from the book. sort of like the String Class has CharAt(),indexOf()etc... mine will need to have a getName() getAnnualSalary() etc...included. Hope this helps explain my dilema.

P.S. I Think my prof needed to teach this first then the other would have been a piece of cake.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: need help with concept of writing A Class Posted: Oct 20, 2005 4:02 AM
Reply to this message Reply
public class Main {
    public Main() {
        //do something important
    public Main(...some prameters...) {
        //do something important
    }
    }
    public String getName() {
        //do something important
    }
    public double getAnnualSalary() {
        //do something important
    }
 
    //if should be executable
    public static void main (String[]args) {
        new Main();
        //do something important
    }
    private double a_variable;
    private String another variable;
}



As I previously said: Try reading some tutorials

Trevor Hatcher

Posts: 10
Nickname: newbie05
Registered: Oct, 2005

Re: need help with concept of writing A Class Posted: Oct 20, 2005 4:51 AM
Reply to this message Reply
neumi
Thanks for your help. Just to let you know I did research this topic and Javaranch.com, Sun, and others seem to think if you have gotten far enough to be looking for answers to this question you should be able to figure it out on your own. I couldn't find anything relating to classes that would help me with this problem. that is why I came here where I could ask a question and get multiple responses to relate to.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: need help with concept of writing A Class Posted: Oct 20, 2005 4:58 AM
Reply to this message Reply
A class simply represents an Object. Objects have attributes and methods.

You should consider reading something about object oriented programming. The rest is just syntax.

If you want to do it right, close down Netbeans right now and try to define your class/object on a piece of paper. Write down all it's attributes and public methods with a definition of input and output.

Flat View: This topic has 6 replies on 1 page
Topic: Is JAVA a Language ?? "There are no dumb questions" Previous Topic   Next Topic Topic: Help me about String

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use