lauren lauren - 5 months ago 8
Java Question

How to write an interactive program ( Parameters and objects)

In my CS class we are learning about parameters and object, which i'm not entirely getting. We were given the problem below that I am having some difficulties with.
Im a beginner at this so bare with me!

Write a Java program that includes a method called pay that accepts two parameters: a real number for an employee's hourly wage and an integer for the number of hours the employee worked that day. The method should return how much money to pay the employee for this day.

For example, the call

pay(5.50, 6)
should return
33.0
.
The employee should receive "overtime" pay of
1.5
times the normal hourly wage for any hours above
8
. For example, the call
pay(4.00, 11)
should return
50.0
(calculated as
(4.00 * 8) + (6.00 *3)
).

Include in the
main
method of this program four calls to the pay method. Two calls, each one using the example data given above and two calls, each one using data that you determine.

So far this is all I got which I feel is nothing:

import java.util.*;

public class Payjava {

//obtain values
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println(" Wage : ");
double wage = console.nextDouble();
System.out.print(" Hours :");
int hours = console.nextInt();

// compute results
double overtime = 1.5;
double overtime = hours > 8 * (wage * 1.5);
wage = hours * wage;
}
}

Answer

I would suggest starting by breaking down the question into smaller pieces. The question asks for a method to calculate an employees pay, so that seems like a reasonable place to start. The question gives you the name of the method and what parameters it should take:

...a method called pay that accepts two parameters: a real number for an employee's hourly wage and an integer for the number of hours the employee worked that day. The method should return how much money to pay the employee for this day.

So your method would look like this:

public double pay(double hourlyWage, int numberOfHours) {
//TODO: complete
}

Then start adding the logic into the method - start with the simple case of no overtime, then go back and add the overtime calculation.

Finally, note that the question says to include four calls to the pay method in the main method - so you dont actually need to prompt for input.