October 2nd, 2005, 01:44 AM
|
#1 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,004
|
hey guys...I have to take a program that takes a farenheight temp and converts it to celsius. I'm having a problem at the moment where if the celsius number is supposed to be <0 then it just shows up as -0.00 on the command prompt....and I have no idea why. I've used double and init for all my values (tried both ways) and got the same results. I'm developing in the eclipse ide with java 1.5 installed. eclipse says it's compiling to java 1.4 i think (not sure why but it's the default so i left it....lol). Everything else has compiled fine, so i doubt that's the problem. any ideas? Code: public class Temperature
{
double degrees;
public Temperature(double degree)
{
degrees=degree;
}
public void getF()
{
System.out.println(degrees);
}
public void getC()
{
double degreesC = (5/9)*(degrees-32);
System.out.println(degreesC);
}
public static void main(String[] args)
{
Temperature thermometer1 = new Temperature(20);
thermometer1.getF();
thermometer1.getC();
}
} |
| |
October 2nd, 2005, 03:25 AM
|
#2 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,549
|
Have not done java in a few years, but my guess is that it is treating (5/9) as integer division and making it 0 * (Degrees - 32)
Change (5/9) to (5.0/9.0) and see if that makes a difference.
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
October 2nd, 2005, 10:51 AM
|
#3 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,004
|
i don't think so cus it works fine when i use any degrees>=32 but i'll give it a try.
/edit ... worked perfectly  lol. I guess i just got put to shame  lol. Thanks a lot man....really helped me out!
I've got another question. Do you think I should make any of my methods nonpublic? If so, which ones and what is the term i replace public with. thanks guys!  .
Last edited by sr71000 : October 2nd, 2005 at 10:56 AM.
|
| |
October 2nd, 2005, 02:00 PM
|
#4 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,004
|
ok...new question...i want to assign a name to each object (so I can create one print method that will print the line "The temperature of " + id + "is " + degrees + "degrees Farenheight" so it looks like "The temperature of thermometer1 is 20 degrees Farenheight" I thought char but char apparently only works for 1 letter  thanks for the help  . |
| |
October 2nd, 2005, 02:16 PM
|
#5 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,004
|
i figured it out..i was using string instead of String....damn caps...lol |
| |
October 2nd, 2005, 03:03 PM
|
#6 (permalink)
| | Perfetc Member
Join Date: Jan 2003 Location: Maryland Suburbia
Posts: 4,327
|
If you want another class to be able to access the methods, then you should make it public. For this scale of a program... it doesn't really matter.
When you get into bigger classes, you will often have methods that the class uses, that the client of the class doesn't need to use / to know about. So in other words, if the class itself is the only one that will use the methods, use the keyword "private" instead of "public"
Generally speaking from an object oriented aspect, your "accessor' methods should return a value rather than print it out. An accessor method is somethign that returns an instance or class variable / "member". It's not really a big deal, but if I was using that class I would be very confused to see a "getWhatever" method with a void return type. Calling it "printCelcius" makes more sense.
Last edited by VHockey86 : October 2nd, 2005 at 03:06 PM.
|
| |
October 3rd, 2005, 12:32 PM
|
#7 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,004
|
i see where you're coming from..idk why but in my mind i saw it as getting celsius for me, not for the accessor. I agree now that you mention it...i'll probably change it to print. The reason I did it that way, was so that I could write out the whole sentences once and then acess it for any number of objects...even if there were hundreds. (i eventually wanna take this project where the user can enter in any number of temps for thermometers 1 through however high they go, then when they leave everything blank, it will stop and convert everything they've entered so far. That's why i put it in a method with a variable for the name of the object, so that i could use it repeatedly. does that make sence, and if so would you have a recommendation of a better way to do it? (this is only the second program i've ever written, and as you can see..it's very simple...but i dream big!! lmao). |
| |
October 3rd, 2005, 04:21 PM
|
#8 (permalink)
| | Perfetc Member
Join Date: Jan 2003 Location: Maryland Suburbia
Posts: 4,327
|
Do you want the user to enter the temperatures from the command line? Or at runtime.
With command line arguments its pretty straightforward. Here's one simple implementation Code: /**
* Thermometer.java
*/
public class Thermometer
{
double degrees;
public Thermometer(double degree)
{
degrees=degree;
}
public double getF()
{
return degrees;
}
public double getC()
{
double degreesC = (5.0/9.0)*(degrees-32);
return degreesC;
}
public static void main(String[] args)
{
Thermometer thermometer[] = new Thermometer[args.length];
for(int i=0; i<args.length; i++) {
double temp = Double.parseDouble(args[i]);
thermometer[i] = new Thermometer(temp);
System.out.println("Thermometer #"+i+" is "+
thermometer[i].getF()+"F and "+
thermometer[i].getC()+"C");
}
}
} Runtime would be something like this Code: andrew@localhost ~ $ java Thermometer 212 32
Thermometer #0 is 212.0F and 100.0C
Thermometer #1 is 32.0F and 0.0C
Last edited by VHockey86 : October 3rd, 2005 at 04:47 PM.
|
| |
October 3rd, 2005, 08:04 PM
|
#9 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,004
|
lol...neither  i just had to show the source..that i know how to use the constructor method and declare variables and print stuff...we didn't even have to do any input...that'll be down the road for me (but i plan to make it graphical before i do anything!  lol. |
| |
October 3rd, 2005, 09:22 PM
|
#10 (permalink)
| | Perfetc Member
Join Date: Jan 2003 Location: Maryland Suburbia
Posts: 4,327
|
I was just responding to this Quote: |
(i eventually wanna take this project where the user can enter in any number of temps for thermometers 1 through however high they go, then when they leave everything blank, it will stop and convert everything they've entered so far
| |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |