This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Okay, I am lost about a class file. I need to write a program that uses two Class files. So, the class file is seperate from my .java file then? Any links to this concept would be appreciated... The school book and course module seem to just confuse me even more...
also, you can have multiple classes in the same .java file (although only one can be a public class.
if i remember right, EACH class will have it's own .class file created when you compile your source. So it's possible to have one .java file, but end up with many .class files.
Never ascribe to malice that which can be adequately explained by stupidity.
Pete Tyo
Ranch Hand
Joined: May 11, 2005
Posts: 38
posted
0
Okay below is a class file I wrote. Is this correct? I tried to compile it and get errors that I will list below the code..
Here are the compiler errors I am getting... Gifts.java:7 possible loss of precision found ouble required: float totalCollected =0.0; ^ Gifts.java:17 Possible loos of precision found: double required: float totalCollected = totalCollected + (amount*1.24); ^
Gifts.java:21 Possible loss of precision found: double required: float totalCollected = totalCollected + (amount*0.0092); ^
When you write a numeric constant with a decimal point, it's a "double," a 64-bit variable. There are also 32-bit variables called "float", which you're using here. Mixing and matching them causes this sort of error. You can either write the constants as float by putting an "f" at the end -- i.e., 0.0f instead of just 0.9 -- or you can change the variables to doubles instead of floats. It doesn't matter which, really -- you just need to be consistent.
When you write a numeric constant with a decimal point, it's a "double," a Where totalCollected = 0.0; this is not intended to be a numeric constant that I know of. I don't understand how I have double's and floats here when I only declare a double now.. there is a new compiler error below the code.
Okay I really don't understand what is going on with the float / double issue here...
Originally posted by Pete Tyo: When you write a numeric constant with a decimal point, it's a "double," a Where totalCollected = 0.0; this is not intended to be a numeric constant that I know of. I don't understand how I have double's and floats here when I only declare a double now.. there is a new compiler error below the code.
Okay I really don't understand what is going on with the float / double issue here...
You declare totalCollected variable as double whereas the return type of the method totalSavings is float . Thats why you are getting the error.
Change the return type of the method as double. It will work
When the compiler gives errors the code is by definition (for all practical purposes) incorrect
"When you write a numeric constant with a decimal point, it's a "double," a Where totalCollected = 0.0; this is not intended to be a numeric constant that I know of. "
0.0 is a constant double, totalCollected is assigned the value of that constant double (so 0.0). If totalCollected is of a datatype that cannot be implicitly cast to double (like float, which is too small to hold the maximum data a double can hold) you will get an error telling you so.
Use 0.0f instead to force it to float, or (and more usually done) forget about float and use double always.
42
Pete Tyo
Ranch Hand
Joined: May 11, 2005
Posts: 38
posted
0
Okay, this is my first attempt at writing a class that will be used with another program I have yet to create... When you say numeric constant I think of something that will always be 0.0 and can not be changed. This i do not want. I was under impression from are book that in the constructor I had to initialize totalCollected to something. How do I write totalCollected = ? ; so that it says its a double like i define it to be? Also do I need to put the amount in the constructor that I use in calculations. The amount will be passed from the main program to the class and calculations will be done on it. I really appreciate the help. For some reason I am having a rather hard time grasping this concept...
Thanks,
Pete
Pete Tyo
Ranch Hand
Joined: May 11, 2005
Posts: 38
posted
0
Wow, I can't believe I missed out on what was going on. I figured it out.. in my last method... i was telling the compiler I was taking in a float instead of a double... Don't I feel like a dumb a@@... Thanks for all the help everyone..