• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variables and Calculations

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.

As a novice, I would consider Java to be fun but it requires considerable amounts of reading.

I have been reading my book and when it came to programming this week's assignment, I am stumped.

The assignment is rather simple, but after reading through it for three days, I cannot figure out it.

This is what the program looks like:

__________________
Enter Gross Income [Textfield]

Amount of Tax [Textfield]

Net Income [Textfield]


Enter Clear Close
__________________

If I enter an income of 60k in the first textfield, I am supposed to get "$12000.00" in the Amount of Tax textfield and "$48000.00" in the Net Income.


I seek your help. If you are interested in seeing the little confusing code I have, I will upload it.


Thank You,
Kalia



 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

It appears that you should first calculate 20% tax, and then calculate net income by subtracting tax from gross income.

What exactly are you having problems with?
 
Khal Laid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Welcome to CodeRanch!

It appears that you should first calculate 20% tax, and then calculate net income by subtracting tax from gross income.

What exactly are you having problems with?



Right now I seem to be having a problem dividing



In the itemTax, I want it to divide the input ("itemQuantity") by the tax rate (20 %)

Everytime I place a decimal with the tax rate, I get an error for lossy conversion from a double to a float.

And quite frankly, I don't fully understand what that means.


Thank you for your response
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd only get that error if you were trying to assign a double value to a float variable. I'm assuming itemTax is a float. Why? Why is it not double?

When you perform mathematical operations in Java, the result of the operation is int for integer operations (unless a long is involved, in which case the result is long) or double for floating point operations.

Since .20 is of type double, the result of the division has type double.

You have an unrelated problem as well. Usually you don't perform division in combination with percentages.
 
Khal Laid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification.

I think I know the difference now between the int, double, and float functions.

I fixed it now.

My last issue is programming the "Close" button. All I want it to do is to close the program and I am currently scouring the net for help. If you could, I'd appreciate it.

Thanks
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can terminate a JVM instance using System.exit()
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khal Laid wrote:In the itemTax, I want it to divide the input ("itemQuantity") by the tax rate (20 %)


Are you sure that that is what you want to do? Do you realize that dividing by 0.20 is the same as multiplying by 5?

Probably you want to multiply by 0.20, not divide.
 
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khal Laid wrote:



Hi Khal Laid,
Just a suggestion, I wouldn't call the value less tax a "total cost"? Probably some better naming choices available.
cheers,
Daniel
 
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Probably you want to multiply by 0.20, not divide.


That is exactly what i was thinking. Why the division? Dividing itemQuantity by .20 means multiplying itemQuantity by 100 and then divide the result by 20.
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic