Results 1 to 20 of 33
Thread: Looking for assistance
- 10-21-2009, 07:10 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 49
- Rep Power
- 0
Looking for assistance
Design a class that models an employee. The employee is defined by the employee name and the pay. For hourly employees, the pay will be the hourly rate. For salaried employees, the pay is the weekly salary paid. You should have a boolean field that indicates whether an employee is paid by the hour or is paid salary. There should be a method called calculateWeeklyGrossPay which takes in the number of hours worked and returns the gross pay. This method should check to see if the employee is hourly or salary and return the calculated pay with or without overtime for hourly employees or the weekly salary for salaried employees.
- 10-21-2009, 07:13 PM #2
Member
- Join Date
- Oct 2009
- Posts
- 25
- Rep Power
- 0
This looks like a homework assignment. What is your question?
- 10-21-2009, 07:39 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 49
- Rep Power
- 0
Java Code://the program is designed to model a employee //Branden Sullivan import java.util.Scanner; import java.text.DecimalFormat; public class Employee { public static void main(String [] args) { //variables are declared here int Workers, totalWorkers; //Accumulator //number of workers String Employee; //employee's name double hours; //hours worked double hourlyRate; //dollars per hour double salary; //salary double calculateWeeklyGrossPay; //calulated the gross pay for that week DecimalFormat dollar = new DecimalFormat("#,##0.00"); Scanner keyboard = new Scanner (System.in); //Get the number of workers from the user System.out.print("How many employee's pay do you want to calculate? "); Workers = keyboard.nextInt(); totalWorkers = 0; for (int count = 1; count <= Workers; count++){ System.out.print("What is the employee's name? "); Employee = keyboard.nextString(); } } }
- 10-21-2009, 07:45 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
We still aren't sure what your question is.
- 10-21-2009, 07:47 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
No those fields need to go into the employee class outside any method (not in the main method).
- 10-21-2009, 07:48 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 49
- Rep Power
- 0
how would i get the program to ask the user for how many workers and then ask them what the name of each worker is? how would i end the loop? how would i start it??
- 10-21-2009, 07:50 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
Start by getting the Employee class right. Read my response above to see where you went wrong.
You get user input from the console using the Scanner class.
- 10-21-2009, 07:54 PM #8
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
//Get the number of workers from the user
System.out.print("How many employee's pay do you want to calculate? ");
Workers = keyboard.nextInt();
for (int count = 1; count <= Workers; count++){
System.out.print("What is the employee's name? ");
Employee = keyboard.nextString();
}
If a user enters 2 employees, its going to go to your for loop and ask the user for a name and then set the variable employee to that name. Then your loop is going to ask what the next user's name is and then it's going to set the variable Employee to that person's name--over writing the first name.
- 10-22-2009, 03:19 AM #9
Member
- Join Date
- Oct 2009
- Posts
- 49
- Rep Power
- 0
Java Code://the program is designed to model a employee import java.util.Scanner; import java.text.DecimalFormat; public class Employee { public static void main(String [] args) { //variables are declared here int Workers, totalWorkers; //number of workers String Employee; //employee's name double hours; //hours worked double hourlyRate; //dollars per hour double salary; //salary double calculateWeeklyGrossPay; //calulated the gross pay for that week DecimalFormat dollar = new DecimalFormat("#,##0.00"); Scanner keyboard = new Scanner (System.in); //Get the number of workers from the user System.out.print("How many employee's pay do you want to calculate? "); Workers = keyboard.nextInt(); totalWorkers = 0; for (int count = 1; count <= Workers; count++){ } System.out.print("What is the employee's name? "); Employee = keyboard.nextString(); } }
Java Code:Employee.java:33: cannot find symbol symbol : method nextString() location: class java.util.Scanner Employee = keyboard.nextString(); ^ 1 error
-
It's telling you exactly what's wrong: the Scanner class doesn't have a method "nextString()". You'd best look up the Scanner API to see what methods are available.
Also, you still haven't created a true Employee class. I have a feeling that your teacher looking for a class that doesn't have a main method, or any static methods or variables. Then you'll likely have your main method in another class, say EmployeeTest that uses objects of the Employee class.
- 10-22-2009, 04:37 AM #11
Member
- Join Date
- Oct 2009
- Posts
- 49
- Rep Power
- 0
ok i got that done. no i need help on the demo class. how would i start???
- 10-22-2009, 06:57 AM #12
Member
- Join Date
- Oct 2009
- Posts
- 49
- Rep Power
- 0
Java Code:import java.util.Scanner; import java.text.DecimalFormat; public class EmployeeDemo { public static void main(String [] args) { DecimalFormat dollar = new DecimalFormat("#,##0.00"); Scanner keyboard = new Scanner (System.in); //variables are declared here int newWorkers, totalWorkers; String newEmployee; double hours; double hourlyRate; double salary; //Get the number of workers from the user System.out.print("How many employee's pay do you want to calculate? "); newWorkers = keyboard.nextInt(); totalWorkers = 0; for (int count = 1; count <= newWorkers; count++){ System.out.print("What is the employee's name? "); newEmployee = keyboard.next(); } } }
- 10-22-2009, 09:16 AM #13
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
Your first post says nothing about a test class for your Employee class. Concentrate on getting the Employee class first. You haven't posted what you have for that and it's the most important part.
-
Agrees with r-etc. You need to get perspective on this assignment. I recommend that you talk to your teacher without delay.
- 10-22-2009, 01:23 PM #15
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
r-etc? The SPNEX will hear about this. I hope they issue you with a head first defen.
r035198x (<---oppressed
- 10-23-2009, 06:54 PM #16
Member
- Join Date
- Oct 2009
- Posts
- 49
- Rep Power
- 0
Java Code://the program is designed to model a employee import java.util.Scanner; import java.text.DecimalFormat; public class Employee { //variables are declared here int Workers, totalWorkers; //number of workers String Employee; //employee's name double hours; //hours worked double hourlyRate; //dollars per hour double salary; //salary double calculateWeeklyGrossPay; //calulated the gross pay for that week public Employee(){ } public void setEmployee(String newEmployee){ Employee = newEmployee; } public void setWorkers(int newWorkers){ Workers = newWorkers; } public void sethours(double newhours){ hours = newhours; } public void sethourlyRate(double newhourlyRate){ hourlyRate = newhourlyRate; } public void setsalary(double newsalary){ salary = newsalary; } //calculates the gross pay for the week public double calculateWeeklyGrossPay(){ calculateWeeklyGrossPay = hours * hourlyRate; return calculateWeeklyGrossPay; } public String getEmployee(){ return Employee; } public int getWorkers(){ return Workers; } public double gethours(){ return hours; } public double hourlyRate(){ return hourlyRate; } public double getsalary(){ return salary; } }
Java Code:import java.util.Scanner; import java.text.DecimalFormat; public class EmployeeDemo { public static void main(String [] args) { DecimalFormat dollar = new DecimalFormat("#,##0.00"); Scanner keyboard = new Scanner (System.in); //variables are declared here int newWorkers, totalWorkers; String newEmployee; double hours; double hourlyRate; double salary; //Get the number of workers from the user System.out.print("How many employee's pay do you want to calculate? "); newWorkers = keyboard.nextInt(); totalWorkers = 0; for (int count = 1; count <= newWorkers; count++){ System.out.print("What is the employee's name? "); newEmployee = keyboard.next(); } System.out.print("Is the employee payed on a hourly rate or salary? "); } }
- 10-23-2009, 06:58 PM #17
Member
- Join Date
- Oct 2009
- Posts
- 49
- Rep Power
- 0
i would like to have the program ask for how many employees there are?? and then ask the first employee's name and store that. then ask whether they are payed on an hourly rate or salary, then ask how many hours they worked and calculate the pay with or without overtime. then i would like that whole process to repeat. is there a way to do that???
- 10-23-2009, 07:19 PM #18
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
Your Employee class cannot possibly compile like that. It is the important part of this assignment. You can't try to use it in the other class before you get it right.
You need to get rid of that setEmployee method and instead include a constructor that takes the values required to create an Employee.
- 10-23-2009, 07:24 PM #19
Member
- Join Date
- Oct 2009
- Posts
- 49
- Rep Power
- 0
the employee class does compile. i just need help on the demo class. i almost have it done.
Java Code:import java.util.Scanner; import java.text.DecimalFormat; public class EmployeeDemo { public static void main(String [] args) { DecimalFormat dollar = new DecimalFormat("#,##0.00"); Scanner keyboard = new Scanner (System.in); //variables are declared here int newWorkers, totalWorkers; String newEmployee; double hours; double hourlyRate; double salary; String choice; //Get the number of workers from the user System.out.print("How many employee's pay do you want to calculate? "); newWorkers = keyboard.nextInt(); totalWorkers = 0; for (int count = 1; count <= newWorkers; count++){ System.out.print("What is the employee's name? "); newEmployee = keyboard.next(); System.out.print("Is the employee payed on a hourly rate or salary? "); choice = keyboard.next(); if(choice == "hourly rate" || choice == "Hourly Rate"){ System.out.print("How many hours did the employee work? "); hours = keyboard.nextDouble(); } else{ System.out.print("How much is this persons salary? "); salary = keyboard.nextDouble(); } } } }
Java Code:How many employee's pay do you want to calculate? 4 What is the employee's name? Greg Is the employee payed on a hourly rate or salary? hourly rate How much is this persons salary? Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextDouble(Scanner.java:2387) at EmployeeDemo.main(EmployeeDemo.java:38) ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
- 10-23-2009, 07:33 PM #20
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
Ok it does compile because your variable called Employee is a String which is meaningless.
You need that constructor that I talked about above for creating the employees. An employee is not a String. It is an object of type Employee created using something like
Employee employee = new Employee("The name of the employee", 4000, ....
Read your assignment requirements again and stop acting Mr know all.
Similar Threads
-
In need of some assistance
By Boer84 in forum New To JavaReplies: 2Last Post: 07-08-2008, 05:14 PM -
X-Tremely new to this...Need assistance...
By Johnny562 in forum New To JavaReplies: 5Last Post: 07-01-2008, 10:17 PM -
[SOLVED] Your assistance required please!
By crazydeo in forum New To JavaReplies: 1Last Post: 05-21-2008, 12:58 PM -
i need assistance with a string triggered loop please!
By Phobos0001 in forum New To JavaReplies: 9Last Post: 11-14-2007, 03:44 PM
Bookmarks