JAVA PROGRAMMING The below program uses an array salaryBase to hold the cutoffs
ID: 3664234 • Letter: J
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 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 +
" Tax rate: " + taxRate +
" Tax 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;
}
}
Explanation / Answer
1. After running the program for annual salary 40000 and 50000 and then enter 0
a) For annual salaries of 40000
taxRate will be . 20
taxToPay will be 40000 x .20 = 8000
b)For annual salaries of 50000
taxRate will be . 30
taxToPay will be 50000 x .30 = 15000
2. Modify the program to use two parallel arrays named annualSalaries and taxesToPay
a) For implementing array of anuualSalaries that take input from user, refer following given code:
Scanner s=new Scanner(System.in);
System.out.println("enter number of anuualSalaries");
int n=s.nextInt();
int anuualSalaries[]=new int[n];
System.out.println("enter anuualSalaries");
for(int i=0;i<n;i++){//for reading array
anuualSalaries[i]=s.nextInt();
b) For implementing array of taxestoPay refer following code
int taxestoPay[] = new int[anuualSalaries.length];
for (int i = 0; i < taxestoPay.length; i++) {
taxestoPay[i] = "enter the result value of tax to pay"
}
4. For two dimensional array of salariesAndTaxes refer following given code
int[][] salariesAndTaxes = new int[2][];
salariesAndTaxes[0] = new int[10];
salariesAndTaxes[1] = new int[10];
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.