番号

2017-03-25 17 views
0

以外の異なる出力を読み取るために、いくつかのデータ型を変更しようとすると、私は、私は出力toStringにファンを得るために、いくつかの型キャストを行う必要があります考えています私の速度の代わりに、1の出力文字列へ番号

、2 3.私は、テストクラスで送信されるか、ファンクラスでプライベートに移行するかのいずれかを変更できるかどうかはわかりません。 toStringにいくつかの呼び出しを追加しようとしましたが、16進数の結果が得られています。これを回避するいくつかの異なる方法が本当に役に立ちます。

public class Fan { 
    public final int SLOW = 1; 
    public final int MEDIUM = 2; 
    public final int FAST = 3; 

    private int speed; // = SLOW; 
    private boolean on; // on = false; 
    private double radius; // radius = 5; 
    private String color; // = "white"; 



    public int getSpeed () {return speed; } 

    public void setSpeed (int speed) { this.speed = speed; } 

    public boolean isOn () {return on; } 

    public void setOn (boolean on) {this.on = on; } 

    public double getRadius () {return radius; } 

    public void setRadius (double radius) { this.radius = radius; } 

    public String getColor () { return color; } 

    public void setColor (String color) { this.color = color; } 

    public Fan () { 
     System.out.println("Default constructor called"); 
    } 
    // default constructor 

    public Fan (int speed, boolean on, double radius, String color) { 
     this.speed = speed; 
     this.on = on; 
     this.radius = radius; 
     this.color = color; 
     System.out.println("Overloaded constructor called"); 
    } 
    //overloaded constructor 

    public String toString() { 
     String x = "speed is " + speed + ", is the fan turned on? " + on + ",the  radius " 
     + "of the blades are " + radius + ", and the color of the fan is " +  color; 
     return x; 
    } 

} 

public class FanTest { 


    public static void main(String[] args) { 

     Fan f1 = new Fan(3, true, 10, "Yellow") ; 
     Fan f2 = new Fan(); 
     Fan f4 = new Fan(); 
     f1.equals(f4) ; 
     Fan f3 = f1; 
     int slow = 1; 
     int medium = 2; 
     int fast = 3; 

      System.out.println(f1); 
      System.out.println(f2); 
      System.out.println(f3); 

     if (f1.equals(f3)) { 
      System.out.println("Obejects r3 and r1 are the same\n"); 
     } else { 
      System.out.println("Objects r3 and r1 are different"); 
     } 

     while (f1.equals(f4)) 
      f4.setOn(false); 
      f4.setSpeed(2); 
      f4.setColor("green"); 
      f4.setRadius(8); 
      System.out.println(f4); 
    } 

} 
+0

と同じように返すことを意味します。 – Blakenblaze

答えて

0

まず、デフォルトのコンストラクタでは、変数は設定されません。つまり、デフォルトのコンストラクタで作成されたすべてのFanオブジェクトは無効な状態です(デフォルトでは、speed0)。あなたは、「出力私のスピードのための文字列の代わりに、1、2、及び3」たいと言っ

private int speed = SLOW; 
private boolean on = false; 
private double radius = 5; 
private String color = "white"; 

:私はあなたはコメントを解除行9-12に示唆しています。これは、if..elseまたはswitchを使用して簡単に達成できます。

if (speed == 1) { 
    str = str + " slow "; 
} else if (speed == 2) { 
    str = str + " medium "; 
} else if (speed == 3) { 
    str = str + " fast "; 
} 

使用スイッチ:

switch (speed) { 
    case 1: 
     str = str + " slow "; 
     break; 
    case 2: 
     str = str + " medium "; 
     break; 
    case 3: 
     str = str + " fast "; 
     break; 
} 

についてf1.equals(f4):これらは、異なるオブジェクトなので、それはtrueことはありません。

equalsequalsは、クラスObjectで定義されているメソッドなので、どのオブジェクトでも使用できます。あなたはそれを上書きしない限りしかし、それはアイデンティティをチェックし、魔女は

a.equals(b) 

はいつも私はあなたが何が変更されたか取得しない

a == b