| | |
using multiple classes in a program
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 17
Reputation:
Solved Threads: 0
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.
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,
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.
Java Syntax (Toggle Plain Text)
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,
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.
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.
•
•
Join Date: Sep 2008
Posts: 17
Reputation:
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.
Ok. Thank you. So how do I do that? As you can see, I have:
Java Syntax (Toggle Plain Text)
<private String employeeName;>
That belongs in the Payroll class, correct?
Sincerely,
•
•
Join Date: Sep 2008
Posts: 17
Reputation:
Solved Threads: 0
Ok, so only this goes in payroll class:
And this goes in the employee class:
Is this right?
Java Syntax (Toggle Plain Text)
<// 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:
Java Syntax (Toggle Plain Text)
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?
Last edited by redflame777; Sep 12th, 2008 at 3:48 pm.
Perhaps you should look through this for some clarification: http://java.sun.com/docs/books/tutor...pts/index.html
![]() |
Similar Threads
- Exception in thread "main" java.lang.NoClassDefFoundError: Invaders Error (Java)
- Linked Lists in multiple classes (C++)
- Interface File and Multiple Classes (C++)
- Error when tring to run program PLEasE Help (C++)
- how will i repeat my whole game program? (Java)
- High-Level Languages (Computer Science)
- Classes (C++)
Other Threads in the Java Forum
- Previous Thread: java.io.FileNotFoundException
- Next Thread: java and windows
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






