Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider a standard classroom setting in which you have one instructor and three

ID: 666853 • Letter: C

Question

Consider a standard classroom setting in which you have one instructor and three students. Each student has the ability to raise their hand in class to respond to an instructor's question. In this scenario, the instructor is considered the object, and the students are considered to be observers.

Directions: For this assignment, you will write a software application which demonstrates a solution to the problem using an Observer pattern approach using Java. Your program should do the following:

When the instructor asks a question, all students will raise their hand to answer.

The instructor will select a student to answer the question.

The student answers the question.

Once the question is answered and accepted by the instructor, the instructor will change the "state" of the question.

The instructor then re-broadcasts a new question.

Each student hears the question and proceeds to answer the new question and so forth.

Explanation / Answer

Java API provides supports the Observer design pattern.

Class:

java.util.Observable

Interface:

java.util.Observer.

According to the problem statement, Instructor is the object and student is the observer.

Steps to solve the problem

Step 1:

package Observer_Pattern;

import java.util.*;

class Teacher extends Observable
    {
        private boolean question_status;
        private boolean asked = false;
        private boolean answered = false;

        public Teacher(boolean question_stat)
        {
             this.question_status = question_stat;
        }

        public void current_status(boolean stat)
        {
            if ("true".equals(stat))
            {
             asked=true;
             setChanged();
            }
            else if ("false".equals(stat))
            {
             asked=true;
             setChanged();
            }

        // Observers are only notified if the vote caused
         // a change to the current tally.
         notifyObservers(stat);
     }

        public boolean asked_or_answered()
        {
         return question_status;
        }

    
}

Step 2:

package Observer_Pattern;
import java.util.*;
class Students implements Observer
{

     public Students(Teacher T)
     {
         T.addObserver(this);
         System.out.println(T.question_status);
         System.out.println();
     }

     public void update(Observable Student, Object o)
     {
         System.out.println(" Current question answered ");
     }
}

Step 3:

Implementation:

package Observer_Pattern;

public class Ask_Question
{

     public static void main(String[] args)
     {
      Teacher t=new Teacher(true);
     
      t.question_status=true;
     }
}