



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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 (15 points) Recursion. By repeatedly summing the digits of a positive integer, one can re- duce the integer to a single digit, in the range 1..9. For example, 56 reduces to 11, which reduces to 2. Similarly, 99934 reduces to 34, which reduces to 7. And 999999999999992 reduces to 128, which re- duces to 11, which reduces to 2. Because addition is symmetric (b+c = c+b) and associative ((b+c)+d = (b + (c+d)), it doesn’t matter what order the summing is done. For example, we can reduce 998 is several ways, some of which are: 998 – > 99 + 8 – > 18 + 8 – > 26 – > 8 998 – > 99 + 8 – > 18 + 8 – > 9 + 8 – > 8 998 – > 99 + 8 – > 107 – > 10 + 7 – > 1 + 7 – > 8 998 – > 99 + 8 – > 107 – > 10 + 7 – > 17 – > 8 Below, write a function that reduces its parameter n to a single digit and returns that digit. You have to (1) specify the function properly, (2) write the function header, and (3) write the function body. The function body should use recursion. It may not use a loop.
Section day ________________________ Section time _______________________ Question 2 (15 points). Loops. Below, we want a sequence of statements that calculates the element-wise averages of arrays a and b, where a and b have the same length. For example, if a = {3, 0, 4, 7} and b = {1, 5, 0 , 0}, the resulting array c is {2.0, 2.5, 2.0, 3.5}. Below, we give: (1) a declaration of an array c that is to contain the answer, (2) a postcondition that says that c contains the answer, and (3) a loop invariant. You job is to write the initialization and loop that will store values in array c so that the postcondition is truthified. Your initialization and loop must be developed to use the invariant shown, using the four loopy questions. An answer that does not attempt to use the invariant may receive 0 points. Feel free to draw the postcondition and invariant as pictures-diagrams, as we have been doing in class, if that helps you. // double arrays a and b have the same length. double [] c= new double [a.length]; // Invariant: c[k..c.length-1] contains the element-wise averages of // a[k..a.length-1] and b[k..b.length-1]. // Postcondition: c[0..c.length-1] contains the element-wise averages of // a[0..a.length-1] and b[0..b.length-1].
Section day ________________________ Section time _______________________ Question 4 (28 points) Classes (a) In modular arithmetic, or “arithmetic mod m”, integers are kept in a range 0..(m-1). The value m is called the modulus. For example, minutes are always in the range 0..59 (so m is 60) and hours of the day in the range 0..23 (so m is 24). In modular arithmetic, the numbers wrap around. For example, the integers mod 4 are, in succession, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, … —adding 1 to 3 in mod 4 arithmetic yields 0, adding 2 to 3 in mod 4 arithmetic yields 1, etc. Interestingly enough, (b mod m) + (c mod m) = (b+c mod m) So, to add two integers in modular arithmetic, just add them as normally done and then change the answer to be in the right range. An instance of class Mod, given on the next page, represents an integer in mod m arithmetic. Note the “class invariant” —the restrictions on fields k and m given as comments. These restrictions must be main- tained. Write the bodies of the constructor (note carefully its specification) and functions add and equals. In writ- ing them, you do not have to check that preconditions mentioned in the specs are met. (b) What is meant by “overriding a method”? How does one call an overridden method m(…)from within the class that does the overriding? (c) What are the four kinds of variables in Java, and where is each declared? (c) Does the first statement inside a constructor always have to be a call on some constructor? Explain. If yes, what constructor call is used if it is missing?
Section day ________________________ Section time _______________________ /** An instance is an integer in mod m arithmetic / public class Mod { private int m; // The modulus. m > 1. private int k; // The integer. 0 ≤ k < m. /* Constructor: integer k in mod m arithmetic. Precondition: m > 1 and k ≥ 0./ public Mod( int k, int m) { } /* If this object and r do not have the same modulus, return null; otherwise, return an object that contains the sum of the two mod m integers represented by this object and r. / public Mod add(Mod r) { } /* = “ob is a non-null Mod object with the same modulus and value as this one”. */ public boolean equals(Object ob) { } }