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.

2. Member
Join Date
Oct 2009
Posts
25
Rep Power
0
This looks like a homework assignment. What is your question?

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();
}

}
}```
this is what i have so far

4. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
We still aren't sure what your question is.

5. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
No those fields need to go into the employee class outside any method (not in the main method).

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

7. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
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. 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();
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. 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();

}
}```
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. 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. 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???

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();
}

}

}```
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. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
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. Agrees with r-etc. You need to get perspective on this assignment. I recommend that you talk to your teacher without delay.

15. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10

r035198x (<---oppressed

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;
}

}```
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. 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???

18. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
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. 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();
}

}

}
}```
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. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
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, ....

Page 1 of 2 12 Last

#### Posting Permissions

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