CS11 Fall 003 Prelim Solutios ad Gradig Guide Problem 1: (a) obj = obj1; ILLEGAL because type of referece must always be a supertype of type of object (b) obj3 = obj1; ILLEGAL because type of referece must always be a supertype of type of object (c) obj3 = obj; ILLEGAL because type of referece must always be a supertype of type of object (d) I1 b = obj3; LEGAL because C3 is a subclass of C1 which implemets I1 (e) I c = obj1; ILLEGAL because type of referece must always be a supertype of type of object Gradig (total 5 poits): For each part -1 : wrog coclusio or reaso
Problem (a): abstract class Exp { abstract it eval(); class BiExp exteds Exp { protected char op; protected Exp left; protected Exp right; public BiExp(char op, Exp l, Exp r) { this.op = op; this.left = l; this.right = r; public it eval() { switch(op) { case '+': retur left.eval() + right.eval(); case '*': retur left.eval() * right.eval(); default: System.out.pritl("ERROR: Ukow op"); retur -1; public char get() { retur op; public Exp getleft() { retur left; public Exp getright() { retur right; class NumExp exteds Exp { protected it ; public NumExp(it ) { this. = ; public it get() { retur ; public it eval() { retur ; Gradig (total 10 poits): The solutio for this part would vary widely. But at a miimum, a correct solutio must have all the class defiitios with variable declaratios, costructors ad getter methods. Setter methods are ot required. -7 : o separate class for umbers ad biary operators -4 : icorrect derivatio of classes (e.g. NumExp should ot be a subclass of BiExp) -3 : BiExp class stores itegers -3 : NumExp class stores operators -3 : o costructor for BiExp for directly settig left, right childre -3 : ot eough getter methods
Problem (b): public static it eval(exp root) { if (root==ull) { System.out.pritl("ERROR: Tree ot iitialized"); retur -1; retur root.eval(); Gradig (total 10 poits): This part would greatly deped o the solutio for part (a). At a miimum, it should implemet a recursive method that evaluates the tree passed. - : o error checkig for root == ull -3 : does ot work if root is just a NumExp ode -5 : illegal dowcast if eval() implemeted exterally ad Exp objects ot checked for type before dowcastig -3 : returs wrog result - : has ay sort of parsig code (this problem does ot require parsig expressios)
Problem 3(a):, log,,,! (i icreasig order of asymptotic complexity) Gradig (total 7 poits): - : ot smallest - :! ot largest - : smaller tha - : log smaller tha - : smaller tha, log, or - : wrote i reverse order Problem 3(b): = oe valid witess pair: ( 1,0) TRUE: O( 3 ) FALSE: 3 = O( ) Proof: Assume O( ) 3 =. Therefore there exists a witess pair ( ) 3 c. for all. I other words: 0 c, 0 such that 3 c 0 3 But the limit (as + ) is = +. Therefore, it is ot possible to have a costat upper 3 boud o. This implies our iitial assumptio of the existece of a witess pair was false. 3 = O is also false. Therefore, the statemet ( ) Gradig (total 8 poits): -4 : first statemet cocluded FALSE - : first statemet cocluded TRUE but ivalid witess pair -4 : secod statemet cocluded TRUE - : secod statemet cocluded FALSE but o relevat argumet (iformal good eough)
Problem 3(c): No. Here is a couter example: ( ) g = ( ) O( g( ) ) Let f = ad ( ). We ca easily show that (, 0). Now, f ( ) = = 4 ad, ( ) g = f = usig the witess pair By the same process that we used to show that 3 = O( ) is false, we ca prove that 4 = O( ) f ( ) g ( ) is also false. Therefore, if f ( ) = O( g( ) ) it does ot imply that = O( ). Gradig (total 5 poits): -5 : wrog coclusio (aswered yes istead of o) -3 : if couter example (or other proof) ot valid
Problem 4: [Breadth-first] a) ABDCE b) Not uique. Aother possibility: ADBCE [Depth-first] c) ABCED d) Not uique. Aother possibility: ADECB e) Yes. Graph with oe ode (A) or, (A) (B), or a graph that looks like a "liked list" i geeral, amog may other possibilities. Gradig (total 10 poits): poits for each part: a) - if wrog sequece b) - if aswered uique -1 if aswered ot uique but gave wrog sequece c) - if wrog sequece d) - if aswered uique -1 if aswered ot uique but gave wrog sequece e) - if aswered o -1 if aswered yes but gave wrog example
Problem 5: public static boolea Valid(Strig s) { if (s==ull) retur false; retur Valid(s,0,s.legth()-1); public static boolea Valid(Strig s, it low, it high) { if (low > high) retur true; if (low == high) retur false; else retur(s.charat(low) == '(') && (s.charat(high) == ')') && (Valid(s,low+1,high-1)); Gradig (total 15 poits): - : fuctio does ot retur Boolea - : fails if s is ull -5 : does ot work for empty strig -3 : extremely iefficiet (e.g. scas strig from begiig i each iteratio) -7 : does ot work for strigs of odd legth (i.e. either crashes or returs true) - : icorrect use of s.charat(i) -10 : o recursio -3 : bad algorithm -7 : allows ivalid strig -1 : returs true if iput is ull
Problem 6: class Hashley implemets SearchStructure { protected ListCell[] spie; protected it size; private fial it buckets = 10; public Hashley() { spie = ew ListCell[buckets]; for (it i=buckets; i<buckets; i++) spie[i] = ull; public void isert(object o) { it idex = ((Iteger) o).itvalue() % buckets; ListCell l = ew ListCell(o,spie[idex]); spie[idex] = l; ++size; retur; public void delete(object o) { it idex = ((Iteger) o).itvalue() % buckets; ListCell curr = spie[idex]; ListCell prev = ull; while (curr!= ull && ((Comparable) curr.getdatum()).compareto(o)!= 0) { prev = curr; curr = curr.getnext(); if (curr == ull) retur; if (prev == ull) spie[idex] = curr.getnext(); else prev.setnext(curr.getnext()); --size; retur; public boolea search(object o) { it idex = ((Iteger) o).itvalue() % buckets; ListCell curr = spie[idex]; while (curr!= ull) { if (((Comparable) curr.getdatum()).compareto(o) == 0) retur true; curr = curr.getnext(); retur false; public it size() { retur size;
Gradig (total 30 poits): -3 : class header does ot have implemets SearchStructure -5 : spie is ot declared as a array -5 : spie array ot allocated (o ew) before first use -3 : object ot type-casted to Iteger before callig itvalue() - : isert() does ot icremet size -5 : deletio of first ode i a list fails -5 : deletio of itermediate odes fail - : delete() does ot decremet size -3 : objects ot compared correctly -5 : iefficiet search if all lists are traversed to look for a object -3 : tries to call methods o a ull poiter (o checkig i while loops etc.) -3 : does ot keep a size variable - : each idex i spie iitialized to poit to empty ListCell s -5 : fuctio headers do t match iterface