• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variables and Calculations  RSS feed

 
Khal Laid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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



 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • 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
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • 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
  • 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: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can terminate a JVM instance using System.exit()
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!