Je pense que je dois faire une coulée de type pour obtenir le ventilateur à la sortie toString
,Essayer de changer certains types de données pour lire une autre sortie autre que le nombre
à des chaînes de sortie pour ma vitesse au lieu de 1, 2 , et 3. Je ne sais pas où je peux apporter ces changements, que ce soit en cours de test ou en privé dans la classe des fans. J'ai essayé d'ajouter quelques appels dans le toString mais j'obtiens le résultat hexadécimal. Quelques façons différentes de contourner cela seraient vraiment utiles.
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);
}
}
Je ne comprends pas ce que vous avez changé. – Blakenblaze