Question: JAVA PROGRAMMING The below program uses an array salaryBase to hold the cutoffs for each salary level and a parallel array taxBase that has the
JAVA PROGRAMMING
The below program uses an array salaryBase to hold the cutoffs for each salary level and a parallel array taxBase that has the corresponding tax rate.
1-Run the program and enter annual salaries of 40000 and 50000, then enter 0.
2-Modify the program to use two parallel arrays named annualSalaries and taxesToPay, each with 10 elements. Array annualSalaries holds up to 10 annual salaries entered; array taxesToPay holds up to 10 corresponding amounts of taxes to pay for those annual salaries. Print the total annual salaries and taxes to pay after all input has been processed.
3-Run the program again with the same annual salary numbers as above.
4-Challenge: Modify the program from the previous step to use a 2-dimensional array of 10 elements named salariesAndTaxes instead of two one-dimensional parallel arrays. The 2D array's first column will hold the salaries, the second the taxes to pay for each salary.
The following program calculates the tax rate and tax to pay based on annual income.
import java.util.Scanner;
public class IncomeTax { public static void main (String [] args) { final int MAX_ELEMENTS = 10; Scanner scnr = new Scanner(System.in); int annualSalary = 0; double taxRate = 0.0; int taxToPay = 0; int numSalaries = 0; boolean keepLooking = true; int i = 0;
int [] salaryBase = { 20000, 50000, 100000, 999999999 }; double [] taxBase = { .10, .20, .30, .40 }; // FIXME: Define annualSalaries and taxesToPay arrays to hold 10 elements each. // FIXME: Use the final constant MAX_ELEMENTS to declare the arrays System.out.println(" Enter annual salary (0 to exit): "); annualSalary = scnr.nextInt();
// FIXME: Extend the while condition with a test that ensures the number of // salaries in the array does not exceed the size of the arrays while (annualSalary > 0) { // Valid annualSalary entered i = 0; keepLooking = true; // Search for appropriate table row for given annualSalary while ((i < salaryBase.length) && keepLooking) { if (annualSalary < salaryBase[i]) { taxRate = taxBase[i]; keepLooking = false; } else { ++i; } } // End inner while loop (search for appropriate table row)
taxToPay = (int) (annualSalary * taxRate); // Truncate tax to an integer amount
// FIXME: Insert code to include entries to the annual salaries and taxes to pay // FIXME: tables. Replace the appropriate variables with the array reference. System.out.println("Annual salary: " + annualSalary + "\tTax rate: " + taxRate + "\tTax to pay: " + taxToPay);
// Get the next annual salary System.out.println(" Enter annual salary (0 to exit): "); annualSalary = scnr.nextInt(); } // End outer while loop (valid annualSalary entered)
// FIXME: Challenge - add code to sum the annual salaries and taxes to pay // and print the totals
return; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
