2017-10-19 14 views
-2

私は温度コンバータを割り当てています。コードが提供され、それを動作させるためにコードを追加する必要があります。私が持っている質問は、スケールが文字列の配列の場合、どのようにスケールのcharを返すかということです。温度コンバータJava

**コード**を持つものはすべて私が書いたものです。

public class Temperature{ 
**private double temp; 
private char scale;** 
/** different scale names */ 
public static String[] scales = {"Celsius", "Fahrenheit", "Kelvin"}; 
**scales[0] = "C"; 
scales[1] = "F"; 
scales[2] = "K";** 

public Temperature(){ 
    **temp = 0.0; 
    scale = scales[0];** 
} 



/** Initializes a temperature object with given value in Celcius 
    * 
    * If the initial temperature is less than -273.15 then the temperature 
    * object will be initialized with -273.15C. 
    * 
    * @param temp is the initial temperature in Celsius. 
    */ 
public Temperature(double temp){ 
    **this.temp = temp; 
    if(temp < (-273.15)){ 
    temp = (-273.15); 
    } 
    scale = scales[0];** 
} 


/** Initializes a temperature object with given value using the specified scale 
* <par> 
* If the temperature is lower than absolute zero, then set the temperature to 
* absolute zero (in whichever scale is specified). 
* <par> 
* Examples: new Temperature(12.3, "K") 
*   new Temperature(-90.2, "Celsius") 
* 
* @param temp is the initial temperature 
* @param scale is the scale of initial temperature and must either be 
*  one of the Strings in the <code>scales</code> array, or 
*  the first letter (capitalized) of one of those strings. 
*/ 
public Temperature(double temp, String scale){ 
    **this.temp = temp; 
    if(temp < (-273.15)){ 
    temp = (-273.15); 
    } 
    scale = scales[];** 
} 



/** The output of this getter method must always be the first letter of one 
    * of the strings in the <code>scales</code> array, capitalized. 
    * 
    * @return the current scale of the object as a single char (the first letter, 
    *   capitalized of one of the strings from <code>scales</code>) 
    */ 
    public char getScale(){ 
    return 'X'; 
    } 
+2

あなたはそれが理にかなっているかどうかを調べるために、それを実行することができます。 – Henry

+1

@Henry how? 「メイン」はありません。 –

+0

@DawoodibnKareemはテスト用に追加するのが難しくありませんか? – Henry

答えて

0

あなたの最後のコンストラクタは、あなたの質問ごとに以下のようになります。..

public Temperature(double temp,String scale){ 
    if(scale.equals("C")) 
    { 
      if(temp<-273.15) 
      { 
       this.temp=-273.15; 
      } 
      else 
       this.temp=temp; 
     } 
     else if(scale.equals("F")) 
     { 
      if(temp<-459.67) 
      { 
       this.temp=-459.67) 
      } 
      else 
       this.temp=temp; 
     } 
     else if(scale.equals("K")) 
     { 
      if(temp<0) 
      { 
       this.temp=0; 
      } 
      else 
       this.temp=temp; 
     } 
     this.scale=scale; 
    } 
+3

'if(scale ==" C ")':真剣に?そしてなぜそれが範囲外である時だけtempを設定するのですか? – Henry

+0

オススメのために申し訳ありません@Henry –

+0

@ShivKumarそれはタイプミスではありません。あなたは '=='を使って 'String'を比較したいですか? –