In: Computer Science
Java Programming
THE PROBLEM: At the beginning of each online meeting, the instructor randomly greets a student whose camera is on. That student greets, also at random, another student whose camera is on. This chain continues until there is only one student with the camera on. That student says hello to the instructor, concluding the hello chain.
A lot of things can go wrong during the hello chain. For example a student's audio may be muffled, muted, or noisy. We can't tell who the student greeted, i.e., we don't know who's the next link in this hello chain. Likewise, the student who has been greeted may not have heard their name. Or a student greets, by mistake, a student whose camera is turned off, etc.
Design and write a HelloChain class to simulate the situation above. Where there are issues in the hello chain but then you have to fix them. This can be done with a random name generator. Your class must be based on a data structure of your choice. Don't pile everything in the main method.
TRY RUNNING FOR DIFFERENT OUTPUTS
JAVA CODE:
import java.util.*;
public class HelloChain{
static Random rand = new Random();
// Creating random object to get the random
replies
static ArrayList<String> students;
// List of students in the meeting
static ArrayList<String> students_message = new
ArrayList<String>();
// List of possible replies of student
static String random_students_message;
// Variable to store random student reply
public static void main(String[] args) {
int i;
Scanner reader = new
Scanner(System.in);
System.out.println("How many
students are there in the meeting?");
int number_of_students =
reader.nextInt();
// Input number of students in the
meeting
System.out.println("Enter the names of all the students");
students = new
ArrayList<String>();
// Initializing students
arraylist
for (i = 0; i <
number_of_students; i++) {
System.out.print(i + 1);
students.add(reader.next());
// Adding
student name to the list
}
System.out.println("Students present in the meeting are : \n" + students);
get_tutor_message(students,
number_of_students);
// Calling method to get the
message from the instructor
}
private static void get_tutor_message(ArrayList<String> students, int number_of_students) {
ArrayList<String>
tutormessage = new ArrayList<String>();
// List of 3 possible instructor
messages
// blank
// hello
// hello any student name(generated
using the random function)
tutormessage.add("");
tutormessage.add("hello");
tutormessage.add("hello " +
students.get(rand.nextInt(students.size())));
String tutor_random_message =
tutormessage.get(rand.nextInt(tutormessage.size()));
System.out.println("Tutor Greeting
........\n" + tutor_random_message);
// Printing instructors message
try {
if
(tutor_random_message.equals("")) {
System.out.println("No greetings from the
Instructor.");
} else
get_student_message(tutor_random_message);
// sending
instructors message to get reply from the student
}
catch
(ArrayIndexOutOfBoundsException e) {
System.out.println("No more Students left for meeting");
}
}
//Method to get student responses
private static void get_student_message(String
tutor_random_message) {
// If last student is remaining
or no one is speaking
if (students.size() == 1 ||
tutor_random_message.equals("")) {
System.out.println((students.get(0) + " replying.......\n Hello
Teacher"));
}
else if (tutor_random_message.equals("hello")) {
// if only
hello is received ,without referral
// Then it can
be answered by any random student
// student's 3
possible responses are added to students message list
students_message.add("");
students_message.add("hello");
students_message.add("hello " +
students.get(rand.nextInt(students.size())));
String
random_student = students.get(rand.nextInt(students.size()));
// Once replied
the student is removed from the greeting that is students
list
students.remove(random_student);
// method to
print message of random_student
print_message(random_student);
}
else {
// if hello +
student name is received
String
student_name =
tutor_random_message.substring(tutor_random_message.indexOf(" ") +
1,
tutor_random_message.length());
// Student name
is taken out from the "hello student" by applying substring
//
function
students.remove(student_name);
// Once replied
the student is removed from the greeting that is students list
print_message(student_name);
// method to
print message of random_student
}
}
private static void print_message(String student) {
System.out.println(student + "
replying..........");
random_students_message =
students_message.get(rand.nextInt(students_message.size()));
// Picking up the randim message
from the students_message list
System.out.println(random_students_message);
get_student_message(random_students_message);
// Again calling the
get_student_message to continue the process
}
}
OUTPUT: