1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Exam Name: Email Address: TA: Section: You have three hours to complete this exam. For coding questions, you do not need to include comments, and you should assume that all necessary files have already been imported. Good luck. Question Points Question 1 Question 2 Question 3 Total 1.00 Fall 2002 Final Exam 1/9 Problem 1. Hashing and Sorting Students (50 Points) 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: public class StudentKey { int height; /* in centimeters */ int weight; /* in kilograms */ public StudentKey(int h, int w) { height = h; weight = w; } } 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: public class Student { String name; public Student(String n) { name = n; } } You can assume that no two students in the class have the same height and same weight. (All keys are unique.) We can create students as follows: Student phat = new Student("Phat Joe"); Student skim = new Student("Slim Kim"); 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 1. if the object passed in is null, it returns false 2. if the object passed is not a StudentKey object, it returns false 3. if the object passed in is a StudentKey object, it only returns true if both the height and weight are the same as those of the this object. Otherwise, it returns false. 1.00 Fall 2002 Final Exam 2/9 (a) 5 points public int hashCode() { return weight * height; } (b) 8 points public boolean equals(Object obj) { if (obj != null && (obj instanceof StudentKey)) { Student s2 = (Student)obj; return weight==s2.weight && height==s2.height; } return false; } Next, complete the code fragment below that will add the previously defined Student objects to a hash table, given the following: name Phat Joe: Slim Kim: height 200 150 weight 100 60 (c) 7 points HashMap hm = new HashMap(); /* put phat & skim into hash table */ hm.put(new StudentKey(200, 100), phat); hm.put(new StudentKey(150, 60), skim); 1.00 Fall 2002 Final Exam 3/9 (d) 4 points What are the hash code values for: Hash code for phat’s key: 20000 Hash code for skim’s key: 9000 (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 key’s 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? Bucket index for phat: 0 Bucket index for skim: 0 (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 Student phat = new Student("Phat Joe"); StudentKey pkey = new StudentKey(200, 100); // put into HashMap ... (line omitted) pkey.height = 201; pkey.weight = 91; Object obj = hm.get(pkey); Which bucket index will the updated key have? 1.00 Fall 2002 Final Exam 4/9 1 What will the value of obj be after the above code fragment executes? 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. public class StudentKeyComparator implements Comparator { public int compare(Object obj1, Object obj2) { /* write your code here to compare by weight */ return ((StudentKey)obj1).weight((StudentKey)obj2).weight; } } (h) 12 points We modify the StudentKey class by adding a reference to its Student object, as follows: public class StudentKey { /* the Student associated with this key */ Student student; } /* the rest is unchanged */ ... Complete the static method below of the StudentKey class which takes as argument the list of StudentKey objects, sorts the list using Java®’s built-in Collections.sort() method , and uses a list iterator to print out the names of the students in the sorted list. 1.00 Fall 2002 Final Exam 5/9 /* * This method sorts skList in order from lightest to * heaviest, and prints the names of the students in order. * * skList is an unsorted list of StudentKey objects. */ static public void sortAndPrintStudents(List skList) { Collections.sort(skList, new StudentKeyComparator()); ListIterator i = skList.listIterator(); while (i.hasNext()) System.out.println(((StudentKey)i.next()).student.name ); } 1.00 Fall 2002 Final Exam 6/9 Problem 2 Iteration and Recursion (20 Points) Given a real number r, and two integers, n and m (given n < m), we want to calculate the expression rn + rn+1 + rn+2 + … + rm-1 + rm . (Note: n and m can be any positive, negative integer or zero, but you can assume that n will always be smaller than m) (a) 5 points Please write an iterative method using a for loop that calculates the above expression. public double f_iterative(double r, int n, int m) { //write code double sum = 0.0; for (int i = n; i <= m; i++) sum += Math.pow(r, i); return sum; } (b) 15 points Please write a recursive method to do the same thing. public double f_recursive(double r, int n, int m) { //write code if (n == m) return Math.pow(r, n); else return (Math.pow(r, n) + f_recursive(r, n+1, m)); } 1.00 Fall 2002 Final Exam 7/9 Problem 3 Binary Search Tree (30 Points) 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.) a. 20, 78, 64, 56 b. 45, 98, 77, 50, 53, 56 c. 25, 35, 45, 59, 55, 56 d. 65, 24, 34, 68, 54, 56 e. 80, 20, 30, 60, 50, 56 Part B. (9 Points) Given a binary parse tree as the following, write down the result of the in-order, and postorder traversal. (Just write out the series of string tokens.) + * 4 / 2 6 In-order Traversal: (Answer) 4*2+ 6/3 Post-order Traversal: (Answer) 4 2 * 6 3 / + Numerical Result of evaluating the parse tree = 1.00 Fall 2002 Final Exam 8/9 Answer:10 3 Part C. (8 Points) Draw a binary search tree with the following values in the nodes inserted in order {5, 2, 3, 9, 6, 1, 10, 4, 19} 5 Answer: 2 1 9 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. Answer: There are several possible answers for this part. One of them is the same as Part C. 1.00 Fall 2002 Final Exam 9/9 Java® is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.