Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: program compiles & executes, but the numbers ain't right :O

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Location
    San Francisco, CA
    Posts
    10
    My Mood
    Yeehaw
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Unhappy program compiles & executes, but the numbers ain't right :O

    NOTE: Totally should have posted this in the "WHAT'S WRONG WITH MY CODE" thread, huh? Derp. >.< Don't see any way to move it, but I will pay more attention to where I'm posting next time.

    Hey y'all, thanks for reading. I need to get this darn homework done within the next, ummm, 20ish hours, and I've hit a real snag.

    Okay, so. I have done the following things:

    1. Read the bejesus out of the chapter we're working on right now for the class, then re-read the sections that apply to if and if-else statements.
    2. Left the problem for an entire day and come back to it, because god knows I wasn't exactly "at my best" by the end of the first frustrating session of slogging away and getting error messages.
    3. Tried to focus on each individual block of code and make sure each part is correct.
    4. Gone back and done all that stuff again.

    I'm still having a problem. As far as I can tell, the code isn't "wrong" (and it DOES compile, and it DOES execute)...but I'm not getting the proper result.

    I had a couple other people check my math, and they say it's right (mind you, we're all liberal arts kids, so if someone here finds a problem in my math at this point...I wouldn't be entirely surprised).

    Here's the problem assigned by my instructor:

    Print a random income (different at each execution) between $0 and $200,000, how much income tax is owed on it, and what the effective tax rate is. The base rate of income tax is 26%, with two exceptions: the first $20,000 of income is tax-free, and incomes of $100,000 and over are subject to an additional $10,000 special assessment. Examples: On $120,000 income, tax is $36,000, for an effective tax rate of 30%. On $12,000 income, tax is $0, for an effective rate of 0%.

    // A program to do a bunch of math stuff (ahh lord make it stop).
     
    public class Homework3
    {
    	public static void main(String[] args)
    	{
     
    // Randomly generate an "income" between $0 and $200,000.  NOTE: Not entirely sure I did this part correctly, though I'm feeling hopeful.  
     
    	int income = (int)(Math.random() * ((200000 - 0) + 1);
     
    /* If income is $20,000 or less, no tax is applied.  Effective tax rate is 0%. */
     
    	if (income <= 20000)
    	{
    		int tax = 0;
    		System.out.println("If your income is " +
    				income + ", you owe $" + tax +
    				" income tax for an effective tax " +
    				"rate of 0%.");
    	}
     
    /* If income is greater than $20,000 and less than $100,000, a 26% tax is applied. 
     
    Effective tax rate is 26%. */
     
    	else if (income > 20000 && income < 100000)
    	{
    		int tax = (int)(income * .26);
    		System.out.println("If your income is " +
    				income + ", you owe $" + tax +
    				" income tax for an effective tax " +
    				"rate of 26%.");
             }
     
    /* If income is $100,000 or more, a 26% income tax is applied along with a $10,000 
     
    special assessment.  Effective tax rate is the tax amount divided by the original 
     
    income. NOTE: This is the math bit I'm not sure of; I need to take a basic math class 
     
    again, I suck at it pretty horrifically. */
     
     
    	else if (income >= 100000)
    	{
    		int adjustedIncome = (income - 20000);
    		int tax = (int)(adjustedIncome * .26 
    			+ 10000);
    	int effectiveTaxRate = (tax / income);
    	System.out.println("If your income is " +
    			income + ", you owe " + tax +
    			" in come taxes for an " +
    			"effective tax rate of " +
    			effectiveTaxRate + "%.");
    	}
     
    /*If the program comes up with a number that doesn't fall into any of these ranges, you 
     
    obviously broke it. Way to go. */
     
    	else
    	{
    		System.out.println("There has " +
    				"been an error, probably " +
    				"somehow your fault. Jerk.");
    	}
     
     
    	}
    }

    My problem is that the program SEEMS to be working...but then when it kicks out a number in the third category (>= 100,000), it gives a tax amount that would be appropriate for an income <= 20,000, and says the effective tax rate is 0.

    So it seems to me that for some reason my program is ONLY using the first block of code, regardless of how big the number is. But I can't figure out WHY. >.< The only reason I specifically know of for that happening is when you put a semicolon at the end of the if statement, but I haven't done that.

    Can anyone point me toward where the problem might be? I'm sure it's simple and probably obvious to anyone who's been doing this longer, but I still can't see it.

    Thanks again for reading,
    crys
    Last edited by crys; September 9th, 2012 at 07:41 AM. Reason: realized my post was in the wrong place


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: program compiles & executes, but the numbers ain't right :O

    Hello crys

    Quote Originally Posted by crys View Post
    So it seems to me that for some reason my program is ONLY using the first block of code, regardless of how big the number is.
    You should always do some basic debug to your code. In this case, try printing a descriptive message in every block of code to see which one is executed.

    I tested your code and is working fine for me for every possible income. For example an income of 250000 gives me a tax of 69800 (i.e. [(250000-20000)*.26] + 10000).
    The only problem I can see in your code is effectiveTaxRate. A tax rate is always a percentage of income. Therefore it can't be represented by an int. A double is more appropriate in this case.
    In addition you are doing integer division to find the tax rate. But in java integer division gives you always an integer - it only keeps the integer part and ignores the fractional part. For example, 9/2 = 4 and 1/2 = 0.

    To avoid this you can cast either tax or income to double.

    Hope this helps.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    crys (September 9th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Location
    San Francisco, CA
    Posts
    10
    My Mood
    Yeehaw
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: program compiles & executes, but the numbers ain't right :O

    Hi, thanks for responding!

    As per your suggestion, I put a message in each block of code to see which ones were being executed...and it does look like ALL THREE are working, which helps me narrow down potential problems.

    Unfortunately, for numbers >= 100,000, the program still keeps telling me that the effective tax rate is "0%" (well, "0.0%" now that I changed int to double, hehe), which clearly is not accurate. So I'm still working on that part.

    Thanks again -- ESPECIALLY for the bit about adding messages to each block of code, such a simple/easy but incredibly useful thing to do -- seems obvious in retrospect and will clearly be helpful from now on, but I never thought of it!

    crys

  5. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: program compiles & executes, but the numbers ain't right :O

    Quote Originally Posted by crys View Post
    Unfortunately, for numbers >= 100,000, the program still keeps telling me that the effective tax rate is "0%" (well, "0.0%" now that I changed int to double, hehe), which clearly is not accurate. So I'm still working on that part.
    You are close. Just look the last suggestion in my previous post; "To avoid this you can cast either tax or income to double".

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Location
    San Francisco, CA
    Posts
    10
    My Mood
    Yeehaw
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: program compiles & executes, but the numbers ain't right :O

    OHHH! Derp. Thanks Andreas, you're totally right, it was all related to the double/int issue you pointed out...and the fact that I apparently was expecting my program to magically convert decimals into percentages. >.>

    Of course my percentages were coming out in decimal form, as I told them to...so they were all .25 or .45 or what-have-you...and, of course, forcing them to be ints meant they all just came out as ....0, so numbers >= 100,000 (where I am using the equation to get the tax %, in decimal form) came out as having a 0% effective tax rate. Ha!

    I feel like an idiot, BUT I'm also really excited that this problem is solved, so I guess it balances out! Thanks again! Problem solved, homework may proceed as planned. /happydance

  7. #6
    Junior Member
    Join Date
    Sep 2012
    Location
    San Francisco, CA
    Posts
    10
    My Mood
    Yeehaw
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: program compiles & executes, but the numbers ain't right :O

    Ha! We cross-posted, hehe -- took me a few minutes, but I realized you'd given me the key to the whole deal. Thanks again!!

  8. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: program compiles & executes, but the numbers ain't right :O

    Glad you got it working. Remember to mark the thread as solved (from the "Thread Tools" in top of the thread).

  9. #8
    Junior Member
    Join Date
    Sep 2012
    Location
    San Francisco, CA
    Posts
    10
    My Mood
    Yeehaw
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: program compiles & executes, but the numbers ain't right :O

    Looks like I don't have the ability to mark the thread "solved." I checked in the Thread Tools, but I only have the options to show a printable version, e-mail it, subscribe to it, or add a poll. Is it possible that junior members can't mark threads "solved"?

  10. #9
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: program compiles & executes, but the numbers ain't right :O

    Quote Originally Posted by crys View Post
    Looks like I don't have the ability to mark the thread "solved." I checked in the Thread Tools, but I only have the options to show a printable version, e-mail it, subscribe to it, or add a poll. Is it possible that junior members can't mark threads "solved"?
    That' really odd. There should be a Mark this thread as solved... option. It has nothing to do with the fact that you are a junior member -as far as I know. You can report it to a moderator.

  11. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: program compiles & executes, but the numbers ain't right :O

    The 'mark thread as solved' was not an option given this was originally posted within the Cafe. I have since moved your post, and the option should now be there

  12. The Following User Says Thank You to copeg For This Useful Post:

    crys (September 9th, 2012)

  13. #11
    Junior Member
    Join Date
    Sep 2012
    Location
    San Francisco, CA
    Posts
    10
    My Mood
    Yeehaw
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: program compiles & executes, but the numbers ain't right :O

    Aha, thanks.

Similar Threads

  1. Program that Opens/Executes a File on Windows
    By davidvee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 5th, 2012, 03:32 PM
  2. Replies: 6
    Last Post: November 25th, 2011, 03:58 PM
  3. Need help with an Octal Numbers program
    By anrowl01 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 23rd, 2011, 03:32 AM
  4. Ant Executes and Stops After a Certain Task
    By DanielPros in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 9th, 2011, 10:46 AM
  5. Replies: 5
    Last Post: July 7th, 2011, 09:22 AM