Question: Salary calculation: Overloading a constructor. The program below calculates a tax rate and tax to pay given an annual salary. The program uses a class,
Salary calculation: Overloading a constructor.
The program below calculates a tax rate and tax to pay given an annual salary. The program uses a class, TaxTableTools, which has the tax table built in. Run the program with annual salaries of 10000, 50000, 50001, 100001 and -1 (to end the program) and note the output tax rate and tax to pay.
1/Overload the constructor.
a.Add to the TaxTableTools class an overloaded constructor that accepts the base salary table and corresponding tax rate table as parameters.
b.Modify the main method to call the overloaded constructor with the two tables (arrays) provided in the main method. Be sure to set the nEntries value, too.
Note that the tables in the main method are the same as the tables in the TaxTableTools class. This sameness facilitates testing the program with the same annual salary values listed above.
2/Test the program with the annual salary values listed above.
a.Modify the salary and tax tables
b.Modify the salary and tax tables in the main method to use different salary ranges and tax rates.
c.Use the just-created overloaded constructor to initialize the salary and tax tables.
Test the program with the annual salary values listed above.
import java.util.Scanner;
public class IncomeTaxMain { public static void main (String [] args) { final String PROMPT_SALARY = " Enter annual salary (-1 to exit)"; Scanner scnr = new Scanner(System.in); int annualSalary; double taxRate; int taxToPay; int i;
// Tables to use in the exercise are the same as in the TaxTableTools class int [] salaryRange = { 0, 20000, 50000, 100000, Integer.MAX_VALUE }; double [] taxRates = { 0.0, 0.10, 0.20, 0.30, 0.40 };
// Access the related class TaxTableTools table = new TaxTableTools();
// Get the first annual salary to process annualSalary = table.getInteger(scnr, PROMPT_SALARY);
while (annualSalary >= 0) { taxRate = table.getValue(annualSalary); taxToPay= (int)(annualSalary * taxRate); // Truncate tax to an integer amount System.out.println("Annual Salary: " + annualSalary + "\tTax rate: " + taxRate + "\tTax to pay: " + taxToPay);
// Get the next annual salary annualSalary = table.getInteger(scnr, PROMPT_SALARY); } } }
import java.util.Scanner;
public class TaxTableTools {
/** This class searches the 'search' table with a search argument and returns the corresponding value in the 'value' table. Variable 'nEntries' has the number of entries in each table. */ private int [] search = { 0, 20000, 50000, 100000, Integer.MAX_VALUE }; private double [] value = { 0.0, 0.10, 0.20, 0.30, 0.40 }; private int nEntries;
// ***********************************************************************
// Default constructor public TaxTableTools () { nEntries = search.length; // Set the length of the search table } // ***********************************************************************
// Overloaded constructor
// FIXME: Add an overloaded constructor to load the search and value tables. // FIXME: Be sure to set the nEntries value, too.
// ***********************************************************************
// Method to prompt for and input an integer public int getInteger(Scanner input, String prompt) { int inputValue = 0; System.out.println(prompt + ": "); inputValue = input.nextInt(); return inputValue; }
// ***********************************************************************
// Method to get a value from one table based on a range in the other table
public double getValue(int searchArgument) { double result; boolean keepLooking; int i;
result = 0.0; keepLooking = true; i = 0;
while ((i < nEntries) && keepLooking) { if (searchArgument <= search[i]) { result = value[i]; keepLooking = false; } else { ++i; } }
return result; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
