| CIT594 Second Quiz Spring, 2004 |
Name_________________________________________ |
Please read all questions carefully. Answer questions briefly and to the point. You have plenty of room for correct answers. Point values are in parentheses
For the following questions, assume the following class has been defined:
public class Student {
String name;
int studentId;
...other stuff...
}
Student names and academic records are not necessarily unique, but a studentId
uniquely identifies a student. There may be different objects representing the
same student.
For the following questions, write only the code that is asked for; do not include extra code. Assume that your answers will be in an appropriate class or method, and do not write that surrounding class or method. You will lose points for inappropriate code.
equals method to put into the Student
class. public boolean equals( Object x) {
if ( ! (x instanceof Student) ) return false;
return ((Student) x).studentId == studentId;
}
Note the cast from Object to Student!
Set myStudents = new TreeSet();
In order for this to work, Student class?equals)
needs to be added to the Student class? public int compareTo(Object o) throws ClassCastException {
if (o instanceof Student)
return studentId - ((Student)o).studentId;
else
throw new ClassCastException("Not a Student!");
}
Short, equivalent version: public int compareTo(Object o) {
return studentId - ((Student)o).studentId;
}
Note the cast from Object to Student!
Set myOtherStudents = new HashSet(myStudentSorter); In order for this to work, equals)
needs to be added to the Student class? public int hashCode() { (must use only studentId)
return studentId;
}
-2 points off for doing arithmetic; -2 for wrong method signature; -4 points off for using names.
Set myStudents = new TreeSet(myNameComparator);name
field.) You can assume that two Student objects with equal StudentId
fields will also have equal name fields (but equal names do not
guarantee equal student IDs.) Student class?NameComparator
class? public int compare(Object o1, Object o2) {
int x = ((Student)o1).name.compareTo(((Student)o2).name);
if (x != 0) return x; // equal names don't guarantee same student
return ((Student)o1).studentId - ((Student)o2).studentId;
}
You need to compare names first, so that students are sorted by
names. But you still need to compare students id - otherwise, students
with the same names but different id will be stored as one single student.
-2 for wrong method signature, -6 for not comparing names, -6 for not
comparing id. static void print(Set students) {
for (Iterator iter = students.iterator(); iter.hasNext();) {
Student element = (Student) iter.next();
System.out.println(element.name);
}
}
makeName() takes constant
time and generates random strings:
public Vector makeNames(int n) {
Vector names = new Vector();
for (int i = 0; i < n; i++) {
String name = makeName();
if (!names.contains(name)) {
names.add(name);
}
}
return names;
}
(8 pts) Assume the following class has been defined:
public class Tree {
public String value;
private Vector children;
...other stuff...
}
Write a print() method for this class that prints the values
of the tree in preorder, one value per line.
void print() {
System.out.println(value); // OK to use size() and get(i) or elementAt(i) instead
for (Iterator iter = children.iterator(); iter.hasNext();) {
Tree element = (Tree) iter.next();
element.print();
}
}
Graph. (The only
difference between my Tree class and my Graph class
is that the Graph class allows cycles.) Assume that the graph
does contain, somewhere, a "goal node."Preorder: D C G B A F EInorder: G C D A B F EPostorder: Draw the binary tree. |
![]() |
public static int max(int[] array) {
int direction = 1;
int middle = array.length / 2;
int value = array[middle];
for (int i = middle; i >=0 && i < array.length; i += direction) {
if (array[i] > value) {
value = array[i];
direction = -direction;
}
}
return value;
}
O(n) on size of array, not guaranteed to find maximum.