WageCalculator.java: Implement a public class called WageCalculator as specified
ID: 3796218 • Letter: W
Question
WageCalculator.java:
Implement a public class called WageCalculator as specified in the UML class diagram. Include the three comments // fields, // constructors, and // methods to group the different class members. Notice that the class WageCalculator has two (overloaded) constructors. Both constructors should provide some input validation. They should ensure that the following rules are followed:
a) The base rate (the hourly rate for the first 40 hours) can’t be less than the federal minimum wage (http://www.dol.gov/whd/minimumWageCalculator.htm)
b) The number of hours worked can’t be negative. If any (or all) of those rules are violated an exception should be thrown and no field should be assigned an invalid value. For now you can use the following statement: throw new IllegalArgumentException(); WageCalculator – constructor with 2 parameters Notice that no value for the overtime multiplier has been provided. We assume that the overtime multiplier should be by default 1.5. That means that the overtime rate should be by default 50% higher than the base rate. Remember to implement input validation: In case of invalid arguments (input) throw an exception as described above.
WageCalculator – constructor with 3 parameters This overloaded constructor provides an additional parameter: the overtime multiplier (different from the overtime rate). The overtime rate can be calculated by multiplying the base rate with the overtime multiplier. E.g. If the overtimeMultiplier is 2, workers get paid twice as much for overtime. We also need to check the validity of this third argument. In addition to the two rules described above we are going to ensure that the overtimeMultiplier is greater 1. In other words: the overtime rate always needs to be greater than the base rate. If any of these three rules are violated an exception should be thrown (see above). Avoid Code Duplication Code duplication is often seen as a sign of bad code. So let’s build a good habit early on by avoiding it. Rather than implementing both overloaded constructors independently we can implement the 2-parameter constructor by calling the 3-parameter constructor. This allows us to re-use the code (functionality) we wrote in the 3-parameter constructor and to avoid code duplication. In order to call the 3-parameter constructor we need to pass 3 arguments. How can you come up with the third argument? In case of the 2-parameter constructor the overtime multiplier is always 1.5 (default value)
Method baseWage
The base wage is calculated like this: base rate * hours worked (up to 40) If a worker works more than 40 hours all additional hours are considered overtime wage.
Method overtimeWage The overtime wage is calculated like this: overtime rate * hours worked in addition to the first 40 hours If a worker works 40 hours or less the overtime wage is 0. Method totalWage The total wage is the sum of the base wage and the overtime wage. Call the method baseWage and overtimeWage to implement totalWage.
Method toString The method toString returns a String in the following format: baseRate: baseRateValue, hours: hoursValue, overtimeMultiplier: overtimeMultiplierValue e.g. baseRate: 20, hours: 60, overtimeMultiplier: 2 Notice that the test client in WageCalculatorApp doesn’t call the method toString. It is still your responsibility to implement it. You also want to make sure that your toString method is tested. Just don’t submit that test code when you turn in the assignment.
WageCalculatorApp.java
Class WageCalculatorApp includes the main method and it is a test client for class WageCalculator. In order to test the class WageCalculator do the following:
Read in the base rate and the hours from the user In order to read in floating point numbers use the method nextDouble from class Scanner.
If desired read in the overtime multiplier and create an instance of WageCalculator. Ask the user whether s/he would like to enter the overtime multiplier. The user should be able to answer with yes or no in upper case, lower case or mixed case. Read in the overtime multiplier only if the user answered yes (or YES, yES, etc.) Create an instance of WageCalculator based on the data provided by the user. Call the 2-parameter constructor if no overtime multiplier was provided and the 3-parameter constructor otherwise. For invalid input an exception will be thrown. This is part of the expected output. (Do not catch the exception)
Print the base wage, the overtime wage, and the total wage Make sure to call the corresponding methods from class WageCalculator.
Display the information as shown in the output.
WageCalculator baseRate double overtimeMultiplier double hours int WageCalculator (baseRate double, hours int) WageCalculator (baseRate double, overtimeMultiplier: double, hours int) baseWage double overtime Wage double total Wage double toString StringExplanation / Answer
import java.util.Scanner;
public class WageCalculatorApp {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try{
WageCalculator wageCalculator = null;
System.out.print("Base Rate: ");
double baseRate = sc.nextDouble();
System.out.print("Hours: ");
int hours = sc.nextInt();
System.out.print("Would you like to enter overtime multiplier (yes/no) :");
String conf = sc.next();
if(conf.equalsIgnoreCase("yes")){
System.out.print("Overtime multiplier: ");
double overtimeMultiplier = sc.nextDouble();
wageCalculator = new WageCalculator(baseRate, hours,overtimeMultiplier);
}
else if(conf.equalsIgnoreCase("no")){
wageCalculator = new WageCalculator(baseRate, hours);
}
else {
System.out.println("Invalid Input");
return;
}
System.out.println("Base wage: "+wageCalculator.baseWage());
System.out.println("Overtime wage: "+wageCalculator.overtimeWage());
System.out.println("Total wage: "+wageCalculator.totalWage());
}
catch(IllegalArgumentException iae){
iae.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
}
class WageCalculator{
// fields
private double baseRate;
private double overtimeMultiplier;
private int hours;
// constructors
public WageCalculator(double baseRate,int hours)throws IllegalArgumentException {
if(baseRate<7.5d)
throw new IllegalArgumentException("wege is less then federal minimum wage");
if(hours<0)
throw new IllegalArgumentException("hours can not be negative ");
this.baseRate = baseRate;
this.hours = hours;
this.overtimeMultiplier = 1.5d;
}
public WageCalculator(double baseRate,int hours,double overtimeMultiplier)throws IllegalArgumentException {
this(baseRate,hours);
if(overtimeMultiplier<1.0d)
throw new IllegalArgumentException("overtime multiplier can not be less then 1 ");
this.overtimeMultiplier = overtimeMultiplier;
}
// methods
public double baseWage(){
if(hours<40)
return baseRate*hours;
else
return 40*baseRate;
}
public double overtimeWage(){
if(hours>40){
return ((hours-40)*overtimeMultiplier*baseRate);
}
else
return 0.0d;
}
public double totalWage(){
return baseWage()+overtimeWage();
}
public String toString(){
return "baseRate : "+baseRate+", Hours : "+hours+", overtimeMultiplier : "+overtimeMultiplier+" ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.