Exam 1 for Object Oriented Programming II | COMP 202, Exams of Computer Science

Material Type: Exam; Class: PRINCIPLES OF OBJECT-ORIENTED PROGRAMMING II; Subject: Computer Science; University: Rice University; Term: Fall 2008;

Typology: Exams

Pre 2010

Uploaded on 08/19/2009

koofers-user-q0s-1
koofers-user-q0s-1 🇺🇸

5

(1)

10 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP 202 – Prin ciples of Object Or iented Programming II EXAM #1
Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________
Oct. 20, 2008 1 of 16
Instructions
1. This is an open-notes, open-book, open-computer, open-Internet exam.
2. You have 1.5 hours to complete th is exam.
3. Please make sure you have all 16 pages of this exam.
4. You will not be penalized on trivial syntax errors, such as a missing par enthesis. Multip le errors or errors
that lead to ambiguous code will hav e points deducted, however.
5. In all of the ques tions, feel free to write additional he lper methods to get the job done.
6. The emphasis is on correctness of th e code, not efficiency or on simply generating the right result.
7. You are free to use any code that wa s given to you in the lectures and labs .
8. You do not have to write generic code in this exam.
Please write and s ign the Rice Honor Pledge here:
1a
1b
1c
2
Total
/40 pts
/10 pts
/10 pts
/40 pts
/100 pts
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Exam 1 for Object Oriented Programming II | COMP 202 and more Exams Computer Science in PDF only on Docsity!

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________

Instructions

1. This is an open-notes, open-book, open-computer, open-Internet exam.

2. You have 1.5 hours to complete this exam.

3. Please make sure you have all 16 pages of this exam.

4. You will not be penalized on trivial syntax errors, such as a missing parenthesis. Multiple errors or errors

that lead to ambiguous code will have points deducted, however.

5. In all of the questions, feel free to write additional helper methods to get the job done.

6. The emphasis is on correctness of the code, not efficiency or on simply generating the right result.

7. You are free to use any code that was given to you in the lectures and labs.

8. You do not have to write generic code in this exam.

Please write and sign the Rice Honor Pledge here:

1a 1b 1c 2 Total

/40 pts /10 pts /10 pts /40 pts /100 pts

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________

Reference: Immutable List Framework (IList)

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________

Reference: Lazy Mutable List Framework (LRStruct with ALazyEval)

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________

Reference: Binary Tree Framework (BiTree)

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________

// FOR STUDENT TO COMPLETE: add fields and methods here private LRStruct src1; private LRStruct src2; private ILambda op; public LRStruct nextLRS() { return makeLRS(); } /**

  • Pass the two lists to a lambda and put the result in the list.
  • @return a list with the elements that come out of the lambda. */ public LRStruct makeLRS() { return (LRStruct)src1.execute(new IAlgo() { public Object emptyCase(LRStruct host, Object... inp) { return src2.execute(new IAlgo() { public Object emptyCase(LRStruct host, Object... inp) { return new LRStruct(); // both empty } public Object nonEmptyCase(LRStruct host, Object... inp) { return makeLazyLRS(src2.removeFront()); } }); } public Object nonEmptyCase(LRStruct host, Object... inp) { return src2.execute(new IAlgo() { public Object emptyCase(LRStruct host, Object... inp) { return makeLazyLRS(src1.removeFront()); } public Object nonEmptyCase(LRStruct host, Object... inp) { return makeLazyLRS(op.apply(src1, src2)); } }); } }); } }

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________

1. Lazy Evaluation :

c) (10 pts) Write a subclass of the LazyCombineEval above called LazyMergeEval and provide an

ILambda that creates a sorted list of Integers, given two sorted input lists of Integers. The lambda

compares the firsts of both lists, removes the one that is less and returns it for the output list.

  • Example:

o lambda: “check which first is less; remove the first that is less and return it”

o list1 = (), list2 = ()  result = ()

o list1 = (1), list2 = ()  result = (1)

o list1 = (1), list2 = (10)  result = (1, 10)

o list1 = (1, 3, 5, …), list2 = (2)  result = (1, 2, 3, 5, …)

o list1 = (1, 3, 5, …), list2 = (2, 4, 6, …)  result = (1, 2, 3, 4, 5, 6, …)

Please insert your code for LazyMergeEval.java below:

package lrs.lazyLRSEvaluators; import fp.; import lrs.; /**

  • Lazily creates a possibly infinite list that, given two sorted LRStructs
  • of Integers, will contain the elements from both LRStructs sorted. If
  • both LRStructs are non-empty, the first elements will be compared, and the
  • one that is less will be removed and returned as element for the output
  • list. */ public class LazyMergeEval extends LazyCombineEval { // FOR STUDENT TO COMPLETE: add fields and methods here public LazyMergeEval(LRStruct src1, LRStruct src2) { super(src1, src2, new ILambda() { public Object apply(Object... param) { LRStruct src1 = (LRStruct)param[0]; LRStruct src2 = (LRStruct)param[1]; Integer i = (Integer)src1.getFirst(); Integer j = (Integer)src2.getFirst(); if (i<j) { return src1.removeFront(); } else { return src2.removeFront(); } } }); } }

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________

2. Binary Trees :

(40 pts) You are to write an IVisitor called BRSTrim that will create an LRStruct containing the leaves

of the BiTree that it visits. The leaves are then removed from the tree.

  • If the tree is empty, the list returned is empty, and the tree remains empty.
  • If the tree is a leaf (i.e. its left and right subtrees are both empty), the list returned contains the single element in the

root of the tree, and the tree becomes empty.

  • If the tree is not a leaf, the list returned contains the list returned from recursing into the left subtree concatenated to

the list returned from recursing into the right subtree, and the tree remains non-empty.

  • Example:

tree= 1

|_ 2

| |_ 3  leaf

| | |_ []

| | |_ []

| |_ 4  leaf

| |_ []

| |_ []

|_ 5

|_ 6  leaf

| |_ []

| |_ []

|_ []

trim, list returned is (3 4 6)

and the tree is changed to

tree = 1

|_ 2  leaf

| |_ []

| |_ []

|_ 5  leaf

|_ []

|_ []

trim, list returned is (2 5)

amd the tree is changed to

tree = 1  leaf

|_ []

|_ []

trim, list returned is (1)

and the tree is changed to

tree = []

trim, the list returned is ()

and the tree remains unchanged:

tree = []

Notes:

a. Think delegation always.

b. There are no if statements needed!

c. Don’t forget about the possibility that the tree may be or become empty.

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________

Please insert your code for BRSTrim.java below:

package brs.visitor; import brs.; import lrs.; /**

  • This visitor returns an LRStruct that contains all the leaves of
  • the BiTree. The nodes that are leaves in the BiTree are then removed
  • from the BiTree. **/ public class BRSTrim implements IVisitor { public static final BRSTrim Singleton = new BRSTrim(); private BRSTrim() {} // STUDENT TO COMPLETE public Object emptyCase(BiTree host, Object... nu) { return new LRStruct(); } public Object nonEmptyCase(BiTree outerHost, Object... nu) { return outerHost.execute(new IVisitor() { public Object emptyCase(BiTree host, Object... inp) { return inp[0]; } public Object nonEmptyCase(final BiTree host, final Object... inp) { return host.getLeftSubTree().execute(new IVisitor() { public Object emptyCase(BiTree h, Object... nu) { // left is empty return host.getRightSubTree().execute(new IVisitor() { public Object emptyCase(BiTree h, Object... nu) { // right is empty ==> this is a leaf LRStruct l = (LRStruct)inp[0]; Object o = host.remRoot(); l.insertFront(o); return l; } public Object nonEmptyCase(BiTree h, Object... nu) { return interiorNodeCase(host, inp); } }); } public Object nonEmptyCase(BiTree h, Object... nu) { return interiorNodeCase(host, inp); } }); } Object interiorNodeCase(BiTree host, Object... inp) { // not a leaf return host.getLeftSubTree().execute(this, host.getRightSubTree().execute(this, inp)); } }, new LRStruct()); } }

Rice University - Instructor: Ricken & Wong NAME & ID#: _______________________________