954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

using multiple classes in a program

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.

public class Payrollprogram

{
   // main method begin program execution
    public static void main( String args[] )
      {
        // scanner to obtain user input 
        Scanner input = new Scanner( System.in );
      }

    private String employeeName; //name of employee
   
    //constructor to initialize employeeName
    public Payrollprogram( String name )

      {
        employeeName = name; //initializes employeeName
      } //end constructor
   
   // method to set employee name
   public void setEmployeeName( String name )
     {
       employeeName = name;
     }// end method setEmployeeName
   
   // method to retrieve employee name
   public String getEmployeeName()
     {
       return employeeName; 
     } // end method getEmployeeName

   //display welcome message for user
   public void displayMessage()
}


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,

redflame777
Newbie Poster
19 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Easter Bunny
Junior Poster in Training
57 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

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:

<private String employeeName;>


That belongs in the Payroll class, correct?

Sincerely,

redflame777
Newbie Poster
19 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Ok, so only this goes in payroll class:

<// main method begin program execution
    public static void main( String args[] )
      {
        // scanner to obtain user input 
        Scanner input = new Scanner( System.in );
      }


And this goes in the employee class:

private String employeeName; //name of employee
   
    //constructor to initialize employeeName
    public Payrollprogram( String name )

      {
        employeeName = name; //initializes employeeName
      } //end constructor
   
   // method to set employee name
   public void setEmployeeName( String name )
     {
       employeeName = name;
     }// end method setEmployeeName
   
   // method to retrieve employee name
   public String getEmployeeName()
     {
       return employeeName; 
     } // end method getEmployeeName

   //display welcome message for user
   public void displayMessage()
     {
       System.out.printf( "Welcome to payroll program for\n%s!\n\n",
        getEmployeeName() );
     } // end displayMessage method

   {
   System.out.println( "Enter employee name: " );
   }

     {
       employeeName = input.nextLine();
       while (employee.equals(""))
       {
         System.out.print( "Enter proper employee name: " );
         employee = Input.readString();
         // continue
       }

       if(employee==stop)
          {
            System.exit();
          }
       }


Is this right?

redflame777
Newbie Poster
19 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

What about when dealing with more than one employee? Does each employee need its own class?

redflame777
Newbie Poster
19 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Perhaps you should look through this for some clarification: http://java.sun.com/docs/books/tutorial/java/concepts/index.html

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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,

redflame777
Newbie Poster
19 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

anyone please tell me how to find Krishnamurti number in java??

rohan_s
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 
anyone please tell me how to find Krishnamurti number in java??

Start a new thread and what is a Krishnamurti number? Do you know and you just want the java code? If not try searching the net for the definition of Krishnamurti number

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You