You are on page 1of 7

CSE 143 Sample Final Exam #2

1. Inheritance and Polymorphism. Consider the following classes (System.out.println has been abbreviated as S.o.pln):
public class Bay extends Lake { public void method1() { S.o.pln("Bay 1"); super.method (); ! public void method () { S.o.pln("Bay "); ! ! public class "ond { public void method () { S.o.pln(""ond "); ! ! public class #cean extends Bay { public void method () { S.o.pln("#cean "); ! ! public class Lake extends "ond { public void method$() { S.o.pln("Lake $"); method (); ! !

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.method (); var .method (); var$.method (); var(.method (); var).method (); var*.method (); var1.method$(); var .method$(); var$.method$(); var(.method$(); var).method$(); var*.method$(); ((#cean) var)).method1(); ((Lake) var$).method$(); ((Lake) var().method1();

Output
++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++ ++++++++++++++++++++++++

The following variables are defined:


Lake var1 % ne& #cean(); "ond var % ne& "ond(); "ond var$ % ne& Lake(); #b'ect var( % ne& Bay(); Lake var) % ne& Bay(); Bay var* % ne& #cean();

((#cean) var1).method1(); ((Bay) var().method1(); ((Lake) var ).method$(); ((#cean) var)).method1(); (("ond) var().method ();

$. Inheritance and Comparable Programming. %ou have been as!ed to e&tend a pre-e&isting class ,ate that represents calendar dates such as 'arch 1(. The ,ate class includes these constructors and methods:
public public public public public public Constructor/Method ,ate(int month- int day) int .et/onth() int .et,ay() void set/onth(int month) void set,ay(int day) int days0n/onth(int month) Description

public void next,ay() public Strin. toStrin.()

constructs a ,ate ob)ect with the given month#da returns the month returns the da sets month to a new value sets da to a new value returns number of da s in the given month (e&amples: * +,- 1, +1- $ $.) advances to ne&t date, wrapping month if needed (+#1( +#$,- 1#+1 $#1- 1$#+1 1#1) returns string version of date, such as "1$213"

%ou are to define a new class called CalendarDate that extends this class through inheritance. It should behave li!e a ,ate e&cept that it should also !eep trac! of the ear. %ou should provide the same methods as the superclass, as well as the following new behavior:
Constructor/Method public 4alendar,ate(int yearint month- int day) public int .et5ear() public void set5ear(int year) Description constructs a 4alendar,ate ob)ect with the given

ear#month#da returns the ear sets ear to a new value

/ome of the e&isting behaviors from ,ate should behave differentl on 4alendar,ate ob)ects: 0hen a 4alendar,ate is printed with toStrin., it should be returned in a ear#month#da format such as " 11321$213". 1ote that the ear comes first. There should be a leading , if necessar if the month and#or da is less a single-digit number. 0hen advancing a 4alendar,ate ob)ect to the ne&t date using next,ay, if this is the last date of the ear (2ecember +1st), ou should wrap the ob)ect to the ne&t ear. 3or e&ample, the ne&t da after 2ecember +1st, $,,( is 4anuar 1st, $,1,.

%ou must also make CalendarDate objects comparable to each other using the Comparable interface. Calendar dates are compared b ear, then b month, then b da . In other words, a 4alendar,ate ob)ect with a smaller ear is considered to be "less than" one with a larger ear. If two ob)ects have the same ear, the one with the lower month is considered "less." If the have the same ear and month, the one with the lower da is considered "less." If the two ob)ects have the same ear, month, and da , the are considered to be "e5ual." %ou ma assume that all ear#month#da values passed to our methods and constructors are valid. %ou should not worr about leap ears for this problem- assume that 3ebruar alwa s has $. da s.

+. Linked List Programming. 0rite a method remove6ll that could be added to the Linked0ntList class from lecture and section. The method should efficientl remove from a sorted list of integers all values appearing in a second sorted list of integers. 3or e&ample, suppose Linked0ntList variables list1 and list refer to the following lists:
list17 81- $- )- 9: list 7 81- - $- (- ):

If the call list1.remove6ll(list ); is made, the lists should store the following values after the call:
list1: list 7 [7] 81- $- (- ):

1otice that all of the values from list1 that appear in list have been removed and list is unchanged. If the call instead had been list .remove6ll(list1); , the lists would have these values afterwards:
list17 list2: 81- $- )- 9: [2, 4]

6oth lists are guaranteed to be in sorted (non-decreasing) order, although there might be duplicates in either list. 6ecause the lists are sorted, ou can solve this problem ver efficientl with a single pass through the data. %our solution is re5uired to run in 7(M 8 N) time where M and N are the lengths of the two lists. 9ecall the Linked0ntList and List;ode classes seen in lecture and section:
public class Linked0ntList { private List;ode <ront; !
methods

public class List;ode { public int data; public List;ode next;


...

22 data stored in this node 22 link to next node in the list

%ou ma not call an new nodes, and ou 6rrayList, =ueue, solve this problem b

methods of our lin!ed list class to solve this problem, ou ma not construct an ma not use an au&iliar data structure to solve this problem (such as an arra , Strin., etc). %ou also ma not change an data fields of the nodes. %ou must rearranging the lin!s of the list.

*. Searching and Sorting. (a /uppose we are performing a binary search on a sorted arra called numbers initiali:ed as follows:
// index 0 int8: numbers % {>11 12 3 4 )- 11- 1?5 6 7 8 9 10 11 12 13 14 3- )$- )9- *1- *(- 9?- ? - ?)- ?3- 3$!;

// search for the value 86 int index % binarySearch(numbers- ?*);

0rite 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: >alue 9eturned: =========================================================== ==========================

(b 0rite the state of the elements of the arra below after each of the first + passes of the outermost loop of the selection sort algorithm.
int8: numbers % {?- )- >3- 1(- 1- >1- >9- $!; 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.
int8: numbers % {?- )- >3- 1(- 1- >1- >9- $!; mer.eSort(numbers);

?. !inary Search "rees. (a 0rite the binar search tree that would result if these elements were added to an empt tree in this order: @isa, 6art, 'arge, Aomer, 'aggie, 3landers, /mithers, 'ilhouse

(b 0rite the elements of our tree above in the order the would be visited b each !ind of traversal: Bre-order: In-order: Bost-order: ==================================================================== ==================================================================== ====================================================================

C. !inary "ree Programming. 0rite a method count/ultiples that could be added to the 0nt@ree class from lecture and section. The method returns a count of the number of multiples of a particular value in the binar tree. Diven a number n, a value m is considered a multiple of n if it can be e&pressed as (k E n) for some integer k. 3or e&ample, suppose that an 0nt@ree variable t stores a reference to the following tree:
A>>>A B * B A>>>A

A>>>A B B A>>>A 2 C 2 C A>>>A A>>>A B ) B B $ B A>>>A A>>>A 2 C 2 C A>>>A A>>>A B ? B B * B A>>>A A>>>A

A>>>A B 3 B A>>>A C C A>>>A B ( B A>>>A 2 C 2 C A>>>A A>>>A B 9 B B 1 B A>>>A A>>>A

The table below shows various calls and the values the should return:
Call t.count/ultiples( ) t.count/ultiples(() t.count/ultiples($) t.count/ultiples(1) Value Returned * $ ) 11 Reason

si& multiples of $: C, $, *, ., C, , three multiples of *: *, ., , five multiples of +: C, (, +, C, , all ten numbers are multiples of 1

%our method should throw an 0lle.al6r.umentDxception if passed the value ,. %ou ma define private helper methods to solve this problem, but otherwise ou ma not call an other methods of the 0nt@ree class nor create an data structures such as arra s, lists, etc. %our method should not change the structure or contents of either of the two trees being compared. 9ecall the 0nt@ree and 0nt@ree;ode classes as shown in lecture and section:
public class 0nt@ree;ode { public int data; 22 data stored in this node public 0nt@ree;ode le<t; 22 re<erence to le<t subtree public 0nt@ree;ode ri.ht; 22 re<erence to ri.ht subtree public 0nt@ree;ode(int data) { ... ! public 0nt@ree;ode(int data- 0nt@ree;ode le<t- 0nt@ree;ode ri.ht) {...!

public class 0nt@ree { private 0nt@ree;ode overallEoot;


methods

F. !inary "ree Programming. 0rite a method matches that could be added to the 0nt@ree class from lecture and section. The method returns a count of the number of nodes in one tree that match nodes in another tree. < match is defined as a pair of nodes that are in the same position in the two trees relative to their overall root and that store the same data. 3or e&ample, suppose 0nt@ree variables t1 and t refer to the following trees (matches underlined):
A>>>A B 3 B A>>>A 2 C 2 C A>>>A A>>>A B ( B B 7 B A>>>A A>>>A 2 C C 2 C C A>>>A A>>>A A>>>A B 0 B B 9 B B B A>>>A A>>>A A>>>A C C A>>>A B ? B A>>>A

t1

A>>>A B 3 B A>>>A 2 C 2 C A>>>A A>>>A B * B B 7 B A>>>A A>>>A 2 2 C 2 2 C A>>>A A>>>A A>>>A B 0 B B 9 B B B A>>>A A>>>A A>>>A C C A>>>A B ? B A>>>A

t2

The calls of t1.matches(t ) and t .matches(t1) would each return (. The overall root of the two trees match (both are +). The nodes at the top of the left subtrees of the overall root do not match (one is * and one is C). The top of the right subtrees of the overall root match (both are F). The ne&t level of the tree has $ matches for the nodes storing , and $ (there are two nodes that each store ( at this level, but the are in different positions relative to the overall root of the tree). The nodes at the lowest level both store ., but the aren;t a match because the are in different positions. %ou ma define private helper methods to solve this problem, but otherwise ou ma not call an other methods of the 0nt@ree class nor create an data structures such as arra s, lists, etc. %our method should not change the structure or contents of either of the two original trees being e&amined.

You might also like