Question: Question 2 . ( 4 0 pts . ) Our friend Willy Wazoo wrote an example to demonstrate the use of the Observer design pattern.

Question 2.(40 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 max_floor;
private List observers = new ArrayList();
public Elevator(int max_floor){
this.max_floor = max_floor;
}
public void up(){
if (this.floor <= max_floor){
this.floor++;
}
}
public void down(){
if (this.floor >0){
this.floor--;
}
}
public void addLight(Observer obs){
observers.add(obs);
}
}
class Light implements Observer {
private int floor;
private boolean state;
public Light(int 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 =25;
public static void main(String[] args){
Elevator anElevator = new Elevator(FLOORS);
Light light;
List lights = new ArrayList();
for(int i =0; i <= FLOORS; i++){
light = new Light(i);
anElevator.addLight(light);
lights.add(light);
}
printLights(lights); // Should print (((0))) as lit and all other floors as unlit
anElevator.up();
printLights(lights); // Should print (((1))) as lit and all other floors as unlit
anElevator.up();
printLights(lights); // Should print (((2))) as lit and all other floors as unlit
anElevator.up();
printLights(lights); // Should print (((3))) as lit and all other floors as unlit
anElevator.up();
printLights(lights); // Should print (((4))) as lit and all other floors as unlit
}
private static void printLights(List lights){
ListIterator listIterator = lights.listIterator(lights.size());
Light light;
while (listIterator.hasPrevious()){
light = listIterator.previous();
if (light.getStatus()){
System.out.printf("(((%d)))
", light.getFloor());
}
else {
System.out.printf("%d
", 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 (i.e., 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!