0% found this document useful (0 votes)
7 views10 pages

Prfoopr en W18 Withanswers

The document outlines an examination for Object Oriented Programming at the University of Applied Science Ravensburg-Weingarten, scheduled for January 30, 2019. It includes various parts with questions related to exception handling, constructors, data structures, and file I/O operations in Java. The exam consists of multiple sections requiring analysis of provided programs and implementation of specific methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Prfoopr en W18 Withanswers

The document outlines an examination for Object Oriented Programming at the University of Applied Science Ravensburg-Weingarten, scheduled for January 30, 2019. It includes various parts with questions related to exception handling, constructors, data structures, and file I/O operations in Java. The exam consists of multiple sections requiring analysis of provided programs and implementation of specific methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Examination in Object Oriented Programming

University of Applied Science Ravensburg-Weingarten


Prof. Dr. M. Zeller

Date, time 30. January 2019, 10:30  12:00 (90 min)


Number of pages 10 pages (in luding title)
Resour es All a epted resour es

Study Program Room


EI C004

Name: Matri ulation number:

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.

leave blank, please:


Part 1 2 3 4 Sum
max. 9 23 10 31 73
Points
Examination OOP W 2018 Prof. Dr. M. Zeller Page 2 (10)
Name: Mat. no: 30. January 2019

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().

1.1 (7 Points) Exception Handling

What ist the output of the program ?


Fill in the missing part of the output. There might be more or less dotted lines than required.

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

1.2 (2 Points) Exception Handling

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 17 anEngineer.print (); is exe uted?


working: 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 21 CivilEngineer aCvEngineer = new CivilEngineer(7); is


exe uted?
reating a profession: 0
Creating an engineer, 3
making a new al ulator
engineer, is a road planner: 3

Output of the program when line 23 aCvEngineer.print(); is exe uted?


road planner, o upation in: 7

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 27 aCvEngineer.planning(); is exe uted?


planning a road

Output of the program when line 29 Pianist aPianist = new Pianist(); is exe uted?
reating a profession: 0

Output of the program when line 31 aPianist .perform(); is exe uted?


working: Glenn G., 3
Glenn G., playing the piano,

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 ;
}

void setProvider ( String aProvider ) {


if ( ( a P r o v i d e r . l e n g t h ( ) > 3 ) && ( a P r o v i d e r . l e n g t h ( ) < 2 0 ) ) {
provider = aProvider ;
}
}
}

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

void printNegative (){


if (num < 0 ) {
System . out . p r i n t l n ( " TreeNode : " + num ) ;
}
if ( l e f t S i d e != null
){
le ft S i de . printNegative ( ) ;
}
if ( rightSide != ){ null
rightSide . printNegative ( ) ;
}
}

6
Examination OOP W 2018 Prof. Dr. M. Zeller Page 7 (10)
Name: Mat. no: 30. January 2019

Teil 4

This part refers to se tion Container and IO


of the handout "Programs and JDK-Do u-
mentation". A program reates obje ts of type Router and stores them in an ArrayList. It
also writes these obje t into a le, reads the data from the le and prints the data to the
s reen.
The method all reateRandomRouters(4) reates obje ts of type Router and stores them
in the ArrayList routerList. Do not worry about this method  the resulting obje ts are
visible through the all of method printRouter(); in line 18; see page 8 of the handout
"Programs and JDK-Do umentation". .

4.1 Print (3 Points)

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.

Router: 1a:..., 12, 104

Complete the method print () at the ellipsis.


void print () {
System . out . p r i n t ( " Router : " + ma Address ) ;
System . out . p r i n t f ( " , %3d" , p o r t s ) ;
System . out . p r i n t f ( " , %3d\n" , s t a t e C o d e ) ;
}

4.2 Print Router (2 Points)

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)

The method getDataOutputStream(String leName) returns an obje t of type DataOutputStream


whi h is onne ted via a Buer to a le named fileName. If an error o urs opening the
streams, the methods throws an IOEx eption (i. e. the method does not at h any ex eption).
Complete the method getDataOutputStream().
DataOutputStream getDataOutputStream ( S t r i n g f i l e N a m e ) throws
IOEx eption {
FileOutputStream f o s = new
FileOutputStream ( fileName ) ;
BufferedOutpu tS tr e am bos = new
BufferedOutpu t St re a m ( f o s ) ;
DataOutputStream dos = new
DataOutputStream ( bos ) ;
return
dos ;
}

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)

The method saveRouter() alls the method getDataOutputStream() to obtain a DataOutpuStream.


Firstly it writes the number of Elements stored in the list routerList to the stream. It then
alls the method saveRouterToStream() for ea h element of the list routerList. If an io-
error o urs during the operation, the methods prints "IO-Error while saving routers" to the
s reen. The method does not throw an IOEx eption.
Complete the method saveRouter(); make sure that the stream is losed before the program
leaves the method.
void s a ve Ro u t e r ( L i s t <Router> r o u t e r L i s t , S t r i n g f i l e N a m e ) {
dos . w r i t e I n t ( r o u t e r L i s t . s i z e ( ) ) ;
try final
( DataOutputStream dos = getDataOutputStream ( f i l e N a m e ) ) {
for ( Router aRouter : r o u t e r L i s t ) {

8
Examination OOP W 2018 Prof. Dr. M. Zeller Page 9 (10)
Name: Mat. no: 30. January 2019

saveRouterToStream ( aRouter , dos ) ;


}
} at h ( IOEx eption i o e x ) {
System . out . p r i n t l n ( "IO−E r r o r w h i l e s a v i n g r o u t e r s " ) ;
}
}

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);

The lass FileIoTest ontains the method runContainerTest().


This method denes and initializes a lo al variable routerMap of type HashMap. The HashMap
uses obje ts of type String as keys and stores obje ts of type Router as values. The initial
apa ity of the Map should be 20.
Complete line 39 of the method runContainerTest().
HashMap<String, Router> routerMap = new HashMap<>(20);

What is the output of the method runContainerTest()? Note: There might be more dottet
lines than reuqired.

Router: b2:..., 12, 112


Router: 3:..., 99, 99
Error during map a ess

10

You might also like