



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
Information about a 90-minute exam for the cs 1110 course at cornell university. It includes instructions for the exam, as well as several programming questions that must be solved using recursion and the provided java class definition. The document also includes a class definition for phd and its subclass faculty, which can be used to maintain information about intellectual genealogy and faculty members, respectively.
Typology: Exams
1 / 5
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 the information, legibly, at the top of each page. (Hint: do it now.) Question 1 ( 20 points). Write the body of recursive function firstC, whose specification and header appear below. In writing it, think of the base cases first —parameter values where the answer is easy to calculate because the value is small. Then handle the other, recursive, case(s). Do not use loops. Use only recursion. One additional restriction, which, incidentally, should help you home in on a simple solution: The only String functions you should use are charAt, length, and substring. /** = number of times the first char of s appears at the beginning of s. Example: firstC("bbbcb$b") = 3. Example: firstC("bcb$bbb") = 1. Precondition: s contains at least one character. */ public static int firstC(String s) { }
Question 2 ( 20 points). Class PhD, below, can be used maintain information about the intellectual gene- alogy of PhDs —about PhD advisors. For example, on the website www.genealogy.ams.org, Gries traces his roots back to Leibniz. Lee’s genealogy is not known that far back because the website does not have the PhD advisor of her intellectual great grandfather, one of the famous founders of AI, Seymour Pappert. We have elided the constructor body to save space. Note that class PhD has no getter methods , and none should be inserted. 2.(a). Explain why one would want to make a class abstract. Then write on the class definition for PhD whatever is necessary to make it abstract. 2.(b) Write the body of function equals to implement its specification. 2.(c). Insert after the declaration of function equals a declaration of an abstract function hasJob(), whose purpose is to return a String that indicates where this PhD works. Don’t forget to specify the func- tion. In the space below this sentence, explain the purpose of making the function abstract. /** An instance maintains information about a PhD. / public class PhD { private String name; // Person's name:
Question 4. (20 points). Part of Gries’s intellectual ancestral tree appears to the right. Each arrow goes from a PhD advisee to an advisor. So, Gries’s PhD advisor was Bauer, and Bauer had two PhD advisors: Bopp and Aumann. If you take much math, you will run across names like Minkowsky and Caratheodory. Each of the boxes represents an object of class PhD, and an arrow represents the contents of either field advisor1 or advisor2 of the object. Write the function that is specified below, which is to be declared in class PhD. It computes (and returns) the total number of intellectual ancestors that a PhD has. For example, for the PhD object for “Gries” and ancestors as shown to the right, the answer is 6. Do not use a loop. Use recursion, in that the function will call the same function (but in other objects). /** = Total number of (known) intellectual ancestral advisors of this PhD. That is: the number of advisors plus the number of the advisors' advisors plus the number of the advisor's advisor's advisors, etc. Precondition: no two people in the ancestral tree share an advisor, and no person in the ancestral tree has advisor1== advisor2 unless both are null.*/ public int ancestors() { } Tietze Bopp Gries Bauer Aumann Caratheodory Minkowsky
Question 5. (18 points) Partial credit for this question cannot be given unless you draw all variables (first) and then actually execute by hand, for example drawing all objects created. Consider the two classes given at the bottom of this page. Suppose the following assignments have been executed (we also give the declarations of the variables being assigned): A a= new B(2); A b= new A(5); A c= b; b.bV= 7 ; b= a; (a) Evaluate the following expressions and write their values to the right of the expressions.
public class A { public int bV; public A ( int n) { bV= n; } public int m() { return 2 * bV; } public static int add(A x, A y) { return x.m() + y.m(); }
public class B extends A { public B( int n) { super (n+1); } public int m() { return bV; }