PDA

View Full Version : Calculate federal taxes program! Help!


Ocmjt
03-10-2010, 06:08 AM
Hello everyone! My names Mary and I'm very new to java and computer programming in general, taking my first class at school right now. We have been asked to write a java program that calculates federal taxes. It has to calculate the appropriate tax for each amount and keep a total of all the taxes. It has to display the tax for each income then show the sum at the end. I have to use a -1 number to tell the program to end.

I think that I could do this if the taxrate was one number but the tricky part for me but probably the easy part for you to understand is that a person pays 10% for the first 15k..if it is over 15k they pay 10% for the first 15k and 15% for the next 45k. If it is over 60k they pay what I just said and 33% for the income over 33%.

Can someone please help me figure this out! I have no idea how to incorporate that last part so if someone could show me how to do this program I would greatly appreciate it! And please explain a little bit what your doing so I can learn from it...I know I have to use a while loop to end the program with a negative number which I think I know how to do. But I also know I have to use a if statement and constants which I do not understand how to do. Thankyouuuu:)

Ocmjt
03-10-2010, 08:19 AM
This is what I have so far..I think I am close someone please help? I have errors under income and tax and it says I should make them say =0 next to the double but I dont think thats right?


public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
final double LOWTAXRATE = .1;
final double MEDTAXRATE = .15;
final double HIGHTAXRATE = .33;
double sumOfTax=0;
double income;
double tax;
System.out.println("Please input the income, type -1 to end");

while(income>=0) {
sumOfTax = sumOfTax + tax;
System.out.print("Please input the income, type -1 to end");
income = keyboard.nextDouble();
tax = keyboard.nextDouble();

if(income<=15000)
tax=income*LOWTAXRATE;
else if(income>60000)
tax=8250+(income-60000*HIGHTAXRATE);
else
tax=1500+(income-15000*MEDTAXRATE);

}
tax = keyboard.nextDouble();
System.out.printf("The tax is %.2f%%", tax);
System.out.printf("The sumOfTax is %.2f%%", sumOfTax);




}

}

Fou-Lu
03-10-2010, 08:12 PM
The errors are because you have not initialized the tax and income variables. The income will be implicitly initialized when you call the nextDouble(), but the tax is previously used with sumOfTax without being initialized. I'd also add a 0 to all of the final variables you have set; I don't believe that Java will complain between the 0.1 and .1, but to me its easier to read as 0.1 anyway. If you're class has covered static variables, I would move those out of the main method and promote them to class members.

I don't think I'd do calculations quite as you are either. The problem is you're hard coding the values for the maximum numbers; I'd create another set of constants to define what each of the tax brackets are. I would also create a new variable for tracking the differences between them instead of calculating that between the two. Also, I don't see why the tax is being provided by the user; twice you're requesting it from the user, but that will result in the first being overwritten by the calculations, and the calculations being overwritten by the second. Remove both lines of tax = keyboard.nextDouble();. I also assumed that since this is a school project that you have not covered error trapping on these, so I will only point out that you're program will seriously suffer if you enter invalid data to use (but if its not yet covered by you're class then everyone else is in the same boat so no worries).

Ocmjt
03-10-2010, 09:50 PM
Thank you for your help Fou-Lou, when I deleted the tax = keyboard.nextDouble(); java said I had to make the double tax; double tax=0;. Is this correct? I initialized income by doing double income = keyboard.nextDouble(); and now I dont have any errors but when it runs the question doesnt even pop up. Can you or someone else help me a little more and tell me what else I am doing wrong please? Also I am confused on what you said about another set of constants, could you explain that a little more? Thank you so much!

Ocmjt
03-10-2010, 09:53 PM
Oh this is what it looks like now..it has no errors but nothing comes up when it runs.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
final double LOWTAXRATE = 0.1;
final double MEDTAXRATE = 0.15;
final double HIGHTAXRATE = 0.33;
double sumOfTax=0;
double income = keyboard.nextDouble();
double tax=0;


while(income>=0) {
sumOfTax = sumOfTax + tax;
System.out.print("Please input the income, type -1 to end");
income = keyboard.nextDouble();


if(income<=15000)
tax=income*LOWTAXRATE;
else if(income>60000)
tax=8250+(income-60000*HIGHTAXRATE);
else if(income<=60000&&income>15000)
tax=1500+(income-15000*MEDTAXRATE);

}

System.out.printf("The tax is %.2f%%", tax);
System.out.printf("The sumOfTax is %.2f%%", sumOfTax);




}

}

Ocmjt
03-11-2010, 03:41 AM
Ok I have messed with the calculations and they work correctly now and it now tells me the tax and the sum correctly. I only have two more questions to make it perfect if someone would be kind enough to help me :) When it says the tax it says it like this The tax is 102413.85%..how do I get that percent sign out of there and put a dollar sign in the front?

The second questions about my only problem. When it asks for the input the first time it always says the tax is 0 for nomatter what I put in but then works correctly after the first input. For example

Please input the income, type -1 to end
3453
The tax is 0.00% Please input the income, type -1 to end
234234
The tax is 65747.22% Please input the income, type -1 to end
2345
The tax is 234.50% Please input the income, type -1 to end

Why does it do that and how can I fix this? If there are any more problems with my program PLEASE let me know I have worked all day on it and have to turn it in tmrw . Thank you!!


Please input the income, type -1 to end
3453
The tax is 0.00% Please input the income, type -1 to end
234234
The tax is 65747.22% Please input the income, type -1 to end
2345
The tax is 234.50% Please input the income, type -1 to end


public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
final double LOWTAXRATE = 0.1;
final double MEDTAXRATE = 0.15;
final double HIGHTAXRATE = 0.33;
double sumOfTax=0;
System.out.println("Please input the income, type -1 to end");
double income = keyboard.nextDouble();
double tax=0;


while(income>=0) {
sumOfTax = sumOfTax + tax;
tax = tax + 0;
System.out.printf("The tax is %.2f%% ", tax);
System.out.println("Please input the income, type -1 to end");
income = keyboard.nextDouble();



if(income<=15000)
tax=income*LOWTAXRATE;
else if(income>60000)
tax=8250+(income-60000)*HIGHTAXRATE;
else if(income<=60000&&income>15000)
tax=1500+(income-15000)*MEDTAXRATE;
}



System.out.printf("The sumOfTax is %.2f%%", sumOfTax);




}

}

Old Pedant
03-11-2010, 06:45 AM
Your format is bad.

You want something likeSystem.out.printf("The tax is $%.2f ", tax);

And you are calculating the tax on -1 when the user finally puts in -1. Surely you don't want to do that.

It's all part of the same problem you noted: You get a tax of 0 the first time.

Simply because you are indeed outputting the value of tax before you calculate it!

You aren't thinking linear enough. Operations happen in the order you code them. Period.

Oh, heck.

Pseudo-code:

get income
while ( income > 0 ) {
calculate tax
show tax
get income
}

Ocmjt
03-11-2010, 07:20 AM
Thank you for your help Oldpedant! Does this look completely right to you?

import java.util.Scanner;
public class IncomeTax {

/**
* This program calculates federal taxes on a list containing various incomes
* Author: Marco Tomasello
* Date: March 8, 2010
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
final double LOWTAXRATE = 0.1;
final double MEDTAXRATE = 0.15;
final double HIGHTAXRATE = 0.33;
double sumOfTax=0;
System.out.println("Please input the income, type -1 to end");
double income = keyboard.nextDouble();
double tax=0;


while(income>=0) {




if(income<=15000)
tax=income*LOWTAXRATE;
else if(income>60000)
tax=8250+(income-60000)*HIGHTAXRATE;
else if(income<=60000&&income>15000)
tax=1500+(income-15000)*MEDTAXRATE;



sumOfTax = sumOfTax + tax;
System.out.printf("The tax is $%.2f ", tax);
System.out.println("Please input the income, type -1 to end");
income = keyboard.nextDouble();
}
System.out.printf("The total tax is $%.2f ", sumOfTax);


}

}

Fou-Lu
03-11-2010, 02:33 PM
Yes, that looks good; however, you can improve it slightly by using a do/while loop instead of a while. SumOfTax and tax should be initialized to 0.0 since they are doubles; but an integer will implicitly cast as a double in Java so it isn't really necessary to do so.
Using a do/while would satisfy you're conditions without needing to use two input requests. You must forgive our use of psuedo code, but since this is a school project we cannot give you full code; thats up to you to develop (this much maybe too much as is, but you've done most by yourself so I'm not too concerned).

declare tax, sumOfTax, income := 0.0
do
if income > 0 // First iteration will skip this block.
tax := call:calculateTax // Can be a method or directly calculated
sumOfTax := sumOfTax + tax
call:showTax
endif
income := STDIN // Request the income from input
STDIN := FLUSH // Flush the buffer
while income >= 0 // if income is >= 0, we keep going
call:showSumOfTax


I'm still not a big fan of the calculations because of the 'magic numbers' that are in use. I would personally adapt that to use variables / constants instead. Also, in Java you can 'shortcut' you're variable [in/de]crements by using sumOfTax += tax. It essentially just says 'take the sumoftax and add tax to it'.


Y'know, it doesn't really make a difference if you use a do/while or while in this example since the calculations do not actually have to occur. Instead, I'd embed an if in either the do/while or the while and always ask the user for input as the last action for the loop.