aspose file tools*
The moose likes Beginning Java and the fly likes Confused on the idea of a Class file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused on the idea of a Class file." Watch "Confused on the idea of a Class file." New topic
Author

Confused on the idea of a Class file.

Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
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...

Thanks,

Pete
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
My guess is that you need to write a program that uses two classes, not two class files.

A .class file is what results when you compile a .java file.

example:
If you have a file as such.

It would be saved as HelloWorld.java

when you run 'javac HelloWorld.java' it would create a HelloWorld.class file.

Hope that helps.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
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);
^

what is going on with that?

Thanks

Pete
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

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.


[Jess in Action][AskingGoodQuestions]
Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
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...
Shalini Chandel
Ranch Hand

Joined: May 05, 2005
Posts: 115
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

Regards,
Shalini


SCJP 1.4
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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
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
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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused on the idea of a Class file.