# Celsius to Fahrenheit

• 09-30-2010, 04:35 AM
Danieldcc
Celsius to Fahrenheit
so my first assignment is to convert celsius to fahrenheit and I think that I got eveything right, except my last taks is to

Write a main method for the previous class Temperature that creates an object of that type and then calls its methods, outputting the result on the console.

I did that but the input is already set to 100 and just outputs the values in the console window. Is there a really quick way just to ask the user for an input in fahrenheit and output in console window in both fahrenheit and celsius and vice versa???

public class Temperature
//Converts between Celsius and Fahrenheit and outputs the conversion
{
private double celsius;
private double fahrenheit;
//converts from celsius to fahrenheit and stores in variables
public void setCelsius(double temp){
fahrenheit = 9.0/5.0*temp + 32;
celsius = temp;
}
//converts from fahrenheit to celsius and stores in variable
public void setFahrenheit(double temp){
celsius = (temp-32) * 5.0/9.0;
fahrenheit = temp;
}
//prints the temperature in celsius in the console
public void outputCelsius(){
System.out.println("The temperature is " + celsius + "C");
}
//prints the temperature in fahrenheit in the console
public void outputFahrenheit(){
System.out.println("The temperature is " + fahrenheit + "F");
}
//Creating an object that will output the temperature on the cosnole window
public static void main(String[] args) {
Temperature t = new Temperature();
t.setCelsius(100);
t.outputFahrenheit();
t.outputCelsius();
}
}
• 09-30-2010, 04:44 AM
jlmp
For input, check the the class Scanner and for output you alread have System.out to show your results on your console !
• 09-30-2010, 04:50 AM
Danieldcc
Thanks for the info. I heard about the Scanner but we didn't learn that in class so I'm assuming I don't need to do it.