Question

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 working on added. I will not run. I need to remove some areas to designate the scanner as the source of input but I am unsure of which area.

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 void readInput(int salaryOne, int salaryTwo, int salaryThree);

annualSalary = 0;

Scanner scnr = new Scanner(System.in);
scnr nextInt();
new annualSalary;

public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
int annualSalary = 0;
double taxRate = 0.0;
int taxToPay = 0;
int i = 0;

int [] salaryBase = { 20000, 50000, 100000, 999999999 };
double [] taxBase = { .10, .20, .30, .40 };
  
// FIXME: Change the input to come from a method
System.out.println("\nEnter annual salary (0 to exit): ");
annualSalary = scnr.nextInt();

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
System.out.println("\nEnter annual salary (0 to exit): ");
annualSalary = scnr.nextInt();
}

return;
}
}

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

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("\nEnter 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

Add a comment
Know the answer?
Add Answer to:
Separating calculations into methods simplifies modifying and expanding programs. The following program calculates the tax rate...
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...

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

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

  • Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both...

    Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...

  • ***\\\\I have been trying to run this but I think there is something wrong with the...

    ***\\\\I have been trying to run this but I think there is something wrong with the variables not being saved properly. I really appreciate some help. *** class Main { public static void main(String[] args) { final int NUM_ITEMS = 2; ItemToPurchase[] itemList = new ItemToPurchase[NUM_ITEMS];    clearScreen();    promptItem(itemList); total(itemList); }    public static void promptItem(ItemToPurchase[] itemList) { Scanner scnr = new Scanner(System.in);    String tempItem; int tempPrice; int tempQuantity;    int count; //-----Print prompts by number of items------//...

  • 1. Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS...

    1. Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 1, 2, 2} and matchValue is 2 , then numMatches should be 3. Your code will be tested with the following values: matchValue: 2, userValues: {2, 1, 2, 2} (as in the example program above) matchValue: 0, userValues: {0, 0, 0, 0} matchValue: 10, userValues: {20, 50, 70, 100} What i am given: import java.util.Scanner; public class FindMatchValue...

  • Write an expression that executes the loop while the user enters a number greater than or...

    Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds,...

  • Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and...

    Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program: Min miles: -10 Max miles: 40 import java.util.Scanner; public class ArraysKeyValue { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_ROWS = 2; final int NUM_COLS = 2; int [][] milesTracker = new int[NUM_ROWS][NUM_COLS]; int i; int j; int maxMiles; // Assign with first element in...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

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