Вы находитесь на странице: 1из 7

CSE 143 Sample Final Exam #3

1. Inheritance and Polymorphism. Consider the following classes (System.out.println has been abbreviated as S.o.pln):
public class Leo extends Don { public void method1() { S.o.pln("Leo 1"); } public void method () { S.o.pln("Leo "); method1(); }

In the table below, indicate in the right-hand column the output produced b the statement in the left-hand column. If the statement produces more than one line of output, indicate the line brea!s with slashes as in "a # b # c" to indicate three lines of output with "a" followed b "b" followed b "c". If the statement causes an error, fill in the right-hand column with the phrase "error" to indicate this. Statement
var1.method1(); var1.method#(); var1.method (); var#.method1(); var#.method#(); var#.method (); var .method1(); var).method1(); var).method#(); var).method (); ((Don) var1).method#(); ((!i"e) var#).method#(); (($aph) var ).method1(); ((Don) var ).method#(); ((Leo) var)).method ();

Output
************************ ************************ ************************ ************************ ************************ ************************ ************************ ************************ ************************ ************************ ************************ ************************ ************************ ************************ ************************

public class !i"e extends Leo { public void method#() { S.o.pln("!i"e #"); super.method#(); } } public class $aph { public void method1() { S.o.pln("$aph 1"); } } public class Don extends $aph { public void method#() { method1(); S.o.pln("Don #"); } }

The following variables are defined:


$aph var1 Leo var# 'b(ect var Don var) % % % % ne& ne& ne& ne& Don(); !i"e(); $aph(); Leo();

$. Inheritance and Comparable Programming. %ou have been as!ed to e&tend a pre-e&isting class +erson that represents a person used as part of an online dating#marriage s stem. The +erson class includes the following constructors and methods:
Constructor/Method
public public public public public public public public Person(Strin, name) Strin, getName() void engageTo(+erson partner) +erson getFiancee() boolean isSingle() -ueue.Strin,/ getPreferences() !ap.Strin,0 1nte,er/ getRankings() Strin, toString()

Description

constructs a person with the given name returns the person's name sets the person to be engaged to the given partner returns the person's fianc(e, or null if none returns true if the person has no fianc(e returns a )ueue of preferred partners returns a map of ran!ings returns a string representation of the person

%ou are to define a new class called Playa that extends this class through inheritance . * +laya should behave li!e a +erson e&cept that it ma!es mischief b allowing itself to be engaged to multiple persons at the same time, !eeping trac! of a collection of all such fianc(es. %ou should provide the same methods as the superclass, as well as the following new behavior.
Constructor/Method public Playa(Strin, name) public int countFiancees() Description

constructs a pla a with the given name returns the number of fianc(es to which this pla a is currentl engaged

The behaviors related to preferences and ran!ings of potential partners are unaffected b this subclass. +ome of the e&isting behaviors from +erson should behave differentl on +laya ob,ects: -hen the en,a,e2o method is called, the +laya should still retain the e&isting en,a,e2o behavior (because it maintains important internal state), but it should also !eep trac! of a collection of all engagement partners seen so far. .ach partner passed to en,a,e2o should become part of this collection. It should not be possible for the same person to appear twice in this collection. If null is passed to en,a,e2o, our +laya should instead clear its collection of partners to become single again. (* +laya can become engaged to an person(s), not ,ust other +layas.) The ,et3iancee method should return the partner to which the +laya most recentl became engaged (not counting null). This will occur automaticall if the original en,a,e2o behavior from +erson is retained. The isSin,le method should return true onl if the +laya has no partners in its engagement collection.

%ou must also make Playa objects comparable to each other using the Comparable interface. +layas are compared b their number of fianc(es, brea!ing ties b name. In other words, a +laya ob,ect with fewer fianc(es in its partner collection is considered to be "less than" one with more fianc(es in its collection. If two +laya ob,ects have the same number of fianc(es, the one whose name comes first in alphabetical order is considered "less." If the two ob,ects have the same number of fianc(es and the same name, the are considered to be "e)ual." The ma,orit of our grade comes from implementing the correct behavior. /art of our grade also comes from appropriatel utili0ing the behavior ou have inherited from the superclass and not re-implementing behavior that alread wor!s properl in the superclass.

1. Linked List Programming. -rite a method expand that could be added to the Lin"ed1ntList class from lecture and section. The method accepts an integer f as a parameter and replaces ever value i with f copies of the value (i / f). +uppose a Lin"ed1ntList variable list stores the following values:
4#10 50 160 70 8 0 #9

The call list.expand( ); would change the list to store the following elements:
4:0 :0 :0 #0 #0 #0 60 60 60 70 70 70 810 810 810 170 170 179

If an element of the original list is not evenl divisible b f, as with 2 and 1$ above, the resulting list should truncate an fractional component (as is done naturall b integer division). If the parameter value passed is 1, the list is unchanged. If it is 3 or negative, the list should become empt . 4or full credit, our solution must run in 5(N) time where N is the length of the list. %ou ma not call an methods of the lin!ed list class to solve this problem, and ou ma not use an au&iliar data structure to solve this problem (such as an arra , ;rrayList, -ueue, Strin,, etc). *ssume that ou are using the Lin"ed1ntList and List<ode class as defined in lecture and section:
public class Lin"ed1ntList { private List<ode =ront;
methods

} public class List<ode { public int data; public List<ode next; >> data stored in this node >> a lin" to the next node in the list

public List<ode() { ... } public List<ode(int data) { ... } public List<ode(int data0 List<ode next) { ... }

6. Searching and Sorting. a! +uppose we are performing a binary search on a sorted arra called numbers initiali0ed as follows:
// index 0 int49 numbers % {8#0 1 70 2 10 3 :0 4 5 6 7 ?0 1@0 1?0 #50 8 9 10 11 12 13 10 )70 6#0 @50 560 ??};

// search for the value 5 int index % binarySearch(numbers0 5);

-rite the inde&es of the elements that would be e&amined b the binar search (the mid values in our algorithm's code) and write the value that would be returned from the search. *ssume that we are using the binar search algorithm shown in lecture and section. Inde&es e&amined: 8alue 9eturned: 77777777777777777777777777777777777777777777777777777777777 77777777777777777777777777

b! -rite the state of the elements of the arra below after each of the first 1 passes of the outermost loop of the selection sort algorithm.
int49 numbers % {@ 0 ?0 )60 :#0 #:0 150 6)0 selectionSort(numbers); @};

c! Trace the complete e&ecution of the merge sort algorithm when called on the arra below, similarl to the e&ample trace of merge sort shown in the lecture slides. +how the sub-arra s that are created b the algorithm and show the merging of sub-arra s into larger sorted arra s.
int49 numbers % {@ 0 ?0 )60 :#0 #:0 150 6)0 mer,eSort(numbers); @};

:. "inary Search #rees. a! -rite the binar search tree that would result if these elements were added to an empt tree in this order: ;eg, +tewie, /eter, <oe, =ois, >rian, ?uagmire, Cleveland

b! -rite the elements of our tree above in the order the would be visited b each !ind of traversal: /re-order: In-order: /ost-order: 77777777777777777777777777777777777777777777777777777777777777777777 77777777777777777777777777777777777777777777777777777777777777777777 77777777777777777777777777777777777777777777777777777777777777777777

@. "inary #ree Programming. -rite a method nodes;tLevels that could be added to the 1nt2ree class from lecture and section. The method accepts minimum and ma&imum integers as parameters and returns a count of how man elements e&ist in the tree at those levels, inclusive. 9ecall that the root of a tree is at level 1, its children are at level $, their children at level 1, and so on. The table below shows the results of several calls on an 1nt2ree variable tree:
Level 1 #
+----+ | 20 | _____ +----+ _____ / \ +----+ +----+ | 76 | | 11 | _____ +----+ ___ +----+ / \ / +----+ +----+ +----+ | 24 | | 42 | | 98 | +----+ +----+ +----+ / \ \ +----+ +----+ +----+ | -8 | | 80 | | -2 | +----+ +----+ +----+ \ / \ +----+ +----+ +----+ | 57 | | 1 | | 27 | +----+ +----+ +----+

tree

Call tree.nodes;tLevels(#0 )) tree.nodes;tLevels()0 6) tree.nodes;tLevels(10 #) tree.nodes;tLevels( 0 )

Value eturned 5 @

tree.nodes;tLevels(:0 ?) tree.nodes;tLevels(60 ?) tree.nodes;tLevels(10 1)

) 6

4or e&ample, tree.nodes;tLevels()0 6) returns @ because -2, 23, -$, :A, 1, and $A are in that range of levels. %our method should throw an 1lle,al;r,umentAxception if the minimum passed is less than 1 or is greater than the ma&imum passed. It is legal for the minimum and#or ma&imum to be larger than the height of the treeB a tree has 3 nodes at an levels that e&ceed its height. *n empt tree contains 3 nodes at an level. %ou ma define private helper methods to solve this problem, but otherwise ou ma not call an other methods of the class nor create an data structures such as arra s, lists, etc. %our method should not change the structure or contents of the tree being e&amined. 9ecall the 1nt2ree and 1nt2ree<ode classes as shown in lecture and section:
public class 1nt2ree<ode { public int data; >> data stored in this node public 1nt2ree<ode le=t; >> re=erence to le=t subtree public 1nt2ree<ode ri,ht; >> re=erence to ri,ht subtree public 1nt2ree<ode(int data) { ... } public 1nt2ree<ode(int data0 1nt2ree<ode le=t0 1nt2ree<ode ri,ht) {...} } public class 1nt2ree { private 1nt2ree<ode overall$oot; }
methods

A. "inary #ree Programming. -rite a method trim that could be added to the 1nt2ree class from lecture and section. The method accepts minimum#ma&imum integers as parameters and removes from the tree an elements that are not in that range, inclusive. 4or this method, assume that our tree is a binar search tree (>+T) and that its elements are in valid >+T order. %our method should maintain the >+T ordering propert of the tree. 4or e&ample, suppose a variable of t pe 1nt2ree called tree stores the following elements:
+----+ | 50 | _____ +----+ _____ / \ +----+ +----+ | 38 | | 90 | _____ +----+ ___ +----+ / \ / +----+ +----+ +----+ | 14 | | 42 | | 54 | +----+ +----+ +----+ / \ \ +----+ +----+ +----+ | 8 | | 20 | | 72 | +----+ +----+ +----+ \ / \ +----+ +----+ +----+ | 26 | | 61 | | 83 | +----+ +----+ +----+

tree

The table below shows what the state of the tree would be if various trim calls were made. The calls shown are separateB it's not a chain of calls in a row. %ou ma assume that the minimum is less than or e)ual to the ma&imum.
+----+ | 50 | _ +----+ / \ +----+ +----+ | 38 | | 54 | +----+ +----+ / \ \ +----+ +----+ +----+ | 26 | | 42 | | 72 | +----+ +----+ +----+ / +----+ | 61 | +----+

tree.trim(25 !"2#$

tree.trim(5% !&'#$
+----+ | 54 | +----+ \ +----+ | 72 | +----+ / +----+ | 61 | +----+

tree.trim((& !%2#$
+----+ | 38 | _ +----+ / \ +----+ +----+ | 20 | | 42 | +----+ +----+ \ +----+ | 26 | +----+

tree.trim()* !+#$

Cint: The >+T ordering propert is important for solving this problem. If a node's data value is too large or too small to fit within the range, this ma also tell ou something about whether that node's left or right subtree elements can be within the range. Ta!ing advantage of such information ma!es it more feasible to remove the correct nodes. %ou ma define private helper methods to solve this problem, but otherwise ou ma not call an other methods of the class nor create an data structures such as arra s, lists, etc.

Вам также может понравиться