該任務是創建一個名爲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),並描述問題以及預期結果 –