13,888 views
0 votes
0 votes
Design and implement your own simple class to represent any household item of your choice (toaster, fan, hair dryer, piano ...) Your class should have a constructor, one additional method and at least one member variable (e.g. boolean isOn to turn the item on or off). Be sure you demonstrate your class works properly by constructing an instance of it and calling your method. Hint: check out the week 6 module readings under "additional readings". The guitar example is a good model for thisDesign and implement your own simple class to represent any household item of your choice (toaster, fan, hair dryer, piano ...) Your class should have a constructor, one additional method and at least one member variable (e.g. boolean isOn to turn the item on or off). Be sure you demonstrate your class works properly by constructing an instance of it and calling your method. Hint: check out the week 6 module readings under "additional readings". The guitar example is a good model for this..

User SouravA
by
3.0k points

1 Answer

4 votes
4 votes

Answer:

class fan{

private String model;

private boolean isOn;

//Constructor goes here

public fan(String model, boolean isOn) {

this.model = model;

this.isOn = isOn;

}

// An additional method goes here

public boolean turnOn(boolean yes){

if(yes) {

this.isOn=true;

System.out.println("Fan is blowing");

return true;

}

else

this.isOn=false;

System.out.println("Fan is off");

return false;

}

}

Step-by-step explanation:

Demonstrating the working of the class in a main method. Below is a complete code

public class fanTest {

public static void main(String[] args) {

fan fan1 = new fan("Binatone", false);

fan1.turnOn(false);

}

}

class fan{

private String model;

private boolean isOn;

//Constructor goes here

public fan(String model, boolean isOn) {

this.model = model;

this.isOn = isOn;

}

// An additional method goes here

public boolean turnOn(boolean yes){

if(yes) {

this.isOn=true;

System.out.println("Fan is blowing");

return true;

}

else

this.isOn=false;

System.out.println("Fan is off");

return false;

}

}

User Anurag Manikkame
by
2.9k points