Question: Hello, the following assignment will simulate activities in a kindergarten classroom. It will simulate the students standing in a line, the students in their seats,

Hello, the following assignment will simulate activities in a kindergarten classroom. It will simulate the students standing in a line, the students in their seats, and the students playing musical chairs. I have already completed the code but the eliminateLosingStudents and the seatMusicalChairsWinner methods do not work. The output is not what it's supposed to be. Can you help me on fixing them? I have also added the failed tests.

The link with the instructions and what the output should be: https://ds.cs.rutgers.edu/assignment-ru-kindergarten/

This is the code:

package kindergarten;

/**

* This class represents a Classroom, with:

* - an SNode instance variable for students in line,

* - an SNode instance variable for musical chairs, pointing to the last student

* in the list,

* - a boolean array for seating availability (eg. can a student sit in a given

* seat), and

* - a Student array parallel to seatingAvailability to show students filed into

* seats

* --- (more formally, seatingAvailability[i][j] also refers to the same seat in

* studentsSitting[i][j])

*

* @author Ethan Chou

* @author Kal Pandit

* @author Maksims Kurjanovics Kravcenko

*/

public class Classroom {

private SNode studentsInLine; // when students are in line: references the FIRST student in the LL

private SNode musicalChairs; // when students are in musical chairs: references the LAST student in the CLL

private boolean[][] seatingLocation; // represents the classroom seats that are available to students

private Student[][] studentsSitting; // when students are sitting in the classroom: contains the students

public void enterClassroom(String filename) {

// CODE HERE

StdIn.setFile(filename); // open the file

int numStudents = StdIn.readInt(); // read the number of students

for (int i = 0; i < numStudents; i++) {

String firstName = StdIn.readString();

String lastName = StdIn.readString();

int height = StdIn.readInt();

Student student = new Student(firstName, lastName, height);

studentsInLine = new SNode(student, studentsInLine); // add student to front of list

}

}

public void setupSeats(String seatingChart) {

//CODE HERE

StdIn.setFile(seatingChart); // open the file

int numRows = StdIn.readInt(); // read the number of rows

int numCols = StdIn.readInt(); // read the number of columns

seatingLocation = new boolean[numRows][numCols]; // initialize seatingLocation

studentsSitting = new Student[numRows][numCols]; // initialize studentsSitting

for (int i = 0; i < numRows; i++) {

for (int j = 0; j < numCols; j++) {

seatingLocation[i][j] = StdIn.readBoolean(); // read boolean value and update seatingLocation

}

}

}

public void seatStudents() {

// CODE HERE

SNode current = studentsInLine;

while (current != null) {

Student student = current.getStudent();

boolean seated = false;

for (int i = 0; i < seatingLocation.length; i++) {

for (int j = 0; j < seatingLocation[i].length; j++) {

if (seatingLocation[i][j]) {

studentsSitting[i][j] = student;

seatingLocation[i][j] = false;

seated = true;

break;

}

}

if (seated) {

break;

}

}

current = current.getNext();

}

studentsInLine = null;

}

public void insertMusicalChairs() {

SNode head = null;// reference to front

musicalChairs = null;// reference to last

for (int i = 0; i < studentsSitting.length; i++) {

for (int j = 0; j < studentsSitting[i].length; j++) {

// checks if a student is present

if (studentsSitting[i][j] != null) {

SNode newNode = new SNode(studentsSitting[i][j], null);

if (head == null) {

head = newNode;

musicalChairs = newNode;

newNode.setNext(head);

} else {

musicalChairs.setNext(newNode);

musicalChairs = newNode;

musicalChairs.setNext(head);

}

// removes student from seat

studentsSitting[i][j] = null;

}

}

}

}

public void moveStudentFromChairsToLine(int size) {

int randStudent = StdRandom.uniform(size);

if (randStudent == 0 && musicalChairs.getNext() != musicalChairs) {

SNode temp = musicalChairs.getNext();

musicalChairs.setNext(musicalChairs.getNext().getNext());

insertByHeight(temp.getStudent());

} else if (randStudent == size - 1 && musicalChairs.getNext() != musicalChairs) {

SNode temp = musicalChairs;

SNode curr = musicalChairs;

while (curr.getNext() != musicalChairs) {

curr = curr.getNext();

}

curr.setNext(musicalChairs.getNext());

musicalChairs = curr;

insertByHeight(temp.getStudent());

} else {

SNode curr = musicalChairs;

int i = 0;

while (i < randStudent) {

curr = curr.getNext();

i++;

}

SNode temp = curr.getNext();

curr.setNext(curr.getNext().getNext());

insertByHeight(temp.getStudent());

}

}

public void insertByHeight(Student studentToInsert) {

SNode newNode = new SNode(studentToInsert, null);

if (studentsInLine == null) {

studentsInLine = newNode;

} else if (studentToInsert.getHeight() <= studentsInLine.getStudent().getHeight()) {

newNode.setNext(studentsInLine);

studentsInLine = newNode;

} else {

SNode prev = studentsInLine;

SNode current = studentsInLine.getNext();

while (current != null && studentToInsert.getHeight() > current.getStudent().getHeight()) {

prev = current;

current = current.getNext();

}

prev.setNext(newNode);

newNode.setNext(current);

}

}

/**

*

* Removes eliminated students from the musicalChairs and inserts those students

* back in studentsInLine in ascending height order (shortest to tallest).

*

* At the end of this method, all students will be in studentsInLine besides

* the winner, who will be in musicalChairs alone.

*

* 1. Find the number of students currently on musicalChairs

* 2. While there are more than 1 student in musicalChairs, call

* moveRandomStudentFromChairsToLine()

*/

public void eliminateLosingStudents() {

int numStudents = countMusicalChairs();

while (numStudents > 1) {

moveStudentFromChairsToLine(numStudents);

numStudents--;

}

}

private int countMusicalChairs() {

int count = 0;

SNode current = musicalChairs;

while (current != null) {

count++;

current = current.getNext();

}

return count;

}

/*

* If musicalChairs (circular linked list) contains ONLY ONE student (the

* winner),

* this method removes the winner from musicalChairs and inserts that student

* into the first available seat in studentsSitting. musicChairs will then be

* empty.

*

* This only happens when the musical chairs was just played.

*

* This methods does nothing if there is more than one student in musicalChairs

* or if musicalChairs is empty.

*/

public void seatMusicalChairsWinner() {

if (musicalChairs == null || musicalChairs.getNext() != musicalChairs) {

// musicalChairs is empty or contains more than one student

return;

}

Student winner = musicalChairs.getStudent(); // get the winner from musicalChairs

musicalChairs = null; // set musicalChairs to be empty

// insert the winner into the first available seat in studentsSitting

for (int i = 0; i < studentsSitting.length; i++) {

for (int j = 0; j < studentsSitting[i].length; j++) {

if (studentsSitting[i][j] == null) {

studentsSitting[i][j] = winner;

return;

}

}

}

}

public void playMusicalChairs() {

/* DO NOT UPDATE THIS METHOD */

eliminateLosingStudents();

seatMusicalChairsWinner();

seatStudents();

}

These are the failed tests:

  • Test 7.0: eliminateLosingStudents() using default constructor, enterClassroom(students1.in), setupSeats(seating1.in), insertMusicalChairs(). - FAILED. : timed out after 5 seconds. Possible infinite loop. Test 7.1: eliminateLosingStudents() using default constructor, enterClassroom(students2.in), setupSeats(seating2.in), insertMusicalChairs(). - FAILED. : timed out after 5 seconds. Possible infinite loop. Test 7.2: eliminateLosingStudents() using default constructor, enterClassroom(students3.in), setupSeats(seating3.in), insertMusicalChairs(). - FAILED. : skipping test, too many infinite loops detected on previous tests. Test 7.3: eliminateLosingStudents() using default constructor, enterClassroom(students4.in), setupSeats(seating4.in), insertMusicalChairs(). - FAILED. : skipping test, too many infinite loops detected on previous tests. Test 7.4: eliminateLosingStudents() using default constructor, enterClassroom(students2.in), setupSeats(seating1.in), insertMusicalChairs(). - FAILED. : skipping test, too many infinite loops detected on previous tests.
  • Test 7.5: eliminateLosingStudents() using default constructor, enterClassroom(students2.in), setupSeats(seatingCorners.in), insertMusicalChairs(). - FAILED. : skipping test, too many infinite loops detected on previous tests. Test 8.0: seatMusicalChairsWinner() using default constructor, enterClassroom(students1.in), setupSeats(seating1.in), insertMusicalChairs(), eliminateLosingStudents(). - FAILED. : skipping test, too many infinite loops detected on previous tests. Test 8.1: seatMusicalChairsWinner() using default constructor, enterClassroom(students2.in), setupSeats(seating2.in), insertMusicalChairs(), eliminateLosingStudents(). - FAILED. : skipping test, too many infinite loops detected on previous tests. Test 8.2: seatMusicalChairsWinner() using default constructor, enterClassroom(students3.in), setupSeats(seating3.in), insertMusicalChairs(), eliminateLosingStudents(). - FAILED. : skipping test, too many infinite loops detected on previous tests. Test 8.3: seatMusicalChairsWinner() using default constructor, enterClassroom(students4.in), setupSeats(seating4.in), insertMusicalChairs(), eliminateLosingStudents(). - FAILED. : skipping test, too many infinite loops detected on previous tests Test 8.4: seatMusicalChairsWinner() using default constructor, enterClassroom(students2.in), setupSeats(seating1.in), insertMusicalChairs(), eliminateLosingStudents(). - FAILED. : skipping test, too many infinite loops detected on previous tests. Test 8.5: seatMusicalChairsWinner() using default constructor, enterClassroom(students2.in), setupSeats(seatingCorners.in), insertMusicalChairs(), eliminateLosingStudents(). - FAILED. : skipping test, too many infinite loops detected on previous tests.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!