Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS61B Exam 2: Problem Solutions, Exams of Data Structures and Algorithms

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

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

80 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5

Partial preview of the text

Download CS61B Exam 2: Problem Solutions and more Exams Data Structures and Algorithms in PDF only on Docsity!

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 }