Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Hours worked help

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Hours worked help

    Hi I cannot figure out why my program won't calculate the overtime pay correctly. Overtime is supposed to be hours worked over 40 paid at 2.5. Can someone please tell me what I am missing?
     import java.util.Scanner;
    public class If {
     
    	  public static void main(String[] args) {
    	    Scanner input = new Scanner(System.in); 
    		// TODO Auto-generated method stub
    	                float payRate;
                                       float timehours;
    		 float pay; 
     
     
    	    System.out.println("Enter payrate per hour: ");	   
                           payRate = input.nextFloat(); 			
    	    System.out.println("How many hours did you work?: "); 	    
                      timehours= input.nextFloat();	   
    	     if(timehours> 40) 
    	    {
    		pay = (float) (payRate*40+2.5*timehours); //over time pay is calculated
     
    	    }	
    	    else timehours <= 40
    	    {
    		pay= payRate*timehours; 
    	    }
     
    	    System.out.println("Your weekly pay is " + pay); 
    	  }//end of main
    	//end of class
     
    }
    Last edited by glacier23; December 10th, 2010 at 01:23 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Hours worked help

    Hello glacier23,

    Welcome to the Java Programming Forums.

    Firstly, I personally wouldn't call a class "If". It's best not to give a class the same name as a Java keyword.

    You have a few small errors in your code. You need to turn the else into an else if:

    import java.util.Scanner;
     
    public class If {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */	
     
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
     
                        float payRate;
                        float timehours;
                        float pay = 0;
     
            System.out.println("Enter payrate per hour: ");    
            payRate = input.nextFloat();            
            System.out.println("How many hours did you work?: ");      
            timehours= input.nextFloat();    
     
            if(timehours > 40)
            {
            pay = (float)(payRate*40+2.5*timehours); //over time pay is calculated         
            }  
            else if (timehours <= 40)
            {
            pay = payRate*timehours;
            }       
            System.out.println("Your weekly pay is " + pay);
          }//end of main
    	// end of class
     
    }

    Also, the variable 'pay' was not initialized.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hours worked help

    Hi Thanks for the help with the "Else if" but for some reason it still isn't doing the calculation correctly for overtime. Say I work 45 hours at $2.00 an hour, anything over 40 hours should calculate my pay at 2.5 times the normal rate. So it is $90.00 for 40 hours but then over 40 hours it should calculate the overtime. So it is outputting 192.5 instead of 115.00. Any suggestions?

  4. #4
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Hours worked help

    Your calculation is wrong when the worker worked over 40 hours. The calculation should be the normal pay rate multiplied by 40 hours worked, plus 2.5 multiplied by the pay rate multiplied by the hours worked over 40 (which is timehours minus 40)

  5. The Following User Says Thank You to DavidFongs For This Useful Post:

    glacier23 (December 11th, 2010)

  6. #5
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Hours worked help

    Quote Originally Posted by JavaPF View Post
    You have a few small errors in your code. You need to turn the else into an else if:
    Why? If anything for readability it should be an if and else. It is either above 40, or its not.

  7. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Re: Hours worked help

    Quote Originally Posted by DavidFongs View Post
    Your calculation is wrong when the worker worked over 40 hours. The calculation should be the normal pay rate multiplied by 40 hours worked, plus 2.5 multiplied by the pay rate multiplied by the hours worked over 40 (which is timehours minus 40)
    Thank You this was the solution I was looking for.

Similar Threads

  1. need in few hours help me
    By erinbasim in forum Java Theory & Questions
    Replies: 3
    Last Post: February 2nd, 2010, 06:39 PM
  2. project needed in two hours
    By erinbasim in forum Project Collaboration
    Replies: 1
    Last Post: February 2nd, 2010, 04:21 AM
  3. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM