java - java不在2个类中打印?
问题描述
我有两个班级,当我运行演示程序时,它不会打印出另外两个“华氏到摄氏”它会打印前 3 个而不是第二组。所以程序将运行,我只是无法将所有华氏温度都打印到摄氏温度。我不知道我的代码中缺少什么以打印出其他信息。谢谢!
第1部分:
import java.util.*;
public class Thermometer//create a class temperature
{
//declare private variable to class Thermometer
private double temperature; // declare the two instance variables
private char tempType;
//*** Create FOUR constructor method****
public Thermometer() // sets default value to 'C' tempType and 0 degrees temperature
{
temperature = 0; // set to zero degrees celsius
tempType = 'C';
}
public Thermometer(char tempType, double temperature)// create a constructor for the instance variable named scale
{
this.temperature = temperature;
this.tempType = tempType;
temperature = 0; // assign zero degrees if no value is specified
}
/********** (1) ****** create TWO accessor methods ****/
public double getCelsius() //create first accesor method to convert F to C degrees called getCelsius
{
if (tempType == 'C')//if tempType is C just return temperature because this method returns Celcius
{
return temperature;
}
else //or if tempType is F do the following calculations to convert Celcius to Fahrenheit and return those results
{
// Round to the nearest tenth of a degree and convert F to C
return ((double) (Math.round((5 * (temperature - 32.0) / 9.0) * 10.0)) / 10); //formula for C to F
}
}
public double getFahrenheit() //create second accesor method to convert C to F degrees
{
if (tempType == 'F')//if tempType is F just return temperature because this method returns Fahrenheit
{
return temperature;
}
else//or if tempType is C do the following calculations to convert Farhenheit to Celsius and return those results
{
// Round to the nearest tenth of a degree and convert F to C
return (Math.round((9 * (temperature / 5) + 32) * 10) / 10);
}
}
/********** (2) **********/
// Create THREE mutator methods.
public void setTemperature(double temperature) //create FIRST mutator to set temperature class variable
{
this.temperature = temperature;
}
public void setTempType(char tempType) // create SECOND mutator to set the tempType class variable
{
this.tempType = tempType;
}
public void setTemperatureScale(double temperature, char tempType) // create THIRD mutator to get temperature class variable and get tempType class variable
{
this.temperature = temperature;
this.tempType = tempType;
}
/*********** (3) *********/
// Create three comparison methods.
public boolean equals(Object obj)// create an equals method to test if two temperatures are equal--- (Object type, obj) coming through as parameters
{
if (obj instanceof Thermometer)
{
Thermometer t = (Thermometer) obj; //create instance of Thermometer 't' to hold the other object to compare both if (equal to)
return getCelsius() == t.getCelsius();//return true if first object == second object
}
return false; //return false if first object not equal second object
}
public boolean lessthan(Object obj)// create less than method to test if one temperatures is less than other --- (Object type, object) coming through as parameter
{
if (obj instanceof Thermometer)
{
Thermometer t = (Thermometer) obj;//return true if first object less than second object
return getCelsius() > t.getCelsius();
}
return false; //return false if greater than
}
public boolean greaterthan(Object obj) // create greater than method to test if one temperatures is greater thant other --- (Object type, object) coming through as parameter
{
if (obj instanceof Thermometer)
{
Thermometer t = (Thermometer) obj;
return getCelsius() < t.getCelsius();//return true if first object greater than second object
}
return false; //return true if less than
}
/************ (4) ***********/
public String toString() // create an appropriate toString method
{
if (tempType == 'C')//if tempType is C
{
return String.format("%.1f degrees C = %.1f degrees F", temperature, getFahrenheit());// return the getFahrenheit method (print it to console)
// prints degrees in fahrenheit = degrees in celsius
}
else
{
return String.format("%.1f degrees F = %.1f degrees C", temperature, getCelsius());//p return the getCelsius method (print it to console)
// prints degrees in celsius = degrees in fahrenheit
}
}
}
第2部分:
import java.util.*;
import javax.swing.*;
public class ThermometerDemo
{
// Create a main method to run the program.
public static void main(String[] args)
{
System.out.println("Celsius to Fahrenheit:");
// Create an object named CtoF_firstObj.
Thermometer CtoF_firstObj = new Thermometer();
CtoF_firstObj.setTemperature(0.00);
CtoF_firstObj.setTempType('C');
System.out.println(CtoF_firstObj.toString());
// Create an object named CtoF_secondObj.
Thermometer CtoF_secondObj = new Thermometer('C', 0);
CtoF_secondObj.setTemperature(-40);
System.out.println(CtoF_secondObj.toString());
// Create an object named CtoF_thirdObj.
Thermometer CtoF_thirdObj = new Thermometer('C',100);
System.out.println(CtoF_thirdObj.toString());
// Create an object named FtoC_fourthObj.
System.out.println("\nFahrenheit to Celsius:");
Thermometer FtoC_fourthObj = new Thermometer();
FtoC_fourthObj.setTemperatureScale(56.00, 'F');
System.out.println(FtoC_fourthObj.toString());
// Display Comparision
System.out.println("\nComparision:");
System.out.println("CtoF_firstObj < CtoF_secondObj: " + CtoF_firstObj.lessthan(CtoF_secondObj));
System.out.println("CtoF_secondObj = CtoF_thirdObj: " + CtoF_firstObj.equals(CtoF_thirdObj));
System.out.println("CtoF_thirdObj > FtoC_fourthObj: "+CtoF_thirdObj.greaterthan(FtoC_fourthObj));
}
}
解决方案
你的lessthan
和greaterthan
逻辑是相反的。修复它们如下:
lessthan
getCelsius() < t.getCelsius()
greaterthan
getCelsius() > t.getCelsius()
以下是输出:
Celsius to Fahrenheit:
0.0 degrees C = 32.0 degrees F
-40.0 degrees C = -40.0 degrees F
100.0 degrees C = 212.0 degrees F
Fahrenheit to Celsius:
56.0 degrees F = 13.3 degrees C
Comparision:
CtoF_firstObj < CtoF_secondObj: false
CtoF_secondObj= CtoF_thirdObj: false
CtoF_thirdObj > FtoC_fourthObj: true
推荐阅读
- iframe - 在父窗口中打开嵌入式谷歌表单的链接
- node.js - Loopback 4 身份验证元数据选项未定义
- gcc - gcc 无法识别选项 --allow-multiple-definition
- python - 计算for循环中生成的数据帧中数据之间的标准偏差
- javascript - Javascript 中的 Date 如何重载 +、-、>= 和 <= 运算符?
- javascript - 离子地理定位在android模拟器中不起作用
- postgresql - 如何创建用于存储产品信息的表 (SQL)
- java - 如何使用现有数据模型(Java 对象 POJO 类)并使用 Jackson 获取完整的 JSON 字符串?
- c - C中的矩阵加法仅在第一次加法中得到0
- c# - 请求通道在 00:01:00 WCF 之后尝试发送超时