Sunday 15 May 2011

java - My tax calculator program is almost complete but with a few issues -



java - My tax calculator program is almost complete but with a few issues -

i having [input] income twice before programme executes taxes owed & using 1 taxation bracket (10%) calculate every other taxation bracket. next code below:

package org.tax.tutorial; import java.util.scanner; public class taxability { //the main method of programme public static void main(string[] args){ int userinput = 0; double bracketone = 0.10; double brackettwo = 0.15; double bracketthree = 0.25; double bracketfour = 0.28; double bracketfive = 0.33; double bracketsix = 0.35; double total = 0.0; //inform user programme system.out.println("this programme designed calcualte annual income , determine how much taxes owe." + "\n"); //inform user taxation brackets system.out.print("based on income may classifed under 1 of 6 brackets:" + "\n" + "\n" + "bracket 1: 0–$8,500 10%" + "\n" + "bracket 2: $34,500 15%" + "\n" + "bracket 3: $34,500–$83,600 25%" + "\n" + "bracket 4: $83,600–$174,400 28%" + "\n" + "bracket 5: $174,400–$379,150 33%" + "\n" + "bracket 6: $379,150 above 35%" + "\n" + "\n"); { //user inputs annual income system.out.println("please submit annual income next year 2013-2014:"); //create scanner object keyboard input @suppresswarnings("resource") scanner keyboard = new scanner(system.in); userinput = keyboard.nextint(); if ((userinput >= 0) & (userinput<= 8500 ));{ total = bracketone * keyboard.nextdouble(); system.out.println("total taxation owed $" + total + "\n" + "\n" + "thank using taxation calculator."); } if ((userinput >=8500) & (userinput <= 34500));{ total = brackettwo * keyboard.nextdouble(); system.out.println("total taxation owed $" + total + "\n" + "\n" + "thank using taxation calculator."); } if ((userinput >=34500) & (userinput <= 83600));{ total = bracketthree * keyboard.nextdouble(); system.out.println("total taxation owed $" + total + "\n" + "\n" + "thank using taxation calculator."); system.exit(0); } if ((userinput >=83600) & (userinput <= 174400));{ total = bracketfour * keyboard.nextdouble(); system.out.println("total taxation owed $" + total + "\n" + "\n" + "thank using taxation calculator."); } if ((userinput >=174400) & (userinput <= 379150));{ total = bracketfive * keyboard.nextdouble(); system.out.println("total taxation owed $" + total + "\n" + "\n" + "thank using taxation calculator."); } if (userinput >=379150);{ total = bracketsix * keyboard.nextdouble(); system.out.println("total taxation owed $" + total + "\n" + "\n" + "thank using taxation calculator."); system.exit(0); } } } }

you have few problems.

(1) reading input 1 time userinput = keyboard.nextint(); , 1 time again keyboard.nextdouble(). think want re-use result first call, instead of requiring more input. alter every occurrence of keyboard.nextdouble() userinput, variable have stored initial input.

(2) have ; characters after each if statement - ones between ) , {. remove them.

(3) problems rounding errors. it's improve utilize bigdecimal rather double financial calculations, if want exact answers. i'm not sure whether or not you've learnt bigdecimal in course.

(4) there few values occur in more 1 of if statements. need bit more careful difference between < , <=, , between > , >=.

(5) won't create difference in case, it's improve write && instead of & when mean "and". means computer won't bother calculating right hand side in case left hand side comes out false.

java oop

No comments:

Post a Comment