Question: Need help rewriting java code using string instead of double (a more basic entry way of coding) I don't really understand 1. Write the class
Need help rewriting java code using string instead of double (a more basic entry way of coding) I don't really understand
1. Write the class Payroll that stores an employee's ID, gross pay, state tax rate, and federal tax rate. Among other methods to make it a complete class, this class should have a method that calculates the employee's net pay.
net pay = gross pay - state tax - federal tax
Note that you need to calculate the state/federal tax based on the rate given and the gross pay.
2. Write a tester that has a loop that:
- asks user for employee's ID, gross pay, and state and federal tax rates
- creates a Payroll object with these values
- calls Payroll's method to calculate the net pay for the employee
- displays the employee's ID and net pay
The loop ends when the user enters 0 for employee ID.
The tester should not accept negative value for state or federal tax rate. It should also check to make sure that net pay returned from calculate method is not negative (meaning the taxes are higher than the gross pay). If it is negative, it should print out an error instead. Use String not double
public class Payroll { private int id; private double grossPay, stateTaxRate, federalTaxRate; public Payroll() { this.id = 0; this.grossPay = this.stateTaxRate = this.federalTaxRate = 0.0; }
public Payroll(int id, double grossPay, double stateTaxRate, double federalTaxRate) { this.id = id; this.grossPay = grossPay; this.stateTaxRate = stateTaxRate; this.federalTaxRate = federalTaxRate; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public double getGrossPay() { return grossPay; }
public void setGrossPay(double grossPay) { this.grossPay = grossPay; }
public double getStateTaxRate() { return stateTaxRate; }
public void setStateTaxRate(double stateTaxRate) { this.stateTaxRate = stateTaxRate; }
public double getFederalTaxRate() { return federalTaxRate; }
public void setFederalTaxRate(double federalTaxRate) { this.federalTaxRate = federalTaxRate; } public double getStateTax() { return(grossPay * (stateTaxRate / 100)); } public double getFederalTax() { return(grossPay * (federalTaxRate / 100)); } public double computeNetPay() { return(grossPay - (getStateTax() + getFederalTax())); } @Override public String toString() { return(String.format("%-10d %-15.2f %-15.2f %-15.2f %-1.2f", id, grossPay, getStateTax(), getFederalTax(), computeNetPay())); } }
PayrollTester.java (Main class)
import java.util.ArrayList; import java.util.Scanner;
public class PayrollTester { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList
thank you for your time
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
