Question

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.

  1. Overload the constructor.
    1. Add to the TaxTableTools class an overloaded constructor that accepts the base salary table and corresponding tax rate table as parameters.
    2. 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.
    3. 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.
    4. Test the program with the annual salary values listed above.
  2. Modify the salary and tax tables
    1. Modify the salary and tax tables in the main method to use different salary ranges and tax rates.
    2. Use the just-created overloaded constructor to initialize the salary and tax tables.
    3. Test the program with the annual salary values listed above.

Sample Input and Output:

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

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

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

IncomeTaxOverloadClassTemplate

package Labs.Lab08.Q2;

import java.util.Scanner;

import Labs.Lab08.Q1.TaxTableTools;

public class IncomeTaxOverloadClassTemplate {

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;

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

}

}

}

TaxTableToolsOverloadTemplate

package Labs.Lab08.Q2;

import java.util.Scanner;

// TODO: rename to TaxTableTools

public class TaxTableToolsOverloadTemplate {

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

}

}

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

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

import java.util.Scanner;

import Labs.Lab08.Q1.TaxTableTools;

public class IncomeTaxOverloadClass {

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;

// Tables to use in the exercise are the same as in the TaxTableTools class

int[] salaryRange = { 0, 20000, 50000, 70000, 100000, Integer.MAX_VALUE };

double[] taxRates = { 0.0, 0.10, 0.20, 0.30, 0.35, 0.40 };

// Access the related class

TaxTableTools table = new TaxTableTools(salaryRange, taxRates);

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

// TODO: rename to TaxTableTools

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 tabl

}

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

// Overloaded constructor

// FIXME: Add an overloaded constructor to load the search and value tables.

// FIXME: Be sure to set the nEntries value, too.

public TaxTableTools(int salaries[], double values[]){

this.search = salaries;

this.value = values;

nEntries = search.length;

}

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

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

}

}

Add a comment
Know the answer?
Add Answer to:
Write Java program ( see IncomeTaxOverloadClassTemplate) calculates a tax rate and tax to pay given an...
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 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...

  • 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...

  • 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...

  • 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...

  • Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for...

    Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for sample program: 3.5 import java.util.Scanner; public class HourToMinConv {    public static void outputMinutesAsHours(double origMinutes) {       /* Your solution goes here */    }    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       double minutes;       minutes = scnr.nextDouble();       outputMinutesAsHours(minutes); // Will be run with 210.0, 3600.0, and 0.0.       System.out.println("");    } } 2....

  • 14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a...

    14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a set numbers (assume that the numbers in the set is less than 20) using overload methods. The program first prompts the user to enter a character; if the character is 'I' or 'i', then invokes the appropriate methods to process integer data set; if the character is 'R' or 'r', then invokes the appropriate methods to process real number data set; if the inputted...

  • Write the following program in Java using Eclipse. A cash register is used in retail stores...

    Write the following program in Java using Eclipse. A cash register is used in retail stores to help clerks enter a number of items and calculate their subtotal and total. It usually prints a receipt with all the items in some format. Design a Receipt class and Item class. In general, one receipt can contain multiple items. Here is what you should have in the Receipt class: numberOfItems: this variable will keep track of the number of items added to...

  • Using Java write a program that performs the following: 1. Define a class that contains an...

    Using Java write a program that performs the following: 1. Define a class that contains an array of type int as a data member. 2. Define a constructor that creates an array of 1024 elements and assigns it to the class data member. The constructor shall populate the elements with random numbers ranging in value from 1 to 1024. 3. Define and implement a search() method that take a parameter of type int and returns the index location if the...

  • Task 3: Main Program Create a main program class that: Creates three or more Nurse instances...

    Task 3: Main Program Create a main program class that: Creates three or more Nurse instances assigned to different shifts. Creates three or more Doctor instances. Creates three or more Patient instances with pre-determined names and manually assigned physicians chosen from the pool of Doctor instances previously created. Generates another 20 Patient instances using randomly generated names and randomly assigns them physicians chosen from the pool of Doctor instances previously created. Prints the toString() values for all employees. Prints the...

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