Question

Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in....

Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), 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.

a. Modify the TaxTableTools class to use a setter method that accepts a new salary and tax rate table.

b. Modify the program to call the new method, and run the program again, noting the same output.

Sample Input and Output:

Enter annual salary (-1 to exit):
100000
Annual Salary: 100000 Tax rate: 0.35 Tax to pay: 35000

Enter annual salary (-1 to exit):
200000
Annual Salary: 200000 Tax rate: 0.4 Tax to pay: 80000

Enter annual salary (-1 to exit):
-1

IncomeTaxClassTemplate

package Labs.Lab08;

import java.util.Scanner;

public class IncomeTaxClassTemplate {

// 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 = "\nEnter 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.15, 0.25, 0.35,

0.40 };

// FIXME: Access the related class to declare a variable with an

instance of TaxTableTools class

// 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);

// 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);

}

}

}

TaxTableToolsTemplate

package Labs.Lab08;

public class TaxTableToolsTemplate {

/**

* 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 TaxTableToolsTemplate() {

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.

//

***********************************************************************

// 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;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

TaxTableTools.java

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.

// ***********************************************************************

// Method to get a value from one table based on a range in the other table

public void setTables(int salary[], double taxable[]){

this.search = salary;

this.value = taxable;

}

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;

}

}

IncomeTaxClass.java

import java.util.Scanner;

public class IncomeTaxClass {

// 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 = "\nEnter 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.15, 0.25, 0.35, 0.40 };

// FIXME: Access the related class to declare a variable with an instance of

// TaxTableTools 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);

table.setTables(salary, 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);

}

}

}

Add a comment
Know the answer?
Add Answer to:
Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write Java program ( see IncomeTaxOverloadClassTemplate) calculates a tax rate and tax to pay given an...

    Write Java program ( see IncomeTaxOverloadClassTemplate) calculates a tax rate and tax to pay given an annual salary. The program uses a class,(see TaxTableToolsOverloadTemplate), 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. Overload the constructor. Add to the TaxTableTools class an overloaded constructor that accepts the base salary table and corresponding tax rate table...

  • Separating calculations into methods simplifies modifying and expanding programs. The following program calculates the tax rate...

    Separating calculations into methods simplifies modifying and expanding programs. The following program calculates the tax rate and tax to pay, using methods. One method returns a tax rate based on an annual salary. Run the program below with annual salaries of 40000, 60000, and 0. Change the program to use a method to input the annual salary. Run the program again with the same annual salaries as above. Are results the same? This is the code including what I am...

  • The program calculates a tax rate and tax to pay given an annual salary. The program...

    The program 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. The output should be as follows: Enter annual salary (-1 to exit): 10000 Annual Salary: 10000 Tax rate: 0.1 Tax to pay: 1000 Enter annual salary...

  • 1. Employees and overriding a class method The Java program (check below) utilizes a superclass named...

    1. Employees and overriding a class method The Java program (check below) utilizes a superclass named EmployeePerson (check below) and two derived classes, EmployeeManager (check below) and EmployeeStaff (check below), each of which extends the EmployeePerson class. The main program creates objects of type EmployeeManager and EmployeeStaff and prints those objects. Run the program, which prints manager data only using the EmployeePerson class' printInfo method. Modify the EmployeeStaff class to override the EmployeePerson class' printInfo method and print all the...

  • this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...

    this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);    Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // Read and set base and height for triangle1 (use setBase() and setHeight())    // Read and set base and height for triangle2 (use setBase() and setHeight())    // Determine larger triangle (use getArea())    private int base; private int height; private...

  • 2. Given following Java program, convert to class diagram, create two instances of Employee, and initialize...

    2. Given following Java program, convert to class diagram, create two instances of Employee, and initialize them with following values: For employee1, name – your friend’s name, salary = 30000, 8 hours and avail is false For employee2, name – your friend’s name, salary = 80000, 10 hours and avail is true public class Employee { private String name; private double salary ; private int hours ; private boolean avail ; public Employee (double r, double h, boolean hd) {...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • The following is the class definition for Multiplier. This class has five (5) members: two private...

    The following is the class definition for Multiplier. This class has five (5) members: two private instance variables two private methods one public method this is only public method for the class it calls other two private methods import java.util.*; //We need to use Scanner & Random in this package public class Multiplier {        private int answer; //for holding the correct answer        private Random randomNumbers = new Random(); //for creating random numbers        //ask the user to work...

  • in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "Ret...

    in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "RetailItem" class which simulates the sale of a retail item. It should have a constructor that accepts a "RetailItem" object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased. Your class should have these methods: getSubtotal: returns the subtotal of the sale, which is quantity times price....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT