In: Computer Science
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)
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.