Docsity

Solution to Quiz 7 of CMSC 131 Fall 2004 - WaitList Class - Prof. David M. Mount, Quizzes of Computer Science

Toggle sidebar
bg1
CMSC 131 Fall 2004 Quiz 7 Solution
This is one possible solution.
public class WaitList {
private Student[] array;
/* constructor */
public WaitList() {
array = new Student[0];
}
/* add student โ€“ expand array if needed */
public void add(Student student) {
Student[] newArray = new Student[array.length+1];
for (int i = 0; i < array.length; i++) {
newArray[i] = array[i];
}
newArray[array.length] = new Student(student);
array = newArray;
}
/* toString */
public String toString() {
String result = "";
for (int i = 0; i < array.length; i++) {
result += array[i] + "\n";
}
return result;
}
}

Partial preview of the text

Download Solution to Quiz 7 of CMSC 131 Fall 2004 - WaitList Class - Prof. David M. Mount and more Quizzes Computer Science in PDF only on Docsity!

CMSC 131 Fall 2004 Quiz 7 Solution

This is one possible solution.

public class WaitList { private Student[] array; /* constructor / public WaitList() { array = new Student[0]; } / add student โ€“ expand array if needed / public void add(Student student) { Student[] newArray = new Student[array.length+1]; for (int i = 0; i < array.length; i++) { newArray[i] = array[i]; } newArray[array.length] = new Student(student); array = newArray; } / toString */ public String toString() { String result = ""; for (int i = 0; i < array.length; i++) { result += array[i] + "\n"; } return result; } }