• 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

Confused on the idea of a Class file.

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic