Answer: Provided in the explanation section
Step-by-step explanation:
Programs:
IncomeTax.java
import java.util.Scanner;
public class IncomeTax {
// Method to get a value from one table based on a range in the other table
public static double getCorrespondingTableValue(int search, int [] baseTable, double [] valueTable) {
int baseTableLength = baseTable.length;
double value = 0.0;
int i = 0;
boolean keepLooking = true;
i = 0;
while ((i < baseTableLength) && keepLooking) {
if (search <= baseTable[i]) {
value = valueTable[i];
keepLooking = false;
}
else {
++i;
}
}
return value;
}
public static int readInput(Scanner scan){
System.out.println("\\Enter annual salary (0 to exit): ");
int annualSalary = scan.nextInt();
return annualSalary;
}
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
int annualSalary = 0;
double taxRate = 0.0;
int taxToPay = 0;
int [] salaryBase = { 20000, 50000, 100000, 999999999 };
double [] taxBase = { .10, .20, .30, .40 };
// FIXME: Change the input to come from a method
annualSalary = readInput(scnr);
while (annualSalary > 0) {
taxRate = getCorrespondingTableValue(annualSalary, salaryBase, taxBase);
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
// FIXME: Change the input to come from a method
annualSalary = readInput(scnr);
}
return;
}
}
Output:
Enter annual salary (0 to exit):
10000
Annual salary: 10000 Tax rate: 0.1 Tax to pay: 1000
Enter annual salary (0 to exit):
50000
Annual salary: 50000 Tax rate: 0.2 Tax to pay: 10000
Enter annual salary (0 to exit):
0