Prfoopr en W18 Withanswers
Prfoopr en W18 Withanswers
Reminder:
Please note name and matri ulation number on ea h sheet.
If you use additional sheets do not forget to note name and matri ulation number on
them too.
Teil 1
Analyse the program given in se tion Ex eption of the handout "Programs and JDK-Do u-
mentation".
The program denes some ex eption lasses and a main lass. It further more denes the
methods fooBar(), foo() and bar().
num: 0
fooBar, aught WriteEx eption
num: 1
fooBar, aught Arithmeti Ex eption
num: 2
fooBar, aught WriteEx eption
num: 3
fooBar, aught PrintEx eption
num: 4
foo, aught ArrayIndexOutOfBoundsEx eption
foo, value: 4
num: 5
fooBar, aught Ex eption
2
Examination OOP W 2018 Prof. Dr. M. Zeller Page 3 (10)
Name: Mat. no: 30. January 2019
Now extend the method fooBar(). The method should print out " fooBar, aught ReadEx-
eption" when method bar() throws a ReadEx eption.
Add the ne essary at h lause to the program:
The at h lause must go in between at hing the PrintEx eption and the WriteEx eption.
try {
bar ( i ) ;
}
at h ( PrintEx eption pre ) {
System . out . p r i n t l n ( " fooBar , aught P r i n t E x e p t i o n " ) ;
}
at h ( ReadEx eption r d e ) {
System . out . p r i n t l n ( " fooBar , aught ReadEx eption " ) ;
}
at h ( Wr i t e E x e p t i o n wre ) {
System . out . p r i n t l n ( " fooBar , aught Wr i t e Ex e p t i o n " ) ;
}
at h ( A r i t h m e t i E x e p t i o n ae ) {
System . out . p r i n t l n ( " fooBar , aught A r i t h m e t i E x e p t i o n " ) ;
}
at h ( E x e p t i o n ex ) {
System . out . p r i n t l n ( " fooBar , aught E x e p t i o n " ) ;
}
3
Examination OOP W 2018 Prof. Dr. M. Zeller Page 4 (10)
Name: Mat. no: 30. January 2019
Teil 2
2.1 (16 Points)
Analyse the program given in se tion Constru tors of the handout "Programs and JDK-
Do umentation". Please be aware that the answers to the questions may o upy more or less
room than provided. All lasse are dened in the pa kage jobs
What is the output of the program? Fill in the output step by step.
Output of the program when line 11 O upation aProfession = new O upation(); is exe-
uted?
reating a profession: 0
Output of the program when line 13 aProfession . print (); is exe uted?
working: best boy/girl, 3
Output of the program when line 15 Engineer anEngineer = new Engineer(); is exe uted?
reating a profession: 0
Creating an engineer, 3
Output of the program when line 19 anEngineer. onstru t(); is exe uted?
working: engineer, 3
engineer is onstru ting:
Output of the program when line 25 aCvEngineer. onstru t(); is exe uted?
road planner, o upation in: 7
road planner is onstru ting:
Output of the program when line 29 Pianist aPianist = new Pianist(); is exe uted?
reating a profession: 0
4
Examination OOP W 2018 Prof. Dr. M. Zeller Page 5 (10)
Name: Mat. no: 30. January 2019
2.2 (3 Points)
Now onsider the lines 34 36. In the following always one of these lines is a tivated the
others remain ommented out.
3 publi stati void main ( S t r i n g [ ℄ a r g s ) {
4 :
34 // aPerson . p r i n t M a i l A d d r ( ) ;
35 // a P i a n i s t . setMailAddr (" gould . . . " ) ;
36 // S t r i n g anAddress = a P i a n i s t . mailAddr ;
37 }
Some of these lines may not ompile. Indi ate whi h lines are orre t and whi h will be
reje ted by the ompiler. Note: You re eive a point for ea h orre t indi ation; for ea h
wrong indi ation one point is dedu ted. If you ommit an indi ation in a line no point is added
or dedu ted. The minimum s ore is zero points even if you marked less orre t answers than
wrong ones.
A tivated line orre t illegal
A tivated line orre t illegal
aPerson.printMailAddr(); X
aPianist.setMailAddr("gould . . ."); X
String anAddress = aPianist.mailAddr; X
2.3 (4 Points)
Dene a new member provider of Type String in lass Cal ulator. All lasses of the
pa kage jobs should be able to obtain the value of this member. All lasses of the pa kage
jobs should be able to hange the value of this member. Classes outside the pa kage are not
allowed to hange the value of provider. The lass Cal ulator should ensure that the value
of the string provider ontains more than three and less than 20 hara ters. Hint: Consider
method length() of lass String.
publi lass C a l u l a t o r {
private S t r i n g p r o v i d e r = " ";
Cal ulator () {
System . out . p r i n t l n ( " making a new al ulator " );
}
String getProvider () {
return
provider ;
}
5
Examination OOP W 2018 Prof. Dr. M. Zeller Page 6 (10)
Name: Mat. no: 30. January 2019
Part 3
3.1 (5 Points)
Analyse the program given in se tion Network of the handout "Programs and JDK-Do u-
mentation".
What data stru ture results in the program after after line 21 tn_1.rightSide = tn_3; is
exe uted? Complete the given sket h by drawing the missing obje ts, the missing referen es
and the values stored in the member-variable num.
Object of type NumTree
num: -4
leftSide:
rightSide:
center num: 14
num: -53
: leftSide: num: 21
leftSide:
rightSide: leftSide:
num: 45 rightSide:
rightSide:
leftSide:
Object of type TreeNode
rightSide:
3.2 (5 Points)
The method printNegative() of lass TreeNode an traverse ea h data stru ture built from
obje t of lass TreeNode provided the data stru ture does not ontain any y les. For ea h
obje t of type TreeNode it prints out the value of the member num if and only if this value is
negative (see sample output below). Hint: Implement this method re ursivly.
Sample output: TreeNode: -4
TreeNode: -53
6
Examination OOP W 2018 Prof. Dr. M. Zeller Page 7 (10)
Name: Mat. no: 30. January 2019
Teil 4
The lass Router is given on page 10 of the listing in se tion Colle tions and IO
. The
method print() of lass Router prints the data stored in an obje t of lass Router to the
s reen (see the given sample output below). It prints out the member ma Address, ports
and stateCode.
The method printRouter() in lass FileIoTest iterates over all elements of the list routerList.
For ea h element it alls the method print().
Complete the method printRouter().
void printRouter () {
System . out . p r i n t l n ( " R o u t e r l i s t : " ) ;
for ( Router aRouter : r o u t e r L i s t ) {
aRouter . p r i n t ( ) ;
}
System . out . p r i n t l n ( "−−−−−−−−−−−−−−−−−−−−" ) ;
}
7
Examination OOP W 2018 Prof. Dr. M. Zeller Page 8 (10)
Name: Mat. no: 30. January 2019
4.3 File IO
The program writes obje ts of type Router to an DataOutputStream and reads data from an
DataInputStream in order to restore the written obje ts. The program uses three methods
for writing and three methods for reading. The methods reside in lass RouterIO.
4.3.1 (5 Points)
4.3.2 (4 Points)
The method saveRouterToStream() writes one obje t of type router to the stream. It writes
the members ma Address, stateCode, and ports. If an error o urs during writing the
methods throws an IOEx eption (i. e. the method does not at h any ex eption).
Complete the method saveRouterToStream().
void saveRouterToStream ( Router aRouter , DataOutputStream dos ) throws IOEx ept
dos . writeUTF ( aRouter . ma Address ) ;
dos . w r i t e B y t e ( aRouter . s t a t e C o d e ) ;
dos . w r i t e I n t ( aRouter . p o r t s ) ;
}
4.3.3 (5 Points)
8
Examination OOP W 2018 Prof. Dr. M. Zeller Page 9 (10)
Name: Mat. no: 30. January 2019
4.3.4 (6 Points)
The method readRouterFromStream() reads the data of one obje t of type router from the
stream dis. It reates an obje t and reads and assigns the values saved by the method
saveRouterToStream(). Lastly it returns the newly reated obje t. If an error o urs during
reading the methods throws an IOEx eption (i. e. the method does not at h any ex eption).
Complete the method readRouterFromStream().
Router readRouterFromStream ( DataInputStream d i s ) throws IOEx eption {
Router aRouter = new Router ( ) ;
aRouter . ma Address = d i s . readUTF ( ) ;
aRouter . s t a t e C o d e = d i s . readByte ( ) ;
aRouter . p o r t s = d i s . r e a d I n t ( ) ;
return aRouter ;
}
9
Examination OOP W 2018 Prof. Dr. M. Zeller Page 10 (10)
Name: Mat. no: 30. January 2019
4.3.5 (6 Points)
The member routerList of lass FileIoTest is of type ArrayList. The list stores obje ts
of type Router. The initial apa ity of the list should be 20.
Complete line 3 of the lass FileIoTest:
ArrayList<Router> routerList = new ArrayList<>(20);
What is the output of the method runContainerTest()? Note: There might be more dottet
lines than reuqired.
10