Question: Question 2 . ( 4 0 pts . ) Our friend Willy Wazoo wrote an example to demonstrate the use of the Observer design pattern.
Question pts Our friend Willy Wazoo wrote an example to demonstrate the use of the Observer design pattern.
import java.util.;
class Elevator extends Observable
private int floor;
private int maxfloor;
private List observers new ArrayList;
public Elevatorint maxfloor
this.maxfloor maxfloor;
public void up
if thisfloor maxfloor
this.floor;
public void down
if thisfloor
this.floor;
public void addLightObserver obs
observers.addobs;
class Light implements Observer
private int floor;
private boolean state;
public Lightint floor
this.floor floor;
public void setOn
this.state true;
public void setOff
this.state false;
public boolean getStatus
return state;
public int getFloor
return floor;
class ElevatorDemo
public static final int FLOORS ;
public static void mainString args
Elevator anElevator new ElevatorFLOORS;
Light light;
List lights new ArrayList;
forint i ; i FLOORS; i
light new Lighti;
anElevator.addLightlight;
lights.addlight;
printLightslights; Should print as lit and all other floors as unlit
anElevator.up;
printLightslights; Should print as lit and all other floors as unlit
anElevator.up;
printLightslights; Should print as lit and all other floors as unlit
anElevator.up;
printLightslights; Should print as lit and all other floors as unlit
anElevator.up;
printLightslights; Should print as lit and all other floors as unlit
private static void printLightsList lights
ListIterator listIterator lights.listIteratorlightssize;
Light light;
while listIteratorhasPrevious
light listIterator.previous;
if lightgetStatus
System.out.printfd
light.getFloor;
else
System.out.printfd
light.getFloor;
Unfortunately, Willy forgot something. What is it Edit the code below to help Willy implement the Observer design
pattern correctly. Do not make any unnecessary changes. Your goal is to preserve Willys code as much as possible but
to make sure that it functions correctly by turning on only one light on the floor where the elevator is
Which model does your implementation above follow?
a Pull model
b Push model
Change the code to implement a different model ie Push instead of Pull or Pull instead of Push Make sure your code
is valid Java code and has the same functionality as the original implementation. Feel free to copy the version of Willys
code that you fixed, paste it in the text area below, and then edit it
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
