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

The solution to quiz 7 of the cmsc 131 fall 2004 course, focusing on the waitlist class. The waitlist class is implemented as a java public class with a constructor, add method, and tostring method. The constructor initializes an empty student array, the add method expands the array when needed and adds a new student object, and the tostring method returns a string representation of the array's student objects.

Typology: Quizzes

Pre 2010

Uploaded on 02/13/2009

koofers-user-49d
koofers-user-49d 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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; } }