Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Question 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.

  2. #2
    literallyjer is offline Member
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default

    This looks like a homework assignment. What is your question?

  3. #3
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    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();
    	}	
    	
     	}
     }
    this is what i have so far

  4. #4
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    We still aren't sure what your question is.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    No those fields need to go into the employee class outside any method (not in the main method).

  6. #6
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

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

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  8. #8
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    //Get the number of workers from the user
    System.out.print("How many employee's pay do you want to calculate? ");
    Workers = keyboard.nextInt();
    Well your asking the user for the number of employees, and then your setting the variable Workers to the number of employees.

    for (int count = 1; count <= Workers; count++){
    System.out.print("What is the employee's name? ");
    Employee = keyboard.nextString();
    }
    You have your loop right, it will loop through the correct number of users, the ony thing is your loop is only going to know the last person's name because you keep over writing the variable employee.

    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.

  9. #9
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    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();
    
    
     	}
     }
    this is what i have and this error comes up

    Java Code:
    Employee.java:33: cannot find symbol
    symbol  : method nextString()
    location: class java.util.Scanner
    	Employee = keyboard.nextString();
    	                   ^
    1 error

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  11. #11
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    ok i got that done. no i need help on the demo class. how would i start???

  12. #12
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    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();
    	}
    	
     	}
     
     }
    this is what i have for the demonstration class. i dont know how i would get the names to not overwrite the other ones

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Agrees with r-etc. You need to get perspective on this assignment. I recommend that you talk to your teacher without delay.

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    r-etc? The SPNEX will hear about this. I hope they issue you with a head first defen.

    r035198x (<---oppressed

  16. #16
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    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;
    	}
     	
     }
    this is what i have for the employee class
    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?  ");
    
    	
     	} 
     }
    and this is what i have for the demo class so far. i need help on the loop. is there a way not only to loop just the employee's names but other things at the same time???

  17. #17
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

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

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  19. #19
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    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();
    		}
    		
    	}
    		
     	} 
     }
    this is what i have so far and it seems to work well. i just get one error message.
    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.
    this is what i get when i type in hourly rate

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. In need of some assistance
    By Boer84 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 05:14 PM
  2. X-Tremely new to this...Need assistance...
    By Johnny562 in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 10:17 PM
  3. [SOLVED] Your assistance required please!
    By crazydeo in forum New To Java
    Replies: 1
    Last Post: 05-21-2008, 12:58 PM
  4. i need assistance with a string triggered loop please!
    By Phobos0001 in forum New To Java
    Replies: 9
    Last Post: 11-14-2007, 03:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •