Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please follow the instructions carefully as there are some specific conditions to the code. Do not use pre-written code, none of them work. I need

Please follow the instructions carefully as there are some specific conditions to the code. Do not use pre-written code, none of them work. I need help with the last four methods only, starting from moveStudentsFromChairToLine(). Thank you!

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedCoding files:

Classroom.java

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])

*/

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 Classroom(SNode l, SNode m, boolean[][] a, Student[][] s) {

studentsInLine = l;

musicalChairs = m;

seatingLocation = a;

studentsSitting = s;

}

/**

* This method simulates students standing in line and coming into the classroom

* (not leaving the line).

* It does this by reading students from input file and inserting these students

* studentsInLine singly linked list.

* 1. Open the file using StdIn.setFile(filename);

* 2. For each line of the input file:

* 1. read a student from the file

* 2. create an object of type Student with the student information

* 3. insert the Student object at the FRONT of the linked list

* Input file has:

* 1) one line containing an integer representing the number of students in the

* file, say x

* 2) x lines containing one student per line. Each line has the following

* student

* information separated by spaces: FirstName LastName Height

*

* To read a string using StdIn use StdIn.readString()

* The input file has Students in REVERSE alphabetical order. So, at the end of

* this

* method all students are in line in alphabetical order.

* DO NOT implement a sorting method, PRACTICE add to front.

* @param filename the student information input file

*/

public void enterClassroom(String filename) {

// WRITE YOUR CODE HERE

}

/**

* This method creates and initializes the seatingAvailability (2D array) of

* available seats inside the classroom. Imagine that unavailable seats are

* broken and cannot be used.

* 1. Open the file using StdIn.setFile(seatingChart);

* 2. You will read the seating chart input file with the format:

* An integer representing the number of rs in the classroom, say r

* An integer representing the number of cumns in the classroom, say c

* Number of r lines, each containing c true or false values (true represents

* that a seat is present in that cumn)

* 3. Initialize seatingLocation and studentsSitting arrays with r rs and c

* cumns

* 4. Update seatingLocation with the boolean values read from the input file

* This method does not seat students on the seats.

*/

public void setupSeats(String seatingChart) {

// WRITE YOUR CODE HERE

}

/**

*

* This method simulates students standing inline and then taking their seats in

* the classroom.

* 1. Starting from the front of the studentsInLine singly linked list

* 2. Remove one student at a time from the list and insert the student into

* studentsSitting according to

* seatingLocations

* studentsInLine will then be empty

* If the students just played musical chairs, the winner of musical chairs is

* seated separately

* by seatMusicalChairsWinner().

*/

public void seatStudents() {

// WRITE YOUR CODE HERE

}

/**

* Traverses studentsSitting r-wise (starting at r 0) removing a seated

* student and adding that student to the end of the musicalChairs list.

*

* row-wise: starts at index [0][0] traverses the entire first r and then

* moves

* into second r.

*/

public void insertMusicalChairs() {

// WRITE YOUR CODE HERE

}

/**

*

* Removes a random student from the musicalChairs.

*

* @param size represents the number of students currently sitting in

* musicalChairs

*

* 1. Select a random student to be removed from the musicalChairs

* CLL.

* 2. Searches for the selected student, deletes that student from

* musicalChairs

* 3. Calls insertByHeight to insert the deleted student into

* studentsInLine list.

*

* Requires the use of StdRandom.uniform(int b) to select a random

* student to remove,

* where b is the number of students currently sitting in the

* musicalChairs.

*

* The random value denotes the refers to the position of the

* student to be removed

* in the musicalChairs. 0 is the first student

*/

public void moveStudentFromChairsToLine(int size) {

// WRITE YOUR CODE HERE

}

/**

* Inserts a single student, eliminated from musical chairs, to the

* studentsInLine list.

* The student is inserted in ascending order by height (shortest to tallest).

*

* @param studentToInsert the student eliminated from chairs to be inserted into

* studentsInLine

*/

public void insertByHeight(Student studentToInsert) {

// WRITE YOUR CODE HERE

}

/**

* 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() {

// WRITE YOUR CODE HERE

}

/*

* 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() {

// WRITE YOUR CODE HERE

}

SNODE.Java

package kindergarten;

/**

* This class represents a student node, with a student

* object representing the student, and a next pointer

* for the student next in line.

*/

public class SNode {

private Student student; // the data part of the node

private SNode next; // a link to the next student int the linked list

public SNode ( Student s, SNode n ) {

student = s;

next = n;

}

public SNode() {

this(null, null);

}

public Student getStudent () { return student; }

public void setStudent (Student s) { student = s; }

public SNode getNext () { return next; }

public void setNext (SNode n) { next = n; }

}

Student.Java

/**

* This class represents a student, with a string variable for the

* student's name and an int variable for the student's height.

*/

public class Student {

private String firstName;

private String lastName;

private int height;

/*

* Constructor

*/

public Student ( String f, String l, int h ) {

firstName = f;

lastName = l;

height = h;

}

/*

* Default constructor sets student's height to 0 (zero)

*/

public Student () {

this(null, null, 0);

}

/**

* Compares the names of this student to paremeter student,

* returning an int value representing which name comes in first alphabetically.

*

* @param other the student being compared

*

* @return an int value of which name comes first alphabetically

* n

* n > 0 means parameter student's name comes before this student

*/

public int compareNameTo ( Student other ) {

int lastNameCompare = this.lastName.compareToIgnoreCase(other.getLastName());

if ( lastNameCompare == 0 ) {

// same last name, compare first names

return this.firstName.compareToIgnoreCase(other.getFirstName());

}

return lastNameCompare;

}

/* Getter and setter methods */

public String getFirstName () { return firstName; }

public void setFirstName ( String f ) { firstName = f; }

public String getLastName () { return lastName; }

public void setLastName ( String l ) { lastName = l; }

public int getHeight () { return height; }

public void setHeight ( int h ) { height = h; }

}

Overview In this assignment you will simulate activities in a kindergarten classroom. You will simulate the students standing in a line, the students on their seats, and the students playing musical chairs. The assignment uses: - a Singly Linked List to simulate student standing in a line. - a 2D array to simulate students at their seats. - a Circular Linked List to simulate students sitting on chairs to play the musical chairs game. A U array simulates students at their seats A Circular Linked List simulates students sitting on chairs to play the musical chairs standing on a line game Implementation Overview of files provided - Student class which holds a student's information. - SNode class represents the node object to be used in the linked structures. It contains a reference to a Student object and a reference to the next node in the list. - Classroom class holds all of the methods to be written and that will be tested when running the game. Edit the empty methods with you solution, but DO NOT edit the provided ones or the methods signatures of any method. This is the file you submit. - Driver class, which is used to test your methods interactively. The classroom state will be printed after every method is used. Feel free to edit this file, as it is provided only to help you test your code. It is not submitted and it is not used to grade your code. - StdRandom class contains the StdRandom.uniform (n) method that you will use in the method that plays the musical chairs game. - Stdln and StdOut, which are used by the driver. Do not edit these classes.. - Multiple text files. Feel free to edit them or even make new ones to help test your code. They are not submitted. - Files containing student information. - Files containing seat location in the classroom. - DO NOT add new import statements. - DO NOT change any of the method's signatures. - You are encouraged to write helper methods as you see fit. Just make sure that they are private. The class contains: - studentlnLine which refers to the first student in the singly linked list. - musicalChairs which refers to the LAST student in the circularly linked list when the game is being played. - seatingLocation which is a 2D boolean array that shows which seats exist for students to seat at. - students Sitting which is a 2D Student array that contains the students when they are sitting. - NOTE: seatingLocation and studentsSitting are parallel arrays meaning that seatingLocation[i][i] also refers to the same seat in students Sitting[i][i] - provided methods that are used by the driver and empty methods you are expected to write your code in. - the printClassroom() function can be used to show the state of the classroom (eg. the states of students in line, seating, and musical chairs) at any time in your code. The input file has Students in reverse alphabetical order; so at the end of this method the list will have all students in alphabetical order. DO NOT implement a sorting algorithm, there is no need. - Submit Classroom.java with this method completed under Early Submission to receive extra credit. This is the expected output for students 1 in Students in Line: M. Brow N. Dyer P. Ferg D. Harb M. Hawk C. Heat J. Keer G. Mata . MCLa M. Modi D. Mont P. Reis W. Ryde N. Schn S. Sink F. Wolf Sitting Students: EMPTY Students in Musical Chairs: EMPTY What would you like to do now? 1. Test a new input file 2. Test another method on the same file 3. Quit Enter a number 2. setupseats This method creates the classroom seats. Imagine that someone is putting down seats on a rectangular or square room. - We will use a 2D array to simulate a room where each cell of the array can have a seat or not. - The instance variable seatingLocations is the 2D boolean array that shows which seats exist for students to seat at. - The instance variable students Sitting is the 2D array that contains the students when they are seating. To complete complete this method do: 1. Read the input file to create the seating arrangement for 2D boolean array seatingLocations 2. Initialize the 2D Student array students Sitting to be of the same size. The input file format is as follows: - The first line contains an integer representing the number of rows in the classroom, say r - The second line contains an integer representing the number of columns in the classroom, say c - Number of r lines, each containing c true or false values (true denotes a seat exists, false denotes that there is no seat at that position in the classroom) Use the Stdln library to read from a file: - stdin.setfile(filename) opens a file to be read - stdin. readint() reads the next integer value from the opened file (weather the value is in the current line or in the next line) - stdin.readboolean() read the next boolean value from the opened file (weather the value is in the current line or in the next line) This method DOES NOT seat students on the seats. This is the expected output for students1.in and seating 1 in: 3. seatStudents This method simulates students taking their seats in the Kindergarten classroom. Assume that the students are currently in studentsinLine and that there are enough available seats to seat all students in the line. - This method will populate the 2D array students Sitting. - Students will be seated starting at the first row, first seat (studentsSitting[0][0] if a seat is present at that location). Once the first row is filled, continues into the next row. - It uses the seatingLocations array to determine if a seat exists at a location. - A student can sit at seat students Sitting[i][j] only if seatingLocations[i][i] is true (seat exists). - Then seat the first student in studentsinLine and moves on to the second, third and so on. NOTES - A student is only in one place (musical chairs, seating chairs, or in line) at a time. At the end of this method studentsinLine is empty. - This method depends on enterClassroom and setupSeats. Test those individually on the driver prior to testing this method. 4. insertMusicalChairs This method represents students preparing to start musical chairs! Assume that the students are in studentssitting. - Imagine this as a circle of chairs. - This method will take students from the students Sitting array and add them to the musicalChairs circular linked list. - Students are to be inserted at the end of the linked list by traversing row-wise, then column-wise in the studentsSitting array. - REMEMBER: the pointer to a circular linked list points to the LAST item in the list, and that item points to the front. NOTES - At the end of this method studentsinLine and students Sitting have no students. - This method depends on the previous methods from parts 1,2 , and 3 . Test those before testing insertMusicalChairs. This is the expected output using students1.in and seating 1 in: playMusicalChairs This method simulates the game of musical chairs! - This method is implemented for you. - Write the following methods to make it work. This is the expected output using students1.in and seating 1 .in: 5. moveStudentFromChairs ToLine This method simulates a student being eliminated during the musical chairs game. - This method randomly chooses a student to be eliminated from musicalChairs. - Use StdRandom.uniform (x), where x is the number of students currently in musicalChairs, to get a number n between 0 (inclusive) and x (exclusive). - Remove the student at position n from musicalChairs. Position 0 is the first student. - Notice that the driver is setting a seed for the random number generator. The seed value is 2022. - Once the student is removed from the chairs call insertByHeight to insert the student into studentsInLine. - Watch this video for more information. Note: this method depends on insertMusicalChairs and insertByHeight. Students in Musical Chairs: M. Brow N. Dyer P. Ferg D. Harb M. Hawk C. Heat J. Keer G. Mata C. McLa D. Mont P. Reis W. Ryde N. Schn S. Sink F. Wolf - POINTS TO FRONT 6. insertByHeight Each student that is eliminated from the musical chairs game is put back in the line (studentsinLine linked list) by height order. - The eliminated student is given as as parameter - Iterate through studentsinLine searching for a student that is taller than the eliminated student. You will have 3 cases: - eliminated student is the shortest, insert at the front of the list. - eliminated student is the tallest, insert at the end of the list. - eliminated student is not the shortest or tallest, insert in the middle of the list. - When inserting into the list, a student will be insert AFTER all students of the same height. - Watch this video for more information. - This method is called from moveStudentFromChairs ToLine() to insert a student that was eliminated from the musical chairs game. In the driver, you can test this method independently using moveStudentFromChairsToLine. 7. eliminateLosingStudents This method eliminates all students from musicalChairs except the winner. 1. Obtain the number of students currently in the musicalChairs, call it size. 2. Repeatedly call moveRandom StudentFromChairs ToLine with the size until one player is left. 3. Don't forget to update size after each call as the number of students in the chairs decreases by 1. At the end of this method one student will remain in musicalChairs, this is the winner! The winner doesn't leave the musical chairs to enter the line. All other students will be standing on the line in ascending order by height. NOTE: this method depends on all previous methods. 8. seatMusicalChairsWinner This method will seat the winner of the musical chairs game! After the game of playMusicalChairs, you must seat the winner first! - If musical musicalChairs contains only one node (that is, the winner), delete the node. The list will be empty. - Insert the student object into the first empty spot in the classroom (seatingStudents). Implementation Notes - YOU MAY only update the methods with the WRITE YOUR CODE HERE line. - DO NOT add any instance variables to the Classroom class. - DO NOT add any public methods to the Classroom class. - YOU MAY add private methods to the Classroom class. - DO NOT use System exit()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems Uropean Conference Adbis 2020 Lyon France August 25 27 2020 Proceedings Lncs 12245

Authors: Jerome Darmont ,Boris Novikov ,Robert Wrembel

1st Edition

3030548317, 978-3030548315

More Books

Students also viewed these Databases questions