Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.");
    }
    }
    }
    }
    Last edited by Skuby35; 02-15-2011 at 10:39 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

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

    Skuby35 (02-14-2011)

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Swapping the signature worked just great!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Skuby35 View Post
    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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Tags for this Thread

    Posting Permissions

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