



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!




Cornell net id ________________________ Name ________________________________ Section day __________________________ Section time ___________________________
This 90-minute exam has 6 questions (numbered 0..5) worth a total of 100 points. Spend a few min- utes looking at all 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 (15 points). Write the body of the following function. Here are the ground rules:
Cornell net id ________________________ Name ________________________________ Section day __________________________ Section time ___________________________ Question 2 (30 points): This question and the next deals with classes that maintain information about bees. On the bottom right of this page is a class Bee, with some parts of it not yet completed. Yes, we are going to have tags to distinguish bees, as strange as that may sound. Answer the following questions. A. Look at the second constructor, the one at the top of the righthand column. Notice that the new Bee object is supposed to be given a unique tag —in field tag. Your job is to implement the rest of the method body of the second constructor to accomplish this. You can declare a static variable in the class to help you accomplish this task, if you wish. If you do declare a variable, be sure to explain in a comment what it means. B. Complete the body of the first constructor. The body should be a single statement. Writing more than one statement gets you at most half credit. C. Suppose the following function, equals, is to be placed within class Bee. Complete the body of the function. Note that field tag should not be tested. /** = obj is an object of class Bee and has the same month and year of birth as this Bee. / public boolean equals(Object obj) { } /* Constructor: A bee with birth month m and birth year y. The bee is given a unique tag. Precondition: 1 <= m <= 12. / public Bee( int m, int y) { month= m; year= y; } /* = this Bee's year of birth / public int getYOB() { return year; } /* = this Bee's month of birth / public int getMOB() { return month; } /* = this Bee's tag (-1 if none)= / public int getTag() { return tag; } } /* An instance represents a Bee / public class Bee { private int month; // month of birth private int year; // year of birth private int tag; // unique number >= 0. // No two Bees have the // same tag. /* Constructor: A bee with birth month 1 and birth year 0. The bee is given a unique tag. */ public Bee() { } //continued in next column
Question 4. (15 points). (a) Write the four steps in evaluating a function call. (b) To the right is part of class Bee of a previous question, with just enough of it for you to answer this question and an additional function isOlder. Suppose variable b1 and b2 contain the names of Bee objects, as shown here: Draw the frame for the following call, including its scope box. b1.isOlder(b2) /** An instance represents a Bee / public class Bee { private int month; // month of birth private int year; // year of birth /* Constructor: A bee with birth month 1 and birth year 0. The bee is given a unique tag. / public Bee() { … } /* = "this bee is older than b". */ public boolean isOlder(Bee b) { boolean x= year < b.year; boolean y= year == b.year && month < b.month; return x || y; } b1 (^) a5 b2 (^) a
Question 5 (20 points). (a) Consider this function. public void f( int p) { p= p + 1; if (p < 0) { int k= p; p= p + k; } } When are parameter p and local variable k created? (b) What is an argument? (c) Consider the following statement, where class Bee is given on page 2 and class MaleBee on page 3: Bee b= new MaleBee(5, 2007, bmom); What are the apparent and real classes of b after execution of this statement? (d) Suppose b using the assignment in part (c). Indicate which of the following three expressions are syn- tactically legal or illegal. For an illegal one, explain why it is illegal; then, if is possible to change it so that it is legal (and returns the obviously desired value), do so; if it is not possible, explain why. b.getTag() b.getMother() b.getFather()