Professional Documents
Culture Documents
Lab Exam
Lab Exam
Lab Exam
Question No. 1:
Draw the class diagram only in Software Ideas Modeler for the following Observer Design Pattern. No
need to generate code for it.
Question No. 2:
Complete the code (No need to submit Diagram, only code files + output is required) for the following
Observer Design Pattern based on the following information. The code will allow that when the subject
is modified then all observer classes updated their values accordingly. For example, if the subject is
holding the value 10, then all observers including binaryObserver, OctalObserver and HexaObserver will
update their values as well. Use the existing code given for Steps 1-3 and complete the code for
ObserverPatternDemo (Step 4) to match the output given in Step 5. Submit the code and output of the
program.
Step 1
Create Subject class.
Subject.java
import java.util.ArrayList;
import java.util.List;
Step 2
Create Observer class.
Observer.java
public abstract class Observer {
protected Subject subject;
public abstract void update();
}
Step 3
Create concrete observer classes
BinaryObserver.java
public class BinaryObserver extends Observer{
@Override
public void update() {
System.out.println( "Binary String: " + Integer.toBinaryString( subject.getState() ) );
}
}
OctalObserver.java
public class OctalObserver extends Observer{
@Override
public void update() {
System.out.println( "Octal String: " + Integer.toOctalString( subject.getState() ) );
}
}
HexaObserver.java
public class HexaObserver extends Observer{
@Override
public void update() {
System.out.println( "Hex String: " + Integer.toHexString( subject.getState() ).toUpperCase() );
}
}
Step 4
Use Subject and concrete observer objects.
ObserverPatternDemo.java
public class ObserverPatternDemo {
public static void main(String[] args) {
//Complete the code in this section to match the output in Step 5
}
}
Step 5
Verify the output.
Lab Exam
Name: Asad Ali Shah
ID: 191236