






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!







Grades for the final will be posted on the CMS as soon as it is graded, hopefully tonight but perhaps tomorrow. Grades for the course will take a few days more. You can look at your final when you return in the fall. HAVE A NICE SUMMER! Please submit all requests for regrades for things other than the final BY 8PM TONIGHT. Use the CMS where possible; email Gries otherwise. You have 2.5 hours to complete the questions in this exam, which are numbered 0..8. Please glance through the whole exam before starting. The exam is worth 100 points. Question 0 (1 point). Print your name and net id at the top of each page. Please make them legible. Question 1 (1 0 points). Matlab. Write Matlab code, with no loops and no recursion, to compute the cumulative sum of n approximations of the Taylor series given below to find the value of ln(6). Do not write a complete function. Just write the sequence of statements and final expression that gives the result. Note: ln is the natural logarithm. You do not need to know what ln or Taylor series mean to solve this problem. ln(1 + x) = x/1 – x 2 /2 + x 3 /3 – x 4 /4 + x 5 /5 – … Question 0. _________ (out of 0 1) Question 1. _________ (out of 1 0 ) Question 2. _________ (out of 1 2 ) Question 3. _________ (out of 1 2 ) Question 4. _________ (out of 12 ) Question 5. _________ (out of 1 2 ) Question 6. _________ (out of 16) Question 7. _________ (out of 15 ) Question 8. _________ (out of 1 0 ) Total ___________ (out of 100)
Question 2 (12 points). Loops. One can sum the digits of a nonnegative integer, like 563, giving 14, and then sum again, giving 5. 5 is called the digit sum of 563. Another example: the digit sum of 74 is calcu- lated in two steps: 74 11 2. When calculating the digit sum, one can do it as follows, always keeping the answer less than 10: (digit sum of 867) = (digit sum of 86) + 7 = (digit sum of 8) + 4 [note: 6 + 7 = 13; we summed 1 and 3 to get 4] = (digit sum of 0) + 3 [note: 8 + 4 = 12; we summed 1 and 2 to get 3] = 3 Complete the program segment below, which stores the digit sum of n in c. The postcondition and in- variant of the loop is given. Your code should be consistent with the invariant —remember the four loopy questions. Any new variables you introduce must be declared in the repetend, not outside the loop. Opera- tions …/10 and …%10 may come in handy. int m= ; int c= ; // invariant: (digit sum of n) = c + (digit sum of m) and 0 <= c < 10 and 0 <= m // Termination: m will decrease at each iteration while ( ) { } // postcondition: (digit sum of n) = c
Question 4 (12 points). On this page are class definitions for classes Sound and Song. On the next page is a class TABand. Execute this call: TABand.recordingSession(); Write the output of println statements directly beneath the println statements, in the space provided (on the next page). If an error would be the result of the statement write ERROR. We suggest that you start by drawing all local variables of method recordingSession. Then, as you execute the sequence, draw all objects created and execute any assignment statement faithfully. You don’t have to do this, but you will have a better chance of getting correct answers if you do. public class Sound { /* Total duration of all instances of Sound / private static double totalDuration= 0.0; private double duration; // duration of sound /* Constructor: new Sound with duration m/ public Sound( double m) { duration= m; totalDuration= totalDuration + duration; } /* = duration of this sound / public double getDuration() { return duration; } /* = the total duration of all sounds / public static double getTotalDuration() { return totalDuration; } /Rreplace sound with new one of duration m / public void overwrite( double m) { totalDuration = totalDuration – duration + m; duration = m; } / Append to this sound a new sound of duration m / public void append( double m) { duration= duration + m; totalDuration= totalDuration + m; } } public class Song extends Sound{ / total number of instances of Song created / private static int numSongs= 0; private String name; // name of song private String author; // author of song /* Constructor: a new Song with name t, author s, and duration m/ public Song(String t, String s, double m) { super(m); name= t; author = s; numSongs= numSongs + 1; } /* = "
public class TABand { public static void recordingSession() { Sound first= new Song("CS100 Blues", new String("Gries"), 6); Song forth= new Song("Help!", "Student", 12); Sound wail= new Sound(30); Song fifth= new Song("I will teach you", new String("Gries"), 10); Sound humming= forth; Song second= (Song) first; humming.overwrite(10); first.append(3.0); System.out.println(first); System.out.println(second); System.out.println(forth); System.out.println(wail); System.out.println(humming); System.out.println("Length of sound: " + Sound.getTotalDuration()); System.out.println("The author is: " + second.getAuthor()); System.out.println("The author is: " + first.getAuthor()); System.out.println("The songs have the same author?" + (second.getAuthor() == fifth.getAuthor())); System.out.println("The songs have the same author?" + (second.getAuthor().equals(fifth.getAuthor()))); } }
Question 6 (16 points). Classes. An online retail store maintains information about the dolls they sell. They classify the dolls as shown in the figure to the right. Class Doll, shown to the right below, is used to represent dolls. Every Doll has a name, price, and seller name (a) A BabyDoll can optionally laugh. Class BabyDoll is defined (partially) below. Your task it to complete the body of the constructor and function equals. (b) After executing the following two statements what will be the apparent and real classes of variable d? [2 points] BabyDoll b= new BabyDoll (“Edeline”, 100, “eToys”, true ); Doll d= b; public class Doll { private String name; // Doll’s name private double price ;// Doll’s price private String seller; // Doll’s seller /** Constructor: Doll with name n price p, and seller s / public Doll(String n, double p, String s) { name= n; price= p; seller= s; } /* = name of doll / public String getName() { return name; } /* = price of the doll / public double getPrice() { return price; } /* = seller’s name / public String getSeller(){ { return seller; } /* = representation of Doll/ public String toString(){ return "Name: " + getName() + ", Price: " + getPrice() + ", Seller: " + getSeller(); } } Doll BabyDoll BarbieDoll public class BabyDoll extends Doll{ boolean canLaugh; // = “doll can laugh” /* Constr: BabyDoll with name n, price p, and seller s. Parameter b indicates whether this doll can laugh / public BabyDoll(String n, double p, String s, boolean b){ } /* = “ ob is an instance of class BabyDoll and has the same values in its fields as this BabyDoll” */ public Boolean equals(BabyDoll ob) { } }
(c) Below, write a definition for class BarbieDoll, which should have the following properties.
Question 8 (10 points). Algorithms. Write algorithm Partition as a function, complete with method header (giving the parameters, for example) and a suitable specification —a precondition and postcondition. You may give the pre- and post-conditions as formulas or as pictures. If you have to swap two variables x and y (say), just write “swap x and y”; you need not write the sequence of three statements to swap them. Requirements. The function must partition array segment b[p..q–1]. Your answer must give a suitable invariant and the loop must be developed from the invariant. Because many of you have not been reading carefully on exams, we give you these hints. After you have finished writing your answer, go back and read the preceding paragraph again and think about the follow- ing.