please write down the methods, pretend get and set functions are already made an
ID: 3591512 • Letter: P
Question
please write down the methods, pretend get and set functions are already made and so on.
variables are String color, String speed( none, slow, medium or fast), boolean on, double radius.
A method called turnON which will turn the fan on if it is not already on. It has no parameters and sets the speed of the fan to SLOW. if the fan is already turned on, dont change the speed to slow.
A method called faster. This changes the speed of the fan and has no parameters. If the fan is already at FAST it does not change; otherwise it speeds the fan up one speed (e.g., from SLOW to MEDIUM).
A method called slower. This changes the speed of the fan (has no parameters). If the fan is already at SLOW it does not change; otherwise it slows down the fan by one speed (e.g., from FAST to MEDIUM)
A method called turnOff that turns the fan off if it is on (has no parameters). It sets the speed of the fan to NONE.
Explanation / Answer
import java.io.*;
class Fan {
private String color;
private String speed;
private boolean on;
private double radius;
public Fan(String col, String sp, boolean st, double rad){
color = col;
speed = sp;
on = st;
radius = rad;
}
public Fan(){
color = "";
speed = "NONE";
on = false;
radius = 0;
}
public turnOff(){
if (on){
speed = "NONE"
on = false;
}
else {
System.out.println("Fan is already off");
}
}
public turnON(){
if (!on){
speed = "SLOW";
on = true;
}
else {
System.out.println("Fan is already on");
}
}
public faster(){
if (on){
if (speed.equals("SLOW")
speed = "MEDIUM";
if (speed.equals("MEDIUM")
speed = "FAST";
}
else {
System.out.println("Plese turn on the fan");
}
}
public slower(){
if (on){
if (speed.equals("FAST")
speed = "MEDIUM"
if (speed.equals("MEDIUM")
speed = "SLOW"
}
else {
System.out.println("Plese turn on the fan");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.