Question
Calculate salary: Using classes. The program below uses a class, TaxTableTools, which has a tax table built in. The main method prompts for a salary,
Calculate salary: Using classes.
The program below uses a class, TaxTableTools, which has a tax table built in. The main method prompts for a salary, then uses a TaxTableTools method to get the tax rate. The program then calculates the tax to pay and displays the results to the user. 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.
- Modify the TaxTableTools class to use a setter method that accepts a new salary and tax rate table.
- Modify the program to call the new method, and run the program again, noting the same output.
Note PLEASE HELP FIXING THE " FIXME" PART OF 2 Classes: IncomeTaxMain and TaxTableTools
import java.util.Scanner;
public class IncomeTaxMain {
// Method to prompt for and input an integer public static int getInteger(Scanner input, String prompt) { int inputValue; System.out.println(prompt + ": "); inputValue = input.nextInt(); return inputValue; } //
// ***********************************************************************
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;
int [] salary = { 0, 20000, 50000, 100000, Integer.MAX_VALUE }; double [] taxTable = { 0.0, 0.10, 0.20, 0.30, 0.40 };
// Access the related class TaxTableTools table = new TaxTableTools();
// FIXME: Call a setter method in the TaxTableClass that supplies new // tables for the class to work with. The method should be called // with: table.setTables(salary, taxTable); public void table.setTable(salary, taxTable){ this.salary = salary; this.taxTable = taxTable; }
// Get the first annual salary to process annualSalary = 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 = getInteger(scnr, PROMPT_SALARY); } } }
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 } // ***********************************************************************
// FIXME: Write a void setter method that sets new values for the private // search and value tables. Name the method: setTables // The method receives as parameters tables from which to load the // search and value tables. // *********************************************************************** public void setTables(int [], double []){ this.Tables = t; this.search = s; }
// 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started