home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1688
Discussions: 188,402, Posts: 2,243,609, Members: 232,632
Old October 2nd, 2005, 01:44 AM   Digg it!   #1 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,004
Send a message via AIM to sr71000
java homework help

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();

	}

}

sr71000 is offline   Reply With Quote
Old October 2nd, 2005, 03:25 AM     #2 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,549
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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.

jkrohn is offline   Reply With Quote
Old October 2nd, 2005, 10:51 AM     #3 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,004
Send a message via AIM to sr71000
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.
sr71000 is offline   Reply With Quote
Old October 2nd, 2005, 02:00 PM     #4 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,004
Send a message via AIM to sr71000
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 .
sr71000 is offline   Reply With Quote
Old October 2nd, 2005, 02:16 PM     #5 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,004
Send a message via AIM to sr71000
i figured it out..i was using string instead of String....damn caps...lol
sr71000 is offline   Reply With Quote
Old October 2nd, 2005, 03:03 PM     #6 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
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.
VHockey86 is offline   Reply With Quote
Old October 3rd, 2005, 12:32 PM     #7 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,004
Send a message via AIM to sr71000
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).
sr71000 is offline   Reply With Quote
Old October 3rd, 2005, 04:21 PM     #8 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
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.
VHockey86 is offline   Reply With Quote
Old October 3rd, 2005, 08:04 PM     #9 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,004
Send a message via AIM to sr71000
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.
sr71000 is offline   Reply With Quote
Old October 3rd, 2005, 09:22 PM     #10 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
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
VHockey86 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Homework help ben-the-slacker IMO Community 8 October 2nd, 2005 10:48 AM
Do homework Bizkitkid2001 IMO Community 30 April 10th, 2005 10:55 PM
I need some Homework Help njolakoski IMO Community 15 March 30th, 2005 03:08 PM
Little homework help please? Gib IMO Community 6 January 28th, 2004 03:47 PM
Do your homework...Or else CERuppel IMO Community 35 October 22nd, 2002 09:37 PM

Most Active Discussions
Is It Just Me? (2906)
3-days in and no threads about Gaza (161)
Misery Loves Company... (2144)
New Build ( Finally ) (7)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
RCA 52Inch HDTV wont turn on (5)
Folderchat Weekday thread (444)
Recent Discussions
Futronix has water features? (0)
Laptop proccesor to desktop mob.. (2)
Please help! multiple problems! (4)
RCA 52Inch HDTV wont turn on (5)
New Build ( Finally ) (7)
Common Spyware Solutions (97)
How do you move a hard-drive to.. (4)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 04:36 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28