Salary calculation for employee: How to get the correct output ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raitoHiong
    New Member
    • Nov 2013
    • 7

    Salary calculation for employee: How to get the correct output ?

    Code:
    public class Employee{
    	  String name;
    	  double salary;
    	   
    	   // This is the constructor of the class Employee
    	   public Employee(String EmpName)
    	   {
    	      name = EmpName;
    	   }
    	   
    	   // Calculate weekly pay
    	   public double weeklyPay(double hrsWorked, double hlyPay)
    	   {
    	      double total; 
    	      if (hrsWorked <= 40)
    	         total = hrsWorked * hlyPay;
    	      else
    	         total = (40 * hlyPay) + ((hrsWorked - 40)*1.5*hlyPay);
    	      return total; 
    	   }
    	   
    	   //Monthly salary is calculated by multiplying weeklyPay with 4
    	   public void monthlyPay(double weeklyPay)
    	   {
    		   salary = weeklyPay * 4;
    	   }
    
    	   /* Print the Employee details */
    	   public void printEmployee()
    	   {
    		   System.out.println(name + "' salary for a month is " + salary);
    	   }
    	}
    Code:
    import java.util.Scanner;
    public class EmployeeTest{
    
       public static void main(String args[]){
       
           // Invoking methods for each object created
          final double hourlyPay = 52.50;
          double hWorked, wPay; 
          Scanner input = new Scanner(System.in);
          System.out.print("Please enter the number of hours work: ");
          hWorked = input.nextDouble();
             
          /* Create employee objects using constructor */
          Employee emp = new Employee ("Mike");
    
          /* invoke weeklyPay() method */
          emp.weeklyPay(hourlyPay, hWorked);
          
          /* invoke monthlyPay() method*/
          emp.monthlyPay(weeklyPay);
          
          /* invoke printEmployee() method*/
          emp.printEmployee();
       }
    }
    Output:
    Please enter the number of hours work: 44
    Ahmad salary is 0.0

    (My output should be 9660.0)

    Any help and response will be appreciate.

    Regards!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You are not using the result of the weeklyPay method in your calculation of the monthly pay.
    Did you instead mean

    Code:
    double weeklyPay = emp.weeklyPay();
    then
    Code:
    emp.monthlyPay(weeklyPay);
    ?
    Also, your weeklyPay method in the Employee class takes two arguments of type double but you are calling it without passing any parameters? Are you sure you posted the code that you ran?

    Comment

    • raitoHiong
      New Member
      • Nov 2013
      • 7

      #3
      Very Sorry, i am still a newbie in Java. Is it correct if i write like this to use the result?
      Code:
      emp.weeklyPay(hWorked, hourlyPay);
      The result of weeklyPay multiplying by 4 write like this is it correct ?
      Code:
       //Monthly salary is calculated by multiplying weeklyPay with 4
             public void monthlyPay(double weeklyPay)
             {
                 salary = weeklyPay * 4;
             }
      i am confuse to invoke monthlyPay() method

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        You are not assigning the value that was calculated in the weeklyPay method to any variable. So it is just being lost. Assign it to a variable with

        Code:
        double weeklyPay = emp.weeklyPay(hWorked, hourlyPay);
        Then use that value when calling the monthlyPay method.

        Comment

        • raitoHiong
          New Member
          • Nov 2013
          • 7

          #5
          Thanks for the help...i get it arldy tq...

          Comment

          • rashmiahuja84
            New Member
            • Apr 2014
            • 3

            #6
            Code:
            class Employee{
                  String name;
                  double salary;
             
                   // This is the constructor of the class Employee
                   public Employee(String EmpName)
                   {
                      name = EmpName;
                   }
             
                   // Calculate weekly pay
                   public double weeklyPay(double hrsWorked, double hlyPay)
                   {
                      double total; 
                      if (hrsWorked <= 40)
                         total = hrsWorked * hlyPay;
                      else
                         total = (40 * hlyPay) + ((hrsWorked - 40)*1.5*hlyPay);
                      return total; 
                   }
             
                   //Monthly salary is calculated by multiplying weeklyPay with 4
                   public void monthlyPay(double weeklyPay)
                   {
                       salary = weeklyPay * 4;
                   }
             
                   /* Print the Employee details */
                   public void printEmployee()
                   {
                       System.out.println(name + "' salary for a month is " + salary);
                   }
                }

            Comment

            • rashmiahuja84
              New Member
              • Apr 2014
              • 3

              #7
              Code:
              import java.util.Scanner;
              public class EmployeeTest{
               
                 public static void main(String args[]){
               
                     // Invoking methods for each object created
                    final double hourlyPay = 52.50;
                    double hWorked, wPay; 
                    double weeklyPay=0.0;
                    Scanner input = new Scanner(System.in);
                    System.out.print("Please enter the number of hours work: ");
                    hWorked = input.nextDouble();
               
                    /* Create employee objects using constructor */
                    Employee emp = new Employee ("Mike");
               
                    /* invoke weeklyPay() method */
                    weeklyPay=  emp.weeklyPay(hWorked,hourlyPay);
               
                    /* invoke monthlyPay() method*/
                    emp.monthlyPay(weeklyPay);
               
                    /* invoke printEmployee() method*/
                    emp.printEmployee();
                 }
              }

              Comment

              Working...