Final Exam
Name:
Email Address:
TA:
Section:
Good luck.
Question Points
Question 1
Question 2
Question 3
Total
In a future term, 1.00 will be using physical characteristics of students (height and weight)
to decide which tutorial section students are assigned to. Consider the following class to
represent the key for students:
You may recall that a key is associated with a value. In this case, the value object we will
be using is a very simple student class:
You can assume that no two students in the class have the same height and same weight.
(All keys are unique.)
We will be storing the Student objects in a hash table for fast lookup. (We use the
HashMap class which is a kind of hash table.) We will use StudentKey objects as
keys in the hash table, and the Student objects as values. But in order to do so, we first
need to add a couple of methods to the StudentKey class. Write a hashCode method
that returns a hash code equal to the product of height and weight (returns height x
weight). In addition, override the StudentKey equals method such that
(b) 8 points
return false;
Next, complete the code fragment below that will add the previously defined Student
objects to a hash table, given the following:
(c) 7 points
(e) 4 points.
Assume that the hash table we declared above has a fixed size of 10 buckets (or, slots),
and that each bucket uses a LinkedList to hold (key, value) pairs. The put and get
methods use the % (modulo) operator on the keys hash code to determine which bucket
index a (key, value) pair should reside in.
If we define the first bucket index as 0, and the last as 9, which buckets will our students
(key, value) pair reside in?
(f) 5 points
Suppose that after we put the (key, value) pairs in the hash table, Phat Joe goes on a diet
and loses 9 kilograms. At the same time, he grows 1 cm. His key is updated such that
his weight is 91 kg, and his height is 201cm. Assume that no other student in the hash
table has the same updated height and updated weight as Phat.
Note: 201 x 91 = 18291
pkey.height = 201;
pkey.weight = 91;
Object obj = hm.get(pkey);
Answer:
null
(g) 5 points
Now assume that we have created a StudentKey and Student object for all students
in 1.00 and have put the keys into a list (a container that implements the List interface).
We wish to sort them by weight, from lightest to heaviest. Implement a
StudentKeyComparator class that can be used to sort StudentKey objects. We
will use the Comparator interface method compare(). Your method can assume
that obj1 and obj2 are of the StudentKey class. You only need to write the
compare method.
return ((StudentKey)obj1).weight-
((StudentKey)obj2).weight;
}
}
(h) 12 points
Complete the static method below of the StudentKey class which takes as argument
the list of StudentKey objects, sorts the list using Javas built-in
Collections.sort() method , and uses a list iterator to print out the names of the
students in the sorted list.
ListIterator i = skList.listIterator();
while (i.hasNext())
System.out.println(((StudentKey)i.next()).student.name
);
Given a real number r, and two integers, n and m (given n < m), we want to calculate the
expression
(a) 5 points
Please write an iterative method using a for loop that calculates the above expression.
(b) 15 points
if (n == m)
return Math.pow(r, n);
else
return (Math.pow(r, n) + f_recursive(r, n+1, m));
Part A. (5 Points)
Suppose we have numbers between 10 and 100 in a binary search tree and want to search
for the number 56. Circle the letter of the sequence below that could not be the sequence
of nodes examined? (Circle only one answer.)
Part B. (9 Points)
Given a binary parse tree as the following, write down the result of the in-order, and post-
order traversal. (Just write out the series of string tokens.)
* /
4 2 6 3
Draw a binary search tree with the following values in the nodes inserted in order
{5, 2, 3, 9, 6, 1, 10, 4, 19}
Answer: 5
2 9
1 3 6 10
19
4
Part D. (8 Points)
Now draw the most balanced binary search tree possible using the same values inserted in
arbitrary order.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.