



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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Solutions to problems from cs61b exam 2, including questions on inheritance, lists, and enumerations. Students can use this document to review concepts covered in the exam, such as constructor execution, list optimization, and hash table implementation.
Typology: Exams
1 / 5
This page cannot be seen from the preview
Don't miss anything!
CS61B - Exam 2 March 17, 2003
Problem 0 (1 point): Identification Fill out you name, you neighbor’s names and other information in the grid on the solution sheet.
Problem 1: (4 points) Inheritance
Consider the following example of a program that uses inheritance.
abstract class Pet{ public pet() { System.out.println(“Pet.Pet”); } public void chase (Pet p) { System.out.println(“Pet.chase”); } abstract public void snarl (Pet p); }
class Cat extends Pet { public Cat () { System.out.println(“Cat.Cat”); } public void snarl (Pet p) { System.out.println(“Cat.snarl”); } }
class Dog extends Pet { public Dog() { this(0); System.out.println(“Dog.Dog”); } public Dog (int x) { System.out.println(“Dog.Dog(int)”); } public void chase (Cat c) { System.out.println(“Dog.chase”); } public void snarl (Pet p) { System.out.println(“Dog.snarl”); } }
public class Animals { public static void main (String [] args) { Pet p1 = new Cat(); Pet p2 = new Dog(); Dog d = new Dog(); Cat c = new Cat();
p2.chase(c); // Part B
p2.snarl(p1); // Part C d.chase(c); // Part D } }
Part A: How many times will the Pet constructor be executed in the above main method?
Part B-D: What will each of these lines print? (Your answers may include: “Compile time error” or “runtime exception” if appropriate).
Remember to write your answer on the separate
Problem 2: (6 points) Lists
Consider a simplified version of the Ring class from Lab6, with code in the appendix. You are to add a “get” method to the class that will return the item at a given position. In general, your implementation may run in time O(n) for a list of size n. however, you must optimize for a common usage pattern I which the user calls the get operations in order, starting at the beginning of the list and moving forward by a constant increment; in this case each call to get should run in O(1). For example, given a Ring r of size n, the following loop:
for(int i=0; i < r.size(); i++) { System.out.println(r.get(i)); }
should run in O(n). A loop that runs for high to low may still run in time O(n^2 ). Your solution may add variables to the Ring class and may include a small number of additional statements existing methods.
Your solutions should modify as few existing methods as possible. Overly complex solutions will not receive full credit, and solutions that do not meet the complexity requirements will receive no partial credit.
Part A) What variables, if any, do you wish to add to the Ring class?
Part B) What invariants are there on your new variables, if any? (Your solution should be precise enough that another CS61B student could understand the constraint.)
Part C) What changes to existing methods need to be made? You should tell us what method(s) are being changed, and what code is being added. Ignore repOk for this problem.
Part D) Give an implementation of the get method.
myCurrent += 1; return new Integer(myCurrent); }
private int myCurrent; }
class Mystery implements Enumeration {
Mystery (Enumeration e, int v) { myValue = v; myEnum = e; }
public Object nextElement() { Integer possible = (Integer) myEnum.nextElement(); while(possible.intValue() % nyValue == 0) { possible = (Integer) myEnum.nextElement(); } return possible; } private int myValue; private Enumeration myEnum; }
Part A) What will the following loop print?
int stopAt = 10; Enumeration mystery = new Mystery ( new NumbersFrom(1), 2); while (mystery.hasMoreElements()) {
int next = ((Integer) mystery.nextElement()).intValue(); if (next > stopAt) { break; } System.out.print(next + “ “); }
Part B-D) Define an Enumeration called Primes that will produce prime numbers starting with 2. For example, if Primes were used in place of Mystery in part A, the loop would print the prime numbers less than stopAt.
public class Primes () {
public Primes() { FILL IN FOR PART C } public boolesn hasMoreElements() { return true; }
public Object nextElement() { FILL IN FOR PART D }
/* Internal variables */ FILL IN FOR PART B }