2017-05-03 69 views
0

該任務是創建一個名爲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()); 
     }** 

    } 
+1

看完所有這些後,我仍然不知道您的問題究竟是什麼。請創建一個[最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve),並描述問題以及預期結果 –

回答

0

您使用的String.format不應該需要多創造正常工作。只用一個。

return String.format("TEMP OBJECT: $d, %nIN C: %.2f, %nIN F: %.2f", this.id, this.getC(), this.getF());

通過改變小數點%.2f%.5f後的值將打印0.00000而不是0.00例如修改浮點的精度。

如果您對使用格式有任何疑問,我建議您閱讀它的文檔,以瞭解它還能做些什麼。 Link

編輯:添加換行符。忘記提及只需換一個換行%n。除非你想讓你的換行符以一個空格開始,否則不要在它們後面空格。

+0

謝謝您提醒我關於放置到小數,因爲這是部分分配。我喜歡你爲字符串使用的代碼,但它使用了我們還沒有在課堂上學到的代碼,所以我實際上會被標記爲使用它,即使這樣做是合理的。 – GuyMcAndrews

+0

正在使用的代碼的哪部分不是你使用的?我看到你正在使用format(),爲什麼你的教授會按照預期使用函數停靠點?它與你實現的並沒有太大的不同,但它連接了過程並執行相同的過程,對嗎?那麼......我很抱歉聽到,也許你可以和你的教授覈對一下這個實現嗎? –

+0

我認爲我們不能使用他沒有教過我們的代碼的原因是因爲它讓他更容易分辨誰在作弊。就像這是java類的介紹,所以如果我突然知道他沒有教我們的代碼,它更可能被複制到某個地方作弊。這也使得我認爲評分更容易,但下次我在實驗室時會提出它,看看是否允許這樣做。無論哪種方式,它仍然有幫助,所以再次感謝您的幫助。 – GuyMcAndrews

0

您需要格式化佔位符,你toString方法應該是這樣

public String toString() 
     { 
      return String.format("TEMP OBJECT %d", this.id) + "\n" + 
       String.format("IN C: %.2f", this.getC()) + "\n" + 
       String.format("IN F: %.2f", this.getF()); 
     } 

這裏%d的整數和%f爲小數。 .2f將小數位數限制爲2.請參閱更多示例here

相關問題