任务要求:
Temperature Class (Temperature.java)
• One field, a double, named fTemp. • One constructor that accepts one argument.
o The constructor should assign the value of this argument to fTemp. • A method named toCelsius that returns the temperature (value of fTemp) in Celsius.
F to C conversion: (fTemp - 32) * (5/9)
• A method named toKelvin that returns the temperature (value of fTemp) in Kelvin.
F to K conversion: (fTemp + 459.67) * (5/9)
• Neither the toCelsius or toKelvin methods should alter the value of the fTemp field.
TemperatureDemo Class (TemperatureDemo.java)
This is the class that will demo a Temperature object; thus, it contains the main method.
In the main method:
• Prompt the user to enter a temperature in Fahrenheit.
• Construct an instance of your Temperature object
o The user’s input value is passed as the argument to the constructor
• Using the Temperature object’s toCelsius, and toKelvin methods, print:
o The temperature in Celsius.
o The temperature in Kelvin.
o All output should be rounded to 2 decimal places.
Be sure to use comments to document your code.
Sample Input/Output
Please enter the Fahrenheit temperature: 98.6
Celsius Temperature: 37.00 Kelvin Temperature: 310.15
记录试错过程:
/**
*This program can convert a user's input for Fahrenheit temperature to Celsius and Kelvin.
*/
import java.text.DecimalFormat;
import java.util.Scanner;
class Temperature {
private static double fTemp;
//private static
//double fTemp;
//Temperature(double fTemp){
// tem = fTemp;
public Temperature(double t) {
fTemp = t;
}
//Create one field
//public static void main(String[] args) {
//double fTemp;
//Instantiate a Temperature object
//Temperature demo = new Temperature();
//}
//A method named toCelsius that returns the temperature (value of fTemp) in Celsius
public static double toCelsius(){
//Scanner show1 = new Scanner(System.in);
//System.out.println("Please enter the Fahrenheit temperature: ");
//double fTemp = Double.parseDouble(show1.nextLine());
double result = (fTemp - 32) * (5.0/9.0);
return result;
}
//A method named toKelvin that returns the temperature (value of fTemp) in Kelvin
public static double toKelvin(){
//Scanner show2 = new Scanner(System.in);
//System.out.println("Please enter the Fahrenheit temperature: ");
//double fTemp = Double.parseDouble(show2.nextLine());
double result = (fTemp + 459.67) * (5.0/9.0);
return result;
}
}
class TemperatureDemo {
public static void main(String[] args) {
//Prompt the user to enter a temperature in Fahrenheit.
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the Fahrenheit temperature: ");
double fTemp = Double.parseDouble(keyboard.nextLine());
//Instantiate a Temperature object
Temperature demo = new Temperature(fTemp);
//Convert input temperature to be rounded to 2 decimal places.
DecimalFormat df = new DecimalFormat("#.00");
System.out.print("Celsius Temperature: ");
//Print the temperature in Celsius using the toCelsius method of Temperature object
System.out.println(df. format(Temperature.toCelsius()));
System.out.print("Kelvin Temperature: ");
//Print the temperature in Kelvin using the toKelvin method of Temperature object
System.out.println(df. format(Temperature.toKelvin()));
}
}
P.S. 被“//”的绝大部分为“待用”部分——意为之前尝试过可行,但正在寻找更好方式的足迹。
可以看出,在class Temperature { private static double fTemp; }
中所花费大量时间的尝试。目前还有一个对于此处的疑问:为什么语句是这样,而不是直接的double fTemp
呢?(因为这一class的其它method引用不了。可是为什么呢?)(待今后的我来回答~)
重大发现:
当我把
Temperature demo = new Temperature(fTemp);
注释掉的时候,输出结果为:
Please enter the Fahrenheit temperature:
98.6
Celsius Temperature: -17.78
Kelvin Temperature: 255.37
数据不对,原因未知。(待未来的我回答)
终稿:
/**
*This program can convert a user's input for Fahrenheit temperature to Celsius and Kelvin.
*/
import java.text.DecimalFormat;
import java.util.Scanner;
class Temperature {
private static double fTemp;
public Temperature(double t) {
fTemp = t;
}
//A method named toCelsius that returns the temperature (value of fTemp) in Celsius
public static double toCelsius(){
double result = (fTemp - 32) * (5.0/9.0);
return result;
}
//A method named toKelvin that returns the temperature (value of fTemp) in Kelvin
public static double toKelvin(){
double result = (fTemp + 459.67) * (5.0/9.0);
return result;
}
}
class TemperatureDemo {
public static void main(String[] args) {
//Prompt the user to enter a temperature in Fahrenheit.
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the Fahrenheit temperature: ");
double fTemp = Double.parseDouble(keyboard.nextLine());
//Instantiate a Temperature object
Temperature demo = new Temperature(fTemp);
//Convert input temperature to be rounded to 2 decimal places.
DecimalFormat df = new DecimalFormat("#.00");
System.out.print("Celsius Temperature: ");
//Print the temperature in Celsius using the toCelsius method of Temperature object
System.out.println(df. format(Temperature.toCelsius()));
System.out.print("Kelvin Temperature: ");
//Print the temperature in Kelvin using the toKelvin method of Temperature object
System.out.println(df. format(Temperature.toKelvin()));
}
}
撒花~~~