



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Gries; Class: Introduction to Computing Using Java; Subject: Computer Science; University: Cornell University; Term: Spring 2009;
Typology: Exams
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Section day ________________________ Section time _______________________
This 90-minute exam has 6 questions (numbered 0..5) worth a total of 100 points. Spend a few minutes looking at all questions before answering any. Budget your time wisely. Use the back of the pages, if you need more space. We have a stapler at the front of the room, so you can tear the pages apart. Question 0 (2 points). Write your netid and your name, legibly, at the top of each page (Hint: do it now). Question 1 ( 18 points) Recursion. Write function hasName, defined below in class Rhino —we put only the fields and methods needed for this question. Do not use loops; you must use recursion. Do not write any other methods. To the right is a possible tree for a rhino, showing the rhino (r1) and its known ancestors. Note that hasName is not a static function; it appears in every object of class Rhino. public class Rhino { private Rhino father; // this Rhino’s father ( null if unknown) private Rhino mother; // this Rhino’s mother ( null if unknown) private String name; // this Rhino’s name /** = “this rhino or one of its ancestors has name n. */ public boolean hasName(String n) { } father mother r r5 r mother r3 r father
Section day ________________________ Section time _______________________ Question 2 (20 points). Exceptions. (a) What is the output of the call P3.foo(), given the following definition for class P3? class P3 { private static int calls = 0; public static void foo() { try { bar(); System.out.println(“first bar done”); bar(); } catch (Exception e) { System.out.println(“Exception!!”); } } private static void bar() throws Exception { if (calls > 0) System.out.println(“Call number “ + calls); else throw new Exception(); } } (b) On the back of the previous page, write a subclass BadNumberException of class Exception (which is a subclass of Throwable). It needs the usual two constructors. (c) The specification of the following function has a precondition. (The function calculates the greatest common divisor gcd(b,c) of b and c.) To make it easier to write robust code that calls this function, change the function to throw a BadNumberException (with a suitable detail message) if the precondi- tion is false — first change the specification; then change the body. Use the back of the previous page for your answer. In writing the body, you don’t have to copy the cde shown below, but you do have to show where it goes. You don’t have to understand completely how the body works, but if you are interested, note that it rests on two properties: (1) gcd(b, c) = gcd(c, b), and, for c > b, gcd(b, c) = gcd(b, c-b) be- cause anything that divides b and c also divides b and c-b, and vice versa. /** = the greatest common divisor of x and y. Precondition: x > 0 and y > 0. */ public static int GCD( int x, int y) { int b= x; int c= y; // invariant: gcd(x, y) = gcd(b, c) and b > 0 and c > 0 while (b != c) { if (b < c) c= c – b; else b= b – c; } return b; }
Section day ________________________ Section time _______________________ (b.) Now complete function isMagicSquare, whose specification is given below. Here are some ground rules.
Section day ________________________ Section time _______________________ Question 4 (12 points). Classes. Answer the follow questions in a few sentences: (a) Explain the "Inside out rule". (b) Explain the two uses of this and super in Java. (c) What is meant by "overriding a method"? How does one call an overridden method from within the class that does the overriding?