Results 1 to 20 of 21
Thread: Trying to understand
- 10-09-2010, 12:23 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
Trying to understand
Hello,
I am new to the forums and pretty new to Java. I am currently taking a course in Java programming, and I am working on an assignment.
My problem is this: I can't seem to wrap my head around the specifics. I am making a basic Payroll Program, and I have done all right so far. Now, the assignment is to modify my program so that it uses a class to store and retrieve data. I can't find it anywhere in my reading how to get the class to store and retrieve data.
Can anyone explain this to me? I don't want anyone to write a solution; I would just like someone to help me understand this concept.
- 10-09-2010, 12:33 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
- 10-09-2010, 01:17 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Now, the assignment is to modify my program so that it uses a class to store and retrieve data.
I'm not too clear what this means.
What does your payroll program do at the moment? Does it store and retrieve data? Perhaps you could post the exact assignment so that someone can give you a nudge in the right direction or suggest some tutorial type documentation.
- 10-09-2010, 08:17 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
I did this.Java Code:Create a non-GUI based Java application that calculates weekly pay for an employee. The application should display text that requests the user input the name of the employee, the hourly rate, and the number of hours worked for that week. The application should then print out the name of the employee and the weekly pay amount. In the printout, display the dollar symbol ($) to the left of the weekly pay amount and format the weekly pay amount to display currency.
I did this.Java Code:Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.
This is the one with which I am having trouble.Java Code:Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.
- 10-09-2010, 08:28 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Stupid forum software ...
kind regards,
Jos
- 10-09-2010, 08:59 AM #6
what kind of troubles? if you are using the System.console look here for more detailed informations.
- 10-09-2010, 09:03 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Just for the record ;)
Create a non-GUI based Java application that calculates weekly pay for an employee. The application should display text that requests the user input the name of the employee, the hourly rate, and the number of hours worked for that week. The application should then print out the name of the employee and the weekly pay amount. In the printout, display the dollar symbol ($) to the left of the weekly pay amount and format the weekly pay amount to display currency.
Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.
Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.
- 10-09-2010, 09:05 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Umm...
I think you posted the second one twice and left out the one that is giving the problem.
- 10-09-2010, 12:33 PM #9
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
I'm guessing what is required is an Employee object.
ladykrimson,
What you need is a class called Employee with variables to hold details such as name, wage, etc. You could then use a series of get and set methods to access the required data.
The employee object is then created using the new keyword and creates a unique instance of that class. Each instance can be set to contain different values.
Regards.
- 10-12-2010, 06:11 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
Thank you so much. That was exactly what was needed. Now, I am having a different issue. Now, I have to call the methods which I have set up. I am having trouble creating a statement to do this. Here is what I have so far (keep in mind, this is incomplete).
I use Eclipse to do my homework. I bolded two lines in the program, because Eclipse suggested that I make the method static. Did I use the wrong statement to call the method?Java Code:package payroll.program; // import predefined Scanner from Java Library import java.util.Scanner; public class PayrollProgram { public static void main(String[] args) { // create Scanner to obtain input from command window Scanner input = new Scanner(System.in); int rate = 0; int hours; int pay; //Welcome message System.out.println("Welcome to the Payroll Program."); System.out.println("Enter the first and last name of the employee or enter - stop - to end the program."); String employeeName = input.nextLine(); // read a line of text [B]Employee.setEmployeeName(employeeName);[/B] while (!employeeName.equalsIgnoreCase("stop")) { System.out.printf("Enter the hourly rate for %s/n:", employeeName); // prompt rate = input.nextInt(); //read input from the user System.out.println(); // prints a blank line Employee.setRate(rate); } while (rate < 0) { System.out.println("Please enter a positive number."); //prompt rate = input.nextInt(); //read input from the user System.out.println(); // prints a blank line } System.out.printf("Enter the number of hours worked for %s/n:", employeeName); // prompt hours = input.nextInt(); // read input from user System.out.println(); // prints a blank line while (hours < 0) { System.out.println("Please enter a positive number."); //prompt hours = input.nextInt(); // read input from user System.out.println(); // prints a blank line } pay = hours * rate; System.out.printf("The pay for employee, %s is $%d\n",employeeName, pay); } } class Employee { // employee name, hours, and rate. [B]public static String employeeName;[/B] public double hours; public double rate; public double pay; Employee name = new Employee(); // method to set employee name public static void setEmployeeName(String name) { employeeName = name; } // end method setEmployeeName // method to set hours public void setHours(double hours) { this.hours = hours; } // end method setHours // method to set rate public void setRate(double rate) { this.rate = rate; } // end method setRate // method to retrieve employee name public String getEmployeeName() { return employeeName; } // end method getEmployeeName // method to retrieve hours public double getHours() { return hours; } // end method getHours // method to retrieve rate public double getRate() { return rate; } // end method getRate // method to compute pay public double getPay() { pay = rate * hours; return pay; } // end method getPay }
-
No -- Eclipse does not suggest this but warns you that you are trying to access a variable or method in a static way. The solution is not to make the variable or method static, it's to access it in a non-static way. In fact you should get rid of all statics from your Employee class and instead create an Employee object in your main method and call the objects methods.
- 10-12-2010, 06:31 PM #12
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
I can't do that. The assignment says:
Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay.
I think I got it. I had to add static to all the class methods and take out the "this." and rename the local variable....Whew...Last edited by ladykrimson; 10-12-2010 at 07:00 PM.
- 10-12-2010, 08:31 PM #13
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Actually, you can.
What you haven't done was to construct the Employee object before accessing the methods.
1) The first thing to add is an appropriate constructor within the Employee class.
2) All variables and methods within the Employee class should be returned to non-static.
3) The line "Employee name = new Employee();" should be removed from the Employee class and placed in the PayrollProgram before any of the methods are called. This code may need altering depending on the constructor called.
Regards.
- 10-12-2010, 10:00 PM #14
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
- 10-12-2010, 10:18 PM #15
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
A constructor defines how an object is to be initialized. This could be as simple as:
Java Code:public Employee(){}
or
Java Code:public Employee(String name) { [INDENT]employeeName = name;[/INDENT] }
Regards.
- 10-12-2010, 10:28 PM #16
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
OK, how does this look?
And thank you for all your help!Java Code:package payroll.program; // import predefined Scanner from Java Library import java.util.Scanner; public class PayrollProgram { public static void main(String[] args) { // create Scanner to obtain input from command window Scanner input = new Scanner(System.in); // create new object, Employee Employee name = new Employee(); int rate = 0; int hours; int pay; //Welcome message System.out.println("Welcome to the Payroll Program."); System.out.println("Enter the first and last name of the employee or enter - stop - to end the program."); String employeeName = input.nextLine(); // read a line of text Employee.setEmployeeName(employeeName); // calls the method, setEm, from the Employee class if (employeeName.equalsIgnoreCase("stop")) { System.exit(0); } else { System.out.printf("Enter the hourly rate for %s/n:", employeeName); // prompt rate = input.nextInt(); //read input from the user System.out.println(); // prints a blank line Employee.setRate(rate); // calls the method, setEmployeeName, from the Employee class while (rate < 0) { System.out.println("Please enter a positive number."); //prompt rate = input.nextInt(); //read input from the user System.out.println(); // prints a blank line } System.out.printf("Enter the number of hours worked for %s/n:", employeeName); // prompt hours = input.nextInt(); // read input from user System.out.println(); // prints a blank line Employee.setHours(hours); Employee.setRate(rate); // calls the method, setHours, from the Employee class while (hours < 0) { System.out.println("Please enter a positive number."); //prompt hours = input.nextInt(); // read input from user System.out.println(); // prints a blank line } // Display the name and pay for the employee System.out.printf("The pay for employee, %s is $%d\n",employeeName, getPay); } } } class Employee { // employee name, hours, and rate. public String employeeName; public double hours; public double rate; public double pay; // employee constructor public Employee(String name) { employeeName = name; } // method to set employee name public void setEmployeeName(String name) { employeeName = name; } // end method setEmployeeName // method to set hours public void setHours(double hours) { this.hours = hours; } // end method setHours // method to set rate public void setRate(double rate) { this.rate = rate; } // end method setRate // method to retrieve employee name public String getEmployeeName() { return employeeName; } // end method getEmployeeName // method to retrieve hours public double getHours() { return hours; } // end method getHours // method to retrieve rate public double getRate() { return rate; } // end method getRate // method to compute pay public double getPay() { pay = rate * hours; return pay; } // end method getPay }
- 10-12-2010, 10:36 PM #17
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
ask the compiler :)
- Employee name = new Employee(); : this constructor does not exist
- Employee.setEmployeeName(employeeName); and Employee.setHours(hours); these are not static methods, call the methods on your employee object !!!
- System.out.printf("The pay for employee, %s is $%d\n",employeeName, getPay); : :confused: du you mean getPay() / name.getPay() ? :)
- 10-12-2010, 10:44 PM #18
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Better, but not quite there yet.
Firstly, remove the codebecause it is not doing anything. I know this contradicts what I said earlier, but bear with me.Java Code:Employee name = new Employee();
Next, removeand instead place the codeJava Code:Employee.setEmployeeName(employeeName);
within the else statement right at the start. You are now calling the constructor for Employee you've just put in.Java Code:Employee employee = new Employee(employeeName);
Lastly, change all referes to "Employee." to "employee.". This allows you to access the properties of the employee object instead of the Employee class. Big difference!
This also eliminates the need for static methods and variables within the Employee class.
After this I think you're done... and eRaaaa beat me to it. Oh well!
eRaaaa,
Cheers for spotting the getPay, I completely missed it.
Regards.Last edited by Ronin; 10-12-2010 at 10:51 PM. Reason: eRaaaa beat me to it
- 10-12-2010, 10:59 PM #19
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
Like this?
Java Code:package payroll.program; // import predefined Scanner from Java Library import java.util.Scanner; public class PayrollProgram { public static void main(String[] args) { // create Scanner to obtain input from command window Scanner input = new Scanner(System.in); int rate = 0; int hours; int pay; //Welcome message System.out.println("Welcome to the Payroll Program."); System.out.println("Enter the first and last name of the employee or enter - stop - to end the program."); String employeeName = input.nextLine(); // read a line of text if (employeeName.equalsIgnoreCase("stop")) { System.exit(0); } else { employee employee = new employee(employeeName); System.out.printf("Enter the hourly rate for %s/n:", employeeName); // prompt rate = input.nextInt(); //read input from the user System.out.println(); // prints a blank line employee.setRate(rate); // calls the method, setEmployeeName, from the Employee class while (rate < 0) { System.out.println("Please enter a positive number."); //prompt rate = input.nextInt(); //read input from the user System.out.println(); // prints a blank line } System.out.printf("Enter the number of hours worked for %s/n:", employeeName); // prompt hours = input.nextInt(); // read input from user System.out.println(); // prints a blank line employee.setHours(hours); // calls the method, setHours, from the Employee class while (hours < 0) { System.out.println("Please enter a positive number."); //prompt hours = input.nextInt(); // read input from user System.out.println(); // prints a blank line } // Display the name and pay for the employee System.out.printf("The pay for employee, %s is $%d\n",employeeName, pay); } } } class Employee { // employee name, hours, and rate. public String employeeName; public double hours; public double rate; public double pay; // employee constructor public Employee(String name) { employeeName = name; } // method to set employee name public void setEmployeeName(String name) { employeeName = name; } // end method setEmployeeName // method to set hours public void setHours(double hours) { this.hours = hours; } // end method setHours // method to set rate public void setRate(double rate) { this.rate = rate; } // end method setRate // method to retrieve employee name public String getEmployeeName() { return employeeName; } // end method getEmployeeName // method to retrieve hours public double getHours() { return hours; } // end method getHours // method to retrieve rate public double getRate() { return rate; } // end method getRate // method to compute pay public double getPay() { pay = rate * hours; return pay; } // end method getPay }
- 10-12-2010, 11:03 PM #20
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Similar Threads
-
Trying to understand the whole 2D thing.
By xael in forum Java 2DReplies: 4Last Post: 10-06-2010, 01:41 AM -
I don't understand the error ... (help!)
By XmisterIS in forum New To JavaReplies: 3Last Post: 09-02-2010, 09:33 AM -
I don't understand the output,
By TIMBERings in forum New To JavaReplies: 1Last Post: 12-10-2009, 08:03 AM -
understand the code
By prof.deedee in forum New To JavaReplies: 8Last Post: 11-11-2009, 02:43 AM -
I don´t understand
By Manikyr in forum New To JavaReplies: 6Last Post: 02-22-2009, 11:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks