So I'm taking an intro CS class and we're learning the basics of Java. Now we're writing a simple program to calculate tax. This is my first programming language, so I'm not very neat with it, but I was wondering if there was a way to take these many repeated lines out and instead put just one of them at the end. Here's part of my code.
IIRC, you should store part of the repetitive crap ("Your payable tax") into a String, and print that out via a different function that I cannot recall off the top of my head.
Or, you could make the message a function you call given the parameters.
[code]<Guo_Si> Hey, you know what sucks?
<TheXPhial> vaccuums
<Guo_Si> Hey, you know what sucks in a metaphorical sense?
<TheXPhial> black holes
<Guo_Si> Hey, you know what just isn't cool?
<TheXPhial> lava?[/code]
make sure every number is a float(or double, what ever they call it in java).
java may in some circumstances explicitly convert from and integer when its required(pretty much any operation with a decimal or division), but might as well make sure everything is on the same page.
Does [Kevin] Smith masturbate with steel wool too?
table1[] = { 0.0, 765.0, 4314.0, 15755.25, 40052.25, 103791.75 };
table2[] = { 0.0, 0.15, 0.25, 0.28, 0.33, 0.35 }
/*
&& is prioritary over ||, your previous test is flawed.
also it prevented value 10300 from being scanned,
making the >=10300 under it useless
*/
if (married == 'n' || married == 'N')
{
offset = (earnings>=10300)+(earnings>=33960)+(earnings>=79725)+
(earnings>=166500)+(earnings>=359650);
tax = (earnings + table1[offset]) * table2[offset] - (withheld);
System.out.println("Your payable tax is " + tax);
}
/*
also INDENT YOUR CODE PROPERLY for best results
aligning the {} with the code makes them mostly unnoticeable
if (stuff)
{
more_stuff();
}
if stuff
{
more_stuff();
}
see what i mean
*/
Last edited by grinvader on Thu Nov 27, 2008 9:15 pm, edited 1 time in total.
grinvader. Thanks for pointing that out, I didn't actually try the minimum values in my program. But I don't get what you mean by my test being flawed. Right now the program does exactly what it's supposed to. Any values below those two ranges (there's another one for married) will tell you the income is not available. Also that offset thing looks really useful... I don't think my book even mentions it. Like I said, it's not a java book. And which indentation style were you trying to emphasize, the first or the second one?
Your test is flawed do to how the comparisons are dealt with in the order of operations. grinvader's assessment is correct. If you want to properly correct your order, you need to apply parenthesis where applicable.
Your current logic is like this...
if (x + y > 4 && 5 + y < 9)
The program processes this differently because of the order
I got it now. I remember doing it that way before and getting an error while compiling. Then I took the parentheses out and then it worked. But now it works with the parentheses. I dunno.
if (earnings > 10300 && married == 'n' || married == 'N') \\Calculate earnings for unmarried status.
You wish to proceed if earnings is above [fault 1: actually, above or equal] 10300 AND either married is 'n' or 'N'.
However that line of code will proceed if earnings is above 10300 AND married is 'n', or married is 'N' and earnings can be whatever.
This is beyond hope at this point.
Learn boolean math, and general orders of operations, then come back.
That, or only have one comparison per if statement, and nest deeply.
This reality, of course, is contrary to your idea that you can somehow reduce the amount of lines this takes.
grinvader wrote:All right. Next, read up on arrays.
i was just demonstrating that he could simplify his original comparison logic a bit on a more beginner level.
that being said, i'm way too rusty to have come up with your more-elegant offset method. it's been at least four and a half years since i've written any code of real purpose (so ghetto vba earlier this year notwithstanding). even if my response was less useful, it's a basic thinking exercise for me, so i don't feel like my brain's completely shriveled up and died.
it's to the point where anything with pointers would probably take three tries before i cleaned up all the brain-dead pointer math and dereferencing segfaults.
ottitude, thank you, that was much simpler than what I had. But I don't understand the toUpperCase thing. I tried it in my code and got an error while compiling. I don't remember what the error was, but apparently the compiler didn't understand it.
Anyways, the syntax was the right idea, but String comparisons are not done like that.
Read up on the String class.
You can't use toUpperCase in that way. It's unclear how you are storing that input to begin with. char comparisons can be done like that, but that function can't be used like that in this instance.
In any case, you should always post what the error messages are, because it's unclear what the heck you mean.