Help with some basic Java

Place to talk about all that new hardware and decaying software you have.

Moderator: General Mods

Annihilated
Rookie
Posts: 47
Joined: Thu Aug 26, 2004 4:50 am
Location: Georgia
Contact:

Help with some basic Java

Post by Annihilated »

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.

Code: Select all

     if (earnings > 10300 && married == 'n' || married == 'N')		\\Calculate earnings for unmarried status.
        {     
           if (earnings >= 10300 && earnings < 33960)
           {
           tax = (earnings + 765.00) * (0.15) - (withheld);
           System.out.println("Your payable tax is " + tax);
           }
           else if (earnings >= 33960 && earnings < 79725)
           {
           tax = (earnings + 4314.00) * (0.25) - (withheld);
           System.out.println("Your payable tax is " + tax);
           }
           else if (earnings >=79725 && earnings < 166500)
           {
           tax = (earnings + 15755.25) * (0.28) - (withheld);
           System.out.println("Your payable tax is " + tax);
           }
           else if (earnings >= 166500 && earnings < 359650)
           {
           tax = (earnings + 40052.25) * (0.33) - (withheld);
           System.out.println("Your payable tax is " + tax);
           }
           else if (earnings >= 359650)
           {
           tax = (earnings + 103791.75) * (0.35) - (withheld);
           System.out.println("Your payable tax is " + tax);
           }
           
        }
So basically, is it necessary to repeat that "System.out.println" line each time?
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

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.
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
Starman Ghost
Trooper
Posts: 535
Joined: Wed Jul 28, 2004 3:26 am

Post by Starman Ghost »

How about

Code: Select all

if (earnings > 10300 && married == 'n' || married == 'N')      \\Calculate earnings for unmarried status.
        {     
           if (earnings >= 10300 && earnings < 33960)
           {
           tax = (earnings + 765.00) * (0.15) - (withheld);
           }
           else if (earnings >= 33960 && earnings < 79725)
           {
           tax = (earnings + 4314.00) * (0.25) - (withheld);
           }
           else if (earnings >=79725 && earnings < 166500)
           {
           tax = (earnings + 15755.25) * (0.28) - (withheld);
           }
           else if (earnings >= 166500 && earnings < 359650)
           {
           tax = (earnings + 40052.25) * (0.33) - (withheld);
           }
           else if (earnings >= 359650)
           {
           tax = (earnings + 103791.75) * (0.35) - (withheld);
           }
           
           System.out.println("Your payable tax is " + tax);
           
        }
[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]
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

Oh well, that's me overlooking the obvious...
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
Annihilated
Rookie
Posts: 47
Joined: Thu Aug 26, 2004 4:50 am
Location: Georgia
Contact:

Post by Annihilated »

I tried that, but then I get an error like

Code: Select all

Tax.java:43: variable tax might not have been initialized
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

Then, give it an initial value.

Although, that shouldn't prevent you from compiling...
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
Annihilated
Rookie
Posts: 47
Joined: Thu Aug 26, 2004 4:50 am
Location: Georgia
Contact:

Post by Annihilated »

Alright, that did it. Thanks. :D

And while we're here, do you suppose you could tell me how to make it so that Y/y/N/n are the only acceptable values for marital status?
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

you know, you can change the case of strings.

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?

- Yes, but don’t change the subject.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

For relatively minimal bulk... surely java can pull that:

Code: Select all

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.
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
Lord Alpha
Lurker
Posts: 165
Joined: Wed Jul 28, 2004 3:15 am
Location: The Land of Insanity
Contact:

Post by Lord Alpha »

Don't line comments in Java begin with // and not \\?
It is better to be silent and thought a fool then to open your mouth and remove all doubt

I am Zophar, Master of Sh*t!

[url=http://archlyn.bravejournal.com]View my blog[/url]
Annihilated
Rookie
Posts: 47
Joined: Thu Aug 26, 2004 4:50 am
Location: Georgia
Contact:

Post by Annihilated »

Yes, I've realized that now.

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?
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

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

if (x + y > (4 && 5) + y < 9)

What you want should be more like..

if ((x+y > 4) && (5 +y < 9))
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
Annihilated
Rookie
Posts: 47
Joined: Thu Aug 26, 2004 4:50 am
Location: Georgia
Contact:

Post by Annihilated »

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. :?
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Re: Help with some basic Java

Post by grinvader »

In your case...
Annihilated wrote:

Code: Select all

     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.
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
whicker
Trooper
Posts: 479
Joined: Sat Nov 27, 2004 4:33 am

Post by whicker »

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.
odditude
Official tech support dood
Posts: 2122
Joined: Wed Jan 25, 2006 7:57 am

Re: Help with some basic Java

Post by odditude »

Code: Select all

     if ((earnings >= 10300) && (toUpperCase(married) == 'N'))		//Calculate earnings for unmarried status.
        {     
           if (earnings < 33960)
           {
              tax = (earnings + 765.00) * (0.15) - (withheld);
           }
           else if (earnings < 79725)
           {
              tax = (earnings + 4314.00) * (0.25) - (withheld);
           }
           else if (earnings < 166500)
           {
              tax = (earnings + 15755.25) * (0.28) - (withheld);
           }
           else if (earnings < 359650)
           {
              tax = (earnings + 40052.25) * (0.33) - (withheld);
           }
           else
           {
              tax = (earnings + 103791.75) * (0.35) - (withheld);
           }
           System.out.println("Your payable tax is " + tax);
        }
Why yes, my shift key *IS* broken.
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

All right. Next, read up on arrays.
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
creaothceann
Seen it all
Posts: 2302
Joined: Mon Jan 03, 2005 5:04 pm
Location: Germany
Contact:

Post by creaothceann »

And trees.

Red, black, red-black...
vSNES | Delphi 10 BPLs
bsnes launcher with recent files list
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

It's stendhal all over again !
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
odditude
Official tech support dood
Posts: 2122
Joined: Wed Jan 25, 2006 7:57 am

Post by odditude »

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.
Why yes, my shift key *IS* broken.
Annihilated
Rookie
Posts: 47
Joined: Thu Aug 26, 2004 4:50 am
Location: Georgia
Contact:

Post by Annihilated »

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.
Deathlike2
ZSNES Developer
ZSNES Developer
Posts: 6747
Joined: Tue Dec 28, 2004 6:47 am

Post by Deathlike2 »

You're spelling his name wrong.

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.
Continuing [url=http://slickproductions.org/forum/index.php?board=13.0]FF4[/url] Research...
grinvader
ZSNES Shake Shake Prinny
Posts: 5632
Joined: Wed Jul 28, 2004 4:15 pm
Location: PAL50, dood !

Post by grinvader »

string toupper(char) ? lovely

remind me never to touch java with any kind of appendage I wish to keep, sticks notwithstanding
皆黙って俺について来い!!

Code: Select all

<jmr> bsnes has the most accurate wiki page but it takes forever to load (or something)
Pantheon: Gideon Zhi | CaitSith2 | Nach | kode54
odditude
Official tech support dood
Posts: 2122
Joined: Wed Jan 25, 2006 7:57 am

Post by odditude »

guess i'm remembering wrong, but i thought it was char toUpperCase(char) from the base class. again, i'm rusty.
Why yes, my shift key *IS* broken.
funkyass
"God"
Posts: 1128
Joined: Tue Jul 27, 2004 11:24 pm

Post by funkyass »

Strings are classes. well everything is techincally a class in java.

so, its marriage.toUpperCase()

more specifically: marriage.toUpperCase().charAt(0)

or, even saner: (marriage.equalsIgnoreCase("N"))

bookmark this: http://java.sun.com/javase/reference/api.jsp
Does [Kevin] Smith masturbate with steel wool too?

- Yes, but don’t change the subject.
Post Reply