Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Jacob Baker
- //Lab-03
- //StartA.java
- //AKA "An Even 'SLICKER' Method Than the Methods Used in Parts B & C" :)
- public class StartA {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- // test methods here:
- // Magic.println(computeTaxes(500000));
- int userIncome = StartD.promptUserGetInt("Enter your Taxable Income");
- Magic.println("You owe this much in federal income taxes: $" + computeTaxes(userIncome));
- }// end main
- //let "double userIncome" take place of the parameter name "taxableIncome"
- public static double computeTaxes(double userIncome) {
- // two arrays whose positions correspond in Marginal Tax Rate and Taxable Income
- double[] taxRate = { 0.10, 0.15, 0.25, 0.28, 0.33, 0.35, 0.396 };
- double[] taxableIncome = { 9275.00, 37650.00, 91150.00, 190150.00, 413350.00, 415050.00, 415050.01 };
- double totalTaxes = 0;
- double lastTaxes = 0;
- //this boolean is used to break the main loop from inside the nested loop
- boolean breakMainLoop = true;
- //condition specific for userIncome > taxableIncome[6]
- if (userIncome >= taxableIncome[6]) {
- for (int i = 0; i < taxableIncome.length && breakMainLoop == true; i++) {
- if (i == 6) {
- lastTaxes += (taxRate[i] * (userIncome - taxableIncome[i]));
- totalTaxes += lastTaxes;
- Magic.println(totalTaxes);
- breakMainLoop = false;
- } else if (i > 0) {
- totalTaxes += (taxRate[i] * (taxableIncome[i] - taxableIncome[i - 1]));
- } else if (i == 0) {
- totalTaxes += taxRate[i] * taxableIncome[i];
- }
- }
- } else {
- //conditions for any userIncome that is less than taxableIncome[6]
- //this loop runs the taxableIncome array elements into the corresponding "if" statement
- for (int i = 0; i < taxableIncome.length && breakMainLoop == true; i++) {
- if (userIncome >= taxableIncome[i]) {
- //this loop runs numbers through corresponding conditionals to determine "totalTaxes"
- for (int j = 0; userIncome >= taxableIncome[j]; j++) {
- if (j > 0) {
- //follows the formula given in the example of the lab instructions
- totalTaxes += (taxRate[j] * (taxableIncome[j] - taxableIncome[j - 1]));
- //checks to see if the next element in the array greater than "userIncome"
- if (userIncome < taxableIncome[j + 1]) {
- //if true, follow the last operation found in the formula example of the lab instructions through this equation
- lastTaxes += taxRate[j + 1] * (userIncome - taxableIncome[j]);
- //add it on to the taxes totaled from previous positions
- totalTaxes += lastTaxes;
- Magic.println(totalTaxes);
- //if the next element in the array is greater, then break the main loop
- breakMainLoop = false;
- }
- } else if (j == 0) {
- //specific for taxable income <= 9752.00
- //value is saved in "totalTaxes" and is run back through the loop to carry on thru "j > 0"
- totalTaxes += taxRate[j] * taxableIncome[j];
- }
- }
- } else if (userIncome <= 9725) {
- totalTaxes += taxRate[0] * userIncome;
- breakMainLoop = false;
- }
- }
- }
- return totalTaxes;
- }
- }// end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement