using multiple classes in a program

Thread Solved

Join Date: Sep 2008
Posts: 17
Reputation: redflame777 is an unknown quantity at this point 
Solved Threads: 0
redflame777 redflame777 is offline Offline
Newbie Poster

using multiple classes in a program

 
0
  #1
Sep 12th, 2008
Hi everyone,

I am new to Java. I am taking a Java programming course and I am having some difficulty. My professor said I should seperate my class that stores employee information and calculates pay from the class that contains my main method. How do I do that? I understand what the professor is saying and I understand how to create the classes but I am confused on where exactly how to do this. I am going to post some of my code, I know that the employee information class needs to be seperated somewhere in the section of code that I am posting but unsure as to where.

  1. public class Payrollprogram
  2.  
  3. {
  4. // main method begin program execution
  5. public static void main( String args[] )
  6. {
  7. // scanner to obtain user input
  8. Scanner input = new Scanner( System.in );
  9. }
  10.  
  11. private String employeeName; //name of employee
  12.  
  13. //constructor to initialize employeeName
  14. public Payrollprogram( String name )
  15.  
  16. {
  17. employeeName = name; //initializes employeeName
  18. } //end constructor
  19.  
  20. // method to set employee name
  21. public void setEmployeeName( String name )
  22. {
  23. employeeName = name;
  24. }// end method setEmployeeName
  25.  
  26. // method to retrieve employee name
  27. public String getEmployeeName()
  28. {
  29. return employeeName;
  30. } // end method getEmployeeName
  31.  
  32. //display welcome message for user
  33. public void displayMessage()
  34. }

This is not all of my code but I closed it so that everyone would know that was not the problem.
I have worked on it since my professor last saw it but I'm not sure I fixed the problem he said I was having. Any feed back would be greatly appreciated. I hope I posted the code correctly according to the rules. Thank you.

Sincerely,
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 57
Reputation: Easter Bunny is an unknown quantity at this point 
Solved Threads: 1
Easter Bunny's Avatar
Easter Bunny Easter Bunny is offline Offline
Junior Poster in Training

Re: using multiple classes in a program

 
0
  #2
Sep 12th, 2008
ok, what you need to do, is to split them up. keep the main method in Payrollprogram and then everything to do with the details of a client, you keep in a different class.

to then access the clients, you'll do exactly the same thing as you were doing. i'm assuming you havn't done packages yet, so don't worry about that.

you'll probably end up with a class that runs the program, a class that creates a client and then maybe a class that works out the client's pay and so on.

splitting them up makes it easier to find find the problem when something isn't working.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 17
Reputation: redflame777 is an unknown quantity at this point 
Solved Threads: 0
redflame777 redflame777 is offline Offline
Newbie Poster

Re: using multiple classes in a program

 
0
  #3
Sep 12th, 2008
Originally Posted by Easter Bunny View Post
ok, what you need to do, is to split them up. keep the main method in Payrollprogram and then everything to do with the details of a client, you keep in a different class.

to then access the clients, you'll do exactly the same thing as you were doing. i'm assuming you havn't done packages yet, so don't worry about that.

you'll probably end up with a class that runs the program, a class that creates a client and then maybe a class that works out the client's pay and so on.

splitting them up makes it easier to find find the problem when something isn't working.

Ok. Thank you. So how do I do that? As you can see, I have:

  1. <private String employeeName;>

That belongs in the Payroll class, correct?

Sincerely,
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,444
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: using multiple classes in a program

 
0
  #4
Sep 12th, 2008
No, it would go in an Employee class, which would contain all of the data pertaining to a particular employee.

The payroll program works with employees, but is not itself an employee. Class design is an exercise in separation of responsibilities.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 17
Reputation: redflame777 is an unknown quantity at this point 
Solved Threads: 0
redflame777 redflame777 is offline Offline
Newbie Poster

Re: using multiple classes in a program

 
0
  #5
Sep 12th, 2008
Ok, so only this goes in payroll class:
  1. <// main method begin program execution
  2. public static void main( String args[] )
  3. {
  4. // scanner to obtain user input
  5. Scanner input = new Scanner( System.in );
  6. }

And this goes in the employee class:
  1. private String employeeName; //name of employee
  2.  
  3. //constructor to initialize employeeName
  4. public Payrollprogram( String name )
  5.  
  6. {
  7. employeeName = name; //initializes employeeName
  8. } //end constructor
  9.  
  10. // method to set employee name
  11. public void setEmployeeName( String name )
  12. {
  13. employeeName = name;
  14. }// end method setEmployeeName
  15.  
  16. // method to retrieve employee name
  17. public String getEmployeeName()
  18. {
  19. return employeeName;
  20. } // end method getEmployeeName
  21.  
  22. //display welcome message for user
  23. public void displayMessage()
  24. {
  25. System.out.printf( "Welcome to payroll program for\n%s!\n\n",
  26. getEmployeeName() );
  27. } // end displayMessage method
  28.  
  29. {
  30. System.out.println( "Enter employee name: " );
  31. }
  32.  
  33. {
  34. employeeName = input.nextLine();
  35. while (employee.equals(""))
  36. {
  37. System.out.print( "Enter proper employee name: " );
  38. employee = Input.readString();
  39. // continue
  40. }
  41.  
  42. if(employee==stop)
  43. {
  44. System.exit();
  45. }
  46. }

Is this right?
Last edited by redflame777; Sep 12th, 2008 at 3:48 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,444
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: using multiple classes in a program

 
0
  #6
Sep 12th, 2008
Well, go through each of those methods and ask yourself, "Does this data or action apply to one and exactly one particular employee?". If it does, it goes there. If it doesn't then it goes in some other class that manages that particular functionality.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 17
Reputation: redflame777 is an unknown quantity at this point 
Solved Threads: 0
redflame777 redflame777 is offline Offline
Newbie Poster

Re: using multiple classes in a program

 
0
  #7
Sep 12th, 2008
What about when dealing with more than one employee? Does each employee need its own class?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,444
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: using multiple classes in a program

 
0
  #8
Sep 12th, 2008
No, each employee is it's own instance. The class is just a template for that instance. All Employee objects would have the same property fields, but the values of those properties are different for each employee.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,444
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: using multiple classes in a program

 
0
  #9
Sep 12th, 2008
Perhaps you should look through this for some clarification: http://java.sun.com/docs/books/tutor...pts/index.html
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 17
Reputation: redflame777 is an unknown quantity at this point 
Solved Threads: 0
redflame777 redflame777 is offline Offline
Newbie Poster

Re: using multiple classes in a program

 
0
  #10
Sep 13th, 2008
After reviewing the site that you suggested and rereading your posts it made more sense. Of all the times I looked over that site I somehow missed that part of it, I guess that's why I was so confused.Thank you Ezzaral. Have a good day.

Sincerely,
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC