You have been hired by the CS Department to write code to help synchronize a pro
ID: 3627525 • Letter: Y
Question
You have been hired by the CS Department to write code to help synchronize a professor and his/her students during office hours. The professor, of course, wants to take a nap if no students are around to ask questions; if there are students who want to ask questions, they must synchronize with each other and with the professor so that- only one person is speaking at any one time,
- each student question is answered by the professor, and
- no student asks another question before the professor is done answering the previous one.
You are to write four procedures: AnswerStart(), AnswerDone(), QuestionStart() and QuestionDone().
The professor loops running the code: AnswerStart(); give answer; AnswerDone(). AnswerStart doesn’t return until a question has been asked. Each student loops running the code: QuestionStart(); ask question; QuestionDone(). QuestionStart() does not return until it is the student’s turn to ask a question. Since professors consider it rude for a student not to wait for an answer, QuestionEnd() should not return until the professor has finished answering the question. You can use a command line interface for this program. You are free to make other design choices and be creative in your implementation. You may use any programming language of your choice.
Explanation / Answer
//ok, here is some rough code that gives the desired output and uses synchronization.
//compile it and run.
public class Demo
{
public static void main(String[]args)
{
System.out.println("Welcome to the lecture !!! ");
Classroom c = new Classroom();
Professor p = new Professor();
c.setProf(p);
p.setClass(c);
Student s1 = new Student("S1", "germany",c);
Student s2 = new Student("S2", "england",c);
Student s3 = new Student("S3", "USA",c);
Student s4 = new Student("S4", "India",c);
Student s5 = new Student("S5", "China",c);
s1.start();
s2.start();
s3.start();
s4.start();
s5.start();
}
}
class Classroom
{
Professor prof;
public void setProf(Professor p)
{
prof = p;
}
public synchronized void currentStudent(String msg, String name)
{
System.out.println(name + " says : " + msg);
prof.answer(name);
System.out.println(" ");
}
}
class Student extends Thread
{
String name;
String ques;
Classroom cl;
Student(String n, String q, Classroom c)
{
name = n;
ques = q;
cl = c;
}
public void run()
{
String message = "Sir where is " + ques + " ? ";
for(int i = 0; i < 3; i++) // ask the same question 3 times !
{
cl.currentStudent(message, name);
//ask a question and wait or sleep before you ask again.
try
{
Thread.sleep(1500); // sleep for 1.5 sec.
}catch(Exception ex){System.out.println("error : " + ex);}
}
}//run
}
class Professor
{
String pro = "Nutty Prof";
Classroom cl;
String ans = "I dont know !";
public void setClass(Classroom c)
{
cl = c;
}
public void answer(String name)
{
System.out.println("PROF says : Dear " + name + " " + ans);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.