2017-03-25 2 views
0

번호 이외의 다른 출력을 읽을 일부 데이터 유형을 변경하려고 3. 나는 시험 수업에서 보내지거나 팬 클래스에서 사적인 것으로 옮겨 갈 수있는 변경 사항을 어디에서 만들 수 있는지 잘 모르겠습니다. toString에 몇 가지 호출을 추가하려고 시도했지만 16 진수 결과를 얻고 있습니다. 이 문제를 해결하기위한 여러 가지 방법이 도움이 될 것입니다.2 출력 내 속도에 대한 문자열 대신 1</p> <p>, 내가 출력 <code>toString</code>에 팬을 얻기 위해 어떤 종류의 캐스팅을 할 필요가 생각하고

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

먼저 기본 생성자에서 변수가 설정되지 않습니다. 즉, 기본 생성자로 생성 된 모든 팬 객체는 유효하지 않은 상태입니다 (예 : speed0). 내가 주석 라인 9-12에 당신을 제안 :

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

당신은 당신을 "출력 내 속도에 대한 문자열 대신 1, 2, 3"을 원했다. 이는 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 결코 없다.

equals : equals은 클래스 Object에 정의 된 방법이므로 모든 개체에서 사용할 수 있습니다. 당신이 그것을 무시하지 않는 한, 그것은이 정체성를 확인, 마녀

a.equals(b) 

항상 당신이 변경된 것을 얻을하지 않습니다

a == b 
관련 문제