



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!




This 90-minute exam has 6 questions (numbered 0..5) worth a total of 100 points. Spend a few minutes looking at all the questions before beginning. Use the back of the pages if you need more space. Question 0 (2 points). Fill in your netID and name, legibly, at the top of each page. (Hint: do it now.) The questions in this exam deal with class BigInt, which implements integers of any size. Type int has the range - 2 31 .. 31
, =, or < b. / private int compareAbs(BigInt b) /* = the sum of x and y. Precondition: x and y are non-negative. / public static BigInt add(BigInt x, BigInt y) /* = – x. / public static BigInt negate(BigInt x) /* = index of minimum of b[h..k]. Precondition: h <= k./ public static int min(BigInt[] b, int h, int k) /* Sort array b, using selection sort. */ public static void sort(BigInt[] b) }
Question 1 (15 points). Recursion Function length in BigInt is supposed to calculate the number of digits needed to store an int n without any leading 0’s. For example, the integer 3712 has four (4) digits. Read carefully the specification of the fields in class BigInt to understand how many digits are needed to store the integer 0. Below, write function length recursively —do not use a loop. Remember, it should work for negative as well as positive integers n. /** = number of digits for n, with no leading 0’s */ public static int length( int n) { }
Question 3. (20 points). Selection sort (a) Write procedure BigInt.sort, which is specified below. You must write a selection-sort algo- rithm. (1) First, write the pre- and post-conditions, as pictures, in mathematical notation, or in English. (2) Second, develop the loop invariant from the pre- and post-conditions. (3) Third, develop the repetend, using the four loopy questions. When writing the repetend, in maintaining the invariant, state what has to be done in one or two statements, written in English. Do not say how anything is done; say what is to be done. Don’t forget part (b), below. /** Sort array b, using selection sort */ public static void sort(BigInt[] b) { } (b) In part (a), you wrote a loop with a repetend, and the repetend was written mostly in English. Below, show how to implement those English statements in Java. You can use other methods in class BigInt, so have a look around at that class on page 1 of this exam.
Question 4 (20 points). Using loop invariants Function add has to add two BigInts (represented in arrays) together, using the standard addition algorithm. To remind you how this works, we give an example in the box on the right. For example, 6+4 = 10, so one writes the 0 below the line and writes the “carry”, 1 , at the column to the left of the 6+4 column. Thus, the second-column addition is 1+3+8 = 12, which also produces a carry. You have to complete the code below. It assumes that x, y, and z are BigInt variables and that int arrays x.numb and y.numb contain integers. The code adds x.numb and y.numb to- gether and stores the answer (except for the final carry) in z.numb. Do this in two steps, (a) and (b). (a) The invariant is given in English. Write the first part of the loop invariant using our pictorial represen- tation of arrays. (b) Complete the assignment to variable carry, and, below the invariant, write a single loop ( for or while , whichever you prefer) that performs the addition. Be careful. Arrays x.numb and y.numb may have different lengths, so don’t try, for example, to reference x.numb[k] if it doesn’t exist. z.numb= new int [Math.max(x.numb.length, y.numb.length); carry= ; // invariant: z.numb[0..k-1] is the sum of x.numb[0..k-1] and y.numb[0..k-1], // except that the carry into position k is in variable carry (carry is 0 if k is 0). // postcondition: z.numb contains the sum of x.numb and y.numb, // except that the carry for the final position is in variable carry. 1 1