该任务是创建一个名为Temp的类,该类对照他免费提供给我们的教师TestTemp类运行。到目前为止,除了把我们应该使用的toString放在外,所有的东西都测试得很好。它应该像注释掉的部分一样格式化,但似乎没有工作。我提出了TestTemp类和我的Temp类的代码。我觉得我缺少一些东西,只是需要朝着正确的方向微调,而我的老师在分配到期之前再没有办公时间。我还粘贴了他添加到作业中的作业指导。试图在不同的Java类中调用toString方法
的类将被命名临时
添加compareTo方法。 (返回-1,如果调用对象具有较低的温度 ,0,如果是相同的,如果1大)
添加静态计数器(对象ID)来跟踪有多少温度 对象已创建(1, 2,3,...)
添加一个静态方法来告诉您已创建多少个温度对象 。
包括显示对象,如下所示一个toString方法(假定创建 第三一项):
对象ID:在F 3温度:用C 32.0温度:0.0
请注意,调用GETF或GETC仅返回值。他们不会 更改本机数据。
为了清楚的唯一方法如下:4层的构造,GETF, GETC,setDegrees,setScale,等于的toString,的compareTo和静态 getTempCount返回已 创建的对象的总数。
请注意,获得者将返回所要求的刻度 ,其四舍五入为十分之一度。不要将原始数据舍入。
请注意,如果温度为 时以摄氏度(四舍五入为 度的十分之一)相同,那么equals方法将返回true。
一定要好好利用这个()并且只有一个构造函数做 任何真正的工作。
Besure验证规模和遵循默认(C)如果一个“坏 规模”在
发送无需验证度和担心的事情,如 绝对零度等。
注:您的Temp类必须与UNIT-04-CodeSamples提供的TestTemp 类
//32 - 212 180 ticks
//
//0-100 1/10
//
public class TestTemp
{
public static void main(String [] args)
{
// only one constructor does any real work
Temp temp1 = new Temp(); // 0 C
Temp temp2 = new Temp(32); // 32 C
Temp temp3 = new Temp('F'); // 0 F
Temp temp4 = new Temp(32, 'F'); // 32 F
Temp temp5 = new Temp(); // 0 C
temp5.setDegrees(10);
temp5.setScale('F'); // 10 F
System.out.println("C: " + temp1.getC()); // C: 0.0
System.out.println("F: " + temp1.getF()); // F: 32.0
System.out.println(temp1.equals(temp4)); // true
System.out.println(temp1.equals(temp2)); // false
System.out.println("You have " + Temp.getTempCount()); // You have 5
if(temp3.compareTo(temp5)< 0) //temp3 is lower than than temp5
{
System.out.println("temp3 is lower than than temp5");
}
else
{
System.out.println("temp3 is same or larger than temp5");
}
System.out.println(temp1);
/*
TEMP OBJECT #1
IN C: 0.0
IN F: 32.0
*/
}
}
public class Temp implements Comparable<Temp>
{
private double degrees;
private char scale;
private static int tempCount = 0;
private int id;
public Temp()
{
this.degrees = 0;
this.scale = 'C';
// this(0.0, 'C');
}
public Temp(double degrees)
{
this.degrees = degrees;
this.scale = 'C';
// this(degrees, 'C');
}
public Temp(char scale)
{
this.degrees = 0;
this.scale = scale;
// this(0.0, scale);
}
public Temp(double degrees, char scale)
{
this.id = ++tempCount;
this.degrees = degrees;
this.scale = scale;
//(degrees, scale);
}
public static int getTempCount()
{
return tempCount;
}
public int getId()
{
return this.id;
}
public void setScale(char scale)
{
if(scale == 'C')
{
this.scale = scale;
}
else
{
this.scale = 'F';
}
}
public void setDegrees(double degrees)
{
this.degrees = degrees;
}
public double getC()
{
if(scale == 'C')
{
return degrees;
}
else
{
return (double)(5.0 * (degrees-32)/9.0);
}
}
public double getF()
{
if(scale == 'F')
{
return (double) degrees;
}
else
{
return (double)(9.0*(degrees)/5.0)+32;
}
}
@Override
public int compareTo(Temp obj)
{
if(this.getC() < obj.getC())
{
return -1;
}
if(this.getC() > obj.getC())
{
return 1;
}
return 0;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Temp))
{
return false;
}
Temp other = (Temp)obj;
return this.getC() == other.getC();
}
**public String toString()
{
return String.format("TEMP OBJECT ", this.id) + "\n" +
String.format("IN C: ", this.getC()) + "\n" +
String.format("IN F: ", this.getF());
}**
}
看完所有这些后,我仍然不知道您的问题究竟是什么。请创建一个[最小,完整和可验证的示例](http://stackoverflow.com/help/mcve),并描述问题以及预期结果 –