Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Computational issue in Java for beginner.

1. ## Computational issue in Java for beginner.

Hi All!

To begin, I am currently teaching myself Java via books and the MIT free online course exchange. Its a bummer not having a teacher to check your work, but I trudge and overcome.

This java code is supposed to allow the user to enter the current wage of an employee and the total hours worked that week. The code will display the amount the user should pay the employee. Errors are given for working beyond 60 hours and/or making less than the minimum wage of \$8/hr.

The code compiled without errors and when executed mostly works. The problem I am having is a computational one...i think. The only time it does not work is when I enter a wage over 40 hours per week. It is coming up with a negative number.

Please take a look at my code and let me know what I can do to fix it?

import java.util.Scanner;
public class FooCorporation {
/**
* This is the 2nd Assignment in the
* MIT Beginners Java Course.
*/
public static double stdPay(double w, double h){
return h * w;
/**
* This calculates how much an employee gets paid if they work 40 hours or less.
*/
}
public static double overTimePay(double w, double h){
return ((h - 40) * w * 1.5)+(40 * w);
/**
* This should calculate how much an employee gets paid if they worked overtime.
*/
}
public static void main(String[] args) {
Scanner myKeyboard = new Scanner(System.in);
double w; double h;
System.out.println("What is the employee's current wage?");
w = myKeyboard.nextDouble();
if (w<8) {
System.out.println("Minimum wage not meet.");
} else {
System.out.println("How many hours did the employee work?");
h = myKeyboard.nextDouble();
if ((h>60)||(h<0)) {
System.out.println("Invalid number used for hour total.");
} else if (h<=40) {
System.out.println("The employee is to be paid" + stdPay(h,w) + "dollars.");
} else {
System.out.println("The employee is to be paid" + overTimePay(h,w) + "dollars.");
}
}
}
}

• You're h and w parameters are backwards. Effectively, you have provided this to the algorithm:
Code:
`return ((8 - 40) * 41 * 1.5) + (40 * 41);`
I determine that by looking at this: `overTimePay(h,w)`, where w is the wage and h is the hours, but the signature like so: `double overTimePay(double w, double h)` when used with the variables in the order you have indicate that the wage and hours have been reversed. Same goes with the stdpay as well, but since its a flat calculation this is not notacble (a * b = b * a).

Modify either the signature or the arguments provided to reverse them, and see if that works for you.

• ## Users who have thanked Fou-Lu for this post:

Skuby35 (02-14-2011)

• Swapping the signature worked just great!

• Originally Posted by Skuby35
Swapping the signature worked just great!
Good to hear.
Also, in the future can you please wrap code in [code][/code] or [php][/php] tags in order to preserve the formatting. This was relatively short, but the longer it gets, the hard it is to trace (especially for syntax errors).

•