Question

In: Computer Science

Java Programming THE PROBLEM: At the beginning of each online meeting, the instructor randomly greets a...

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.

Solutions

Expert Solution

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:


Related Solutions

PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
JAVA - Programming Exercise 10-5. The developers of a free online game named Sugar Smash have...
JAVA - Programming Exercise 10-5. The developers of a free online game named Sugar Smash have asked you to develop a class named SugarSmashPlayer that holds data about a single player. The class contains the following fields: idNumber - the player’s ID number (of type int) name - the player's screen name (of type String) scores - an array of integers that stores the highest score achieved in each of 10 game levels Include get and set methods for each...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
This problem is about the java programming and contain two parts First part: a word that...
This problem is about the java programming and contain two parts First part: a word that reads the same forward and backward is called a palindrome, e.g., "mom", "dad", "racecar", "madam", and "Radar" (case-insensitive). Write a program called TestPalindrome, that asks the user for a word and prints whether the word is a palindrome or not. Hint: for a string called inStr, you can use inStr. toLowerCase() which returns a new string which is all in lower case letters. Use...
The following code is included for the java programming problem: public class Bunny {        private...
The following code is included for the java programming problem: public class Bunny {        private int bunnyNum;        public Bunny(int i) {               bunnyNum = i;        }        public void hop() {               System.out.println("Bunny " + bunnyNum + " hops");        } } Create an ArrayList <????> with Bunny as the generic type. Use an index for-loop to build (use .add(….) ) the Bunny ArrayList. From the output below, you need to have 5. Use an index for-loop...
Assignment 1. Linear Programming Case Study Your instructor will assign a linear programming project for this...
Assignment 1. Linear Programming Case Study Your instructor will assign a linear programming project for this assignment according to the following specifications. It will be a problem with at least three (3) constraints and at least two (2) decision variables. The problem will be bounded and feasible. It will also have a single optimum solution (in other words, it won’t have alternate optimal solutions). The problem will also include a component that involves sensitivity analysis and the use of the...
Assignment 1. Linear Programming Case Study Your instructor will assign a linear programming project for this...
Assignment 1. Linear Programming Case Study Your instructor will assign a linear programming project for this assignment according to the following specifications. It will be a problem with at least three (3) constraints and at least two (2) decision variables. The problem will be bounded and feasible. It will also have a single optimum solution (in other words, it won’t have alternate optimal solutions). The problem will also include a component that involves sensitivity analysis and the use of the...
JAVA Please put detailed comments as well explaining your program. I'm in a beginning programming class,...
JAVA Please put detailed comments as well explaining your program. I'm in a beginning programming class, and so please use more basic techniques such as if else statements, switch operators, and for loops if needed. http://imgur.com/a/xx9Yc Pseudocode for the main method: Print the headings             Print the directions             Prompt for the month             If the month is an integer       (Hint: Use the Scanner class hasNextInt method.)                         Input the integer for the month                         Get the...
Java Programming Question The problem is to count all the possible paths from top left to...
Java Programming Question The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints that from each cell you can either move to right or down.Input: The first line of input contains an integer T, denoting the number of test cases. The first line of each test case is M and N, M is number of rows and N is number of columns.Output: For each test case, print the...
Java programming problem: For simplicity, you can consider DNA sequences as strings in the alphabet of...
Java programming problem: For simplicity, you can consider DNA sequences as strings in the alphabet of {A, C, G, T}. Implement a special hash table to store all short DNA segments of certain length (e.g., 20) found in a long DNA sequence. Every DNA segment is stored as a (key, value) pair, where the key is the sequence of the segment, and the value is the position of the segment in the DNA. For example given a DNA sequence of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT