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:

  1. asks user for employee's ID, gross pay, and state and federal tax rates
  2. creates a Payroll object with these values
  3. calls Payroll's method to calculate the net pay for the employee
  4. 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 payrolls = new ArrayList<>(); int id; do { System.out.print("Enter employee id: "); id = Integer.parseInt(sc.nextLine().trim()); while(id < 0) { System.out.println("Emp id cannot be negative!"); System.out.print("Enter employee id: "); id = Integer.parseInt(sc.nextLine().trim()); } if(id == 0) { System.out.println(" ALL EMPLOYEE DATA: " + "------------------"); System.out.printf("%-10s %-15s %-15s %-15s %-1s ", "EMP ID", "GROSS PAY($)", "STATE TAX($)", "FEDERAL TAX($)", "NET PAY($)"); for(Payroll p : payrolls) System.out.println(p); System.exit(0); } System.out.print("Enter gross pay: $"); double grossPay = Double.parseDouble(sc.nextLine().trim()); while(grossPay < 0) { System.out.println("Gross pay should be positive!"); System.out.print("Enter gross pay: $"); grossPay = Double.parseDouble(sc.nextLine().trim()); } System.out.print("Enter state tax rate (%): "); double stateTaxRate = Double.parseDouble(sc.nextLine().trim()); while(stateTaxRate < 0) { System.out.println("State tax rate should be positive!"); System.out.print("Enter state tax rate (%): "); stateTaxRate = Double.parseDouble(sc.nextLine().trim()); } System.out.print("Enter federal tax rate (%): "); double federalTaxRate = Double.parseDouble(sc.nextLine().trim()); while(federalTaxRate < 0) { System.out.println("Federal tax rate should be positive!"); System.out.print("Enter federal tax rate (%): "); federalTaxRate = Double.parseDouble(sc.nextLine().trim()); } Payroll payroll = new Payroll(id, grossPay, stateTaxRate, federalTaxRate); payrolls.add(payroll); System.out.println(); }while(id != 0); } }

thank you for your time

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!