Question

In: Computer Science

Let’s represent 61 students sitting in a row as a simple string, 61 characters long. Students...

Let’s represent 61 students sitting in a row as a simple string, 61 characters long. Students not wearing a mask are represented by the space character. Students wearing a mask are represented by an asterisk (*). On the first day, only one student, sitting in the middle of the row is wearing a mask. Our initial string looks like this: " * " Students will decide whether to wear a mask the next day according to the following rule: If a student is sitting next to exactly one student wearing a mask (on the left or the right) then they will wear a mask to the next class. Otherwise they won’t wear a mask the next day. How does the mask-wearing behavior change over time. Generate the subsequent strings for 100 subsequent classes. Embed your output inside your code as a triple-quoted string. Here are the first few classes: " * " " * * " " * * " " * * * * " (should look like a pyramid with space in middle of third row)

Solutions

Expert Solution

Below approach has been followed :

1. Take one list of students (61) where randomly 4 students + middle student are wearing mask.

2. Iterate 100 times and check if left and right side student, if they are wearing mask , then mark that student is wearing mask, otherwise keep it as it is.

Please find the code snippet for the above problem statement:

import java.util.ArrayList;
import java.util.Random;

public class Wearing_Mask {

        static char wearingMask = '*';

        public static void main(String[] args) {

                int total_students = 61;
                int initial_random = 5;
                int total_day_report = 100;
                int count = 1;
                ArrayList<Integer> randomList = generateListOfRandomNumbers(initial_random, total_students);
                // System.out.println(randomList);
                ArrayList<Character> listOfStudents = new ArrayList<>();
                for (int i = 0; i < total_students; i++) {
                        if (randomList.contains(i))
                                listOfStudents.add(wearingMask);
                        else
                                listOfStudents.add(' ');
                }
                // System.out.println(listOfStudents.toString());//.substring(1,
                // listOfStudents.size()-1).length()
                while (count <= total_day_report) {
                        System.out.println("Day " + count + ": " + listOfStudents.toString());
                        listOfStudents = updateWearingMaskBehavior(listOfStudents);
                        count++;
                }

        }

        public static ArrayList<Character> updateWearingMaskBehavior(ArrayList<Character> students) {
                ArrayList<Character> updateStudent = new ArrayList<Character>(students.size());
                /*
                 * Iterate given arrayList and check if student is not wearing mask and
                 * left/right student is wearing mask then update that student as wearing mask
                 * otherwise mark it as it is.
                 */
                for (int i = 0; i < students.size(); i++) {
                        // first student
                        if (i == 0) {
                                if (students.get(i) != wearingMask && students.get(i + 1) == wearingMask)
                                        updateStudent.add(i, wearingMask);
                                else
                                        updateStudent.add(i, students.get(i));
                        }
                        // last student
                        else if (i == students.size() - 1) {
                                if (students.get(i) != wearingMask && students.get(i - 1) == wearingMask)
                                        updateStudent.add(i, wearingMask);
                                else
                                        updateStudent.add(i, students.get(i));
                        }
                        // middle students
                        else {
                                if (students.get(i - 1) == wearingMask || students.get(i + 1) == wearingMask)
                                        updateStudent.add(i, wearingMask);
                                else
                                        updateStudent.add(i, students.get(i));
                        }

                }
                return updateStudent;
        }

        public static ArrayList<Integer> generateListOfRandomNumbers(int total_rand, int total_student) {
                /*Generate random numbers i.e. 5
                 * Middle number is always included as middle student is wearing mask.*/
                Random rand = new Random();
                int exclude = total_student / 2;
                int random;
                ArrayList<Integer> list = new ArrayList<>();
                list.add(exclude);
                while (total_rand != 1) {
                        random = rand.nextInt(total_student);
                        if (!list.contains(random)) {
                                list.add(random);
                                total_rand--;
                        }

                }
                return list;
        }

}

I would recommend you to go through the comment and run code after uncommenting print statement. It will give you a better insight. For better understanding, you can refer below screenshot

Still if you have any queries, please put in comment box. I would be glad to assist you here. If you like my explanation, please hit a like button, it really motivates us to provide a good quality answer.


Related Solutions

Problem 3 (a) There are 3 male and 3 female students sitting in a row, and...
Problem 3 (a) There are 3 male and 3 female students sitting in a row, and they chose their seats randomly. What is the probably of the event "No two males sit together and no two females sit together"? (Hint: The gender of the seated students can only be FMFMFM or MFMFMF.) (b) Compute the probability of randomly drawing ve cards from a deck, and getting two Aces. (Hint: A deck has 52 cards, and 4 of them are Aces.)
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone...
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone number, e.g., 800-MYPYTHON, and prints the all numeric equivalent, 800-69798466. You should implement this using a loop construct to process each character from left to right. Build a new string that is the all numeric equivalent and then print the string.
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT