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(); } }
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!
Comment