Lab Exam

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

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;

public class Subject {

private List<Observer> observers = new ArrayList<Observer>();


private int state;

public int getState() {


return state;
}

public void setState(int state) {


this.state = state;
notifyAllObservers();
}

public void attach(Observer observer){


observers.add(observer);
}

public void notifyAllObservers(){


for (Observer observer : observers) {
observer.update();
}
}
}

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{

public BinaryObserver(Subject subject){


this.subject = subject;
this.subject.attach(this);
}

@Override
public void update() {
System.out.println( "Binary String: " + Integer.toBinaryString( subject.getState() ) );
}
}
OctalObserver.java
public class OctalObserver extends Observer{

public OctalObserver(Subject subject){


this.subject = subject;
this.subject.attach(this);
}

@Override
public void update() {
System.out.println( "Octal String: " + Integer.toOctalString( subject.getState() ) );
}
}
HexaObserver.java
public class HexaObserver extends Observer{

public HexaObserver(Subject subject){


this.subject = subject;
this.subject.attach(this);
}

@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

//Create Subject object

//Create Hexa Observer for subject


//Create Octal Observer for subject
//Create Binary Observer for subject

// Print Lab Exam


// Print your name
// Print your ID

//Set Subject State to 13


//Set Subject State to 91

}
}

Step 5
Verify the output.
Lab Exam
Name: Asad Ali Shah
ID: 191236

First state change: 13


Hex String: D
Octal String: 15
Binary String: 1101

Second state change: 97


Hex String: 61
Octal String: 141
Binary String: 01100001

You might also like