In: Computer Science
(Code in Java please)
You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to Light, LightOnCommand and LightOffCommand), AND (edited)
Implement the CeilingFan class (with state, see p. 220), AND CeilingFanHighCommand (p. 221), CeilingFanMediumCommand, CeilingFanLowCommand, CeilingFanOffCommand (WITH Undo), AND
TEST your CeilingFan classes (see pp. 222 and 223), AND
Finally, implement and Test the MacroCommand class on pp. 224-227
EXAMPLE output for homework assignment……………
======= The COMMAND PATTERN =============
guest house garage door is UP guest house garage door is down
family room tv is ON family room tv is off
bathroom ceiling fan is on MEDIUM bathroom ceiling fan is on OFF
bathroom ceiling fan is on HIGH bathroom ceiling fan is on
OFF
patio hottub is heating to a steaming 104 deg F and is bubbling !
patio hottub is cooling to 98 deg F
------ Remote Control -------
[slot 0] com.company.LightOnCommand
[slot 1] com.company.LightOnCommand
[slot 2] com.company.CeilingFanOnCommand
[slot 3] com.company.StereoOnWithCDCommand
[slot 4] com.company.GarageDoorUpCommand
[slot 5] com.company.CeilingFanHighCommand
[slot 6] com.company.CeilingFanMediumCommand
[slot 7] com.company.CeilingFanLowCommand
[slot 8] com.company.CeilingFanOnOffCommand
Living room light is ON --- Living room light is off
Kitchen light is ON --- Kitchen light is off
Living room ceiling fan is on HIGH --- Living room ceiling fan is
off
Living room stereo is ON , CD , Volume: 11 --- Living room stereo
is off , RADIO , Volume: 5
guest house garage door is UP --- guest house garage door is
down
bathroom ceiling fan is on OFF bathroom ceiling fan is on LOW
bathroom ceiling fan is on MEDIUM bathroom ceiling fan is on
HIGH
bathroom ceiling fan is on MEDIUM bathroom ceiling fan is on LOW
bathroom ceiling fan is on OFF
================= Macros running...
Living room light is ON Living room stereo is ON , CD , Volume: 11
bathroom ceiling fan is on HIGH
Living room light is off Living room stereo is off , Volume: 5
bathroom ceiling fan is on OFF
Process finished with exit code 0
import java.util.*;
class Light{
boolean status;
String roomName;
public Light(String roomName){
this.roomName = roomName;
}
public Light(boolean status){
this.status = status;
}
public void LightOnCommand(){
this.status = true;
System.out.println("I am glowing in " + this.roomName );
}
public void LightOffCommand(){
this.status = false;
System.out.println("I am off in " + this.roomName);
}
}
class CeilingFan{
boolean status = false;
String speed;
String roomName;
public CeilingFan(String roomName){
this.roomName = roomName;
}
public void FanOffOn(){
this.status = !this.status;
if(this.status){
System.out.println("Fan is On in " + this.roomName);
}else{
System.out.println("Fan Off in " + this.roomName);
}
}
public void CeilingFanHighSpeed(){
this.speed = "High";
System.out.println("Running High in " + this.roomName);
}
public void CeilingFanMediumSpeed(){
this.speed = "Medium";
System.out.println("Running Meddium in " + this.roomName);
}
public void CeilingFanLowSpeed(){
this.speed = "Low";
System.out.println("Running Low in " + this.roomName);
}
}
class Stereo{
boolean status = false;
String roomName;
int volume = 0;
public Stereo(String roomName){
this.roomName = roomName;
}
public void StereoOnWithCDCommand(){
this.status = !this.status;
if(this.status){
System.out.println("The Stereo is ON in " + this.roomName);
}else{
System.out.println("THe stereo is Off " + this.roomName);
}
}
public void IncreseVolume(){
if(this.status){
this.volume = this.volume + 1;
System.out.println("Volume: " + this.volume);
}
}
public void DecreaseVolume(){
if(this.status){
if(this.volume >= 1)
this.volume = this.volume -1 ;
System.out.println("Volume: " + this.volume);
}
}
}
class GarageDoor{
boolean status = false;
String roomName;
public GarageDoor(String roomName){
this.roomName = roomName;
}
public void GarageDoorUpCommand(){
this.status = true;
System.out.println("The Garage door is open in " + roomName);
}
public void GarageDoorDownCommand(){
this.status = false;
System.out.println("The garage door is down for " + this.roomName);
}
}
public class Solution{
public static void main(String[] args) {
System.out.println("Remote Control");
System.out.println("Select Room");
System.out.println("1. Living Room");
System.out.println("2. Guest Room");
System.out.println("3. Kitchen");
System.out.println("4. Bathroom");
System.out.println("5. Family Room");
Scanner sc = new Scanner(System.in);
int roomNo = sc.nextInt();
String roomName = "";
switch (roomNo) {
case 1:
roomName = "Living Room";
break;
case 2:
roomName = "Guest Room";
break;
case 3:
roomName = "Kitchen";
break;
case 4:
roomName = "BaathRoom";
break;
case 5:
roomName = "Family Room";
default:
roomName = "UnKnown Room";
break;
}
Light light = new Light(roomName);
light.LightOnCommand();
light.LightOffCommand();
CeilingFan cf = new CeilingFan(roomName);
cf.FanOffOn();
cf.FanOffOn();
cf.CeilingFanHighSpeed();
cf.CeilingFanMediumSpeed();
cf.CeilingFanLowSpeed();
Stereo stereo = new Stereo(roomName);
stereo.StereoOnWithCDCommand();
stereo.IncreseVolume();
stereo.IncreseVolume();
stereo.DecreaseVolume();
stereo.StereoOnWithCDCommand();
GarageDoor garageDoor = new GarageDoor(roomName);
garageDoor.GarageDoorUpCommand();
garageDoor.GarageDoorDownCommand();
}
}