# Need help with java chp 4 Gross Pay

• 10-05-2010, 05:28 AM
Liquify
Need help with java chp 4 Gross Pay
Develop a Java application that will determine the gross pay for each of three employees. The company pays "straight-time" for the first 40 hours worked by each employee and pays "time-and-a- half" for all hours worked in excess of 40 hours. You are given a list of the employees of the company, the number of hours each employee worked last week and the hourly rate of each employee. Your program should input this information for each employee, and should determine and display the employee's gross pay. Use input dialogs to input the data.
this is what i have so far...

Employee.java
public class Employee
{

private int hours;
private double rate, pay;
private double grossPay;

public double calculatePay( double hours, double rate )
{
if( hours <= 40 )
{
grossPay = hours * rate;
System.out.printf(" Gross Pay: %s", grossPay);
}

else
{

double grossPay = 40 * rate + ( hours -40 ) * rate * 1.5;
System.out.printf(" Gross Pay: %s\n", grossPay);

}

return grossPay;

}
}

EmployeeTest.java

import java.util.Scanner;

public class EmployeeTest
{
public static void main(String[] args)
{

Scanner input = new Scanner(System.in);

Employee employee = new Employee();
int x = 0;

do
{

System.out.printf ("Employee: ");
input = new Scanner(System.in);
String name = input.nextLine();

System.out.printf( " Hours Worked: ");
double hours = input.nextInt();

System.out.printf( " Hourly rate: ");
double rate = input.nextDouble();

employee.calculatePay( hours, rate );
x++;
} while (x < 3);

}
}

The RESULT NEEDS TO LOOK LIKE THIS: sample run
Employee: Smith Hours worked: 50.00 Hourly rate: 24.00 Gross pay: 1320.00
Employee: Johnson Hours worked: 20.00 Hourly rate: 26.00 Gross pay: 520.00
Employee: Watkins Hours worked: 40.00 Hourly rate: 20.00 Gross pay: 800.00

when i run my javascript i can't get employee, hours worked, hourly rate, and gross pay on one line. any tips to solving this problem? i would appreciate it.
• 10-05-2010, 10:21 AM
Tolls
I have a feeling ( could be wrong, don't use command line input much) that it requires a carriage return to identify the "next" part.

What output are you getting?

Reading the problem, I suggest you read in the data for each employee, then display it in the format expected.

So:
Read employee, saving the data in the employee attributes (which you're not using at all at the moment).
Save that employee in an array.
Keep reading employees until some input indicates that's all the employees to be entered.
Display the employee data.
• 10-05-2010, 10:53 AM
m00nchile
Its also good practice to define a toString() method for every class you make that achieves the output you want, since it simplifies the printout of data:
Code:

```public class ToStringTest {   private int x,y;   public ToStringTest(int x, int y) {     this.x = x; this.y = y;   }   public String toString()  {     return "Value x: "+x+", value y: "+y+".";   }   public static void main(String[] args) {     ToStringTest[] arr = new ToStringTest[10];     for(int i = 0; i < arr.length; i++) {       int x = (int)(Math.random()*9)+1, y = (int)(Math.random()*9)+1;       arr[i] = new ToStringTest(x,y);     }     for(int i = 0; i < arr.length; i++) System.out.println(arr[i]);   } }```
• 10-05-2010, 10:59 AM
JosAH
Quote:

Originally Posted by Assignment text; last sentence
Use input dialogs to input the data.