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,

Recommended Answers

All 11 Replies

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, 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,

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.

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?

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.

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

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.

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,

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

commented: Start a new thread. +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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.