0% found this document useful (0 votes)
4 views13 pages

Exam Oop W14 Inclusive Solution

The document outlines an examination for Object Oriented Programming at the University of Applied Science Ravensburg-Weingarten, scheduled for February 7, 2015. It includes various parts with questions related to programming concepts, class definitions, exception handling, and file operations, totaling 64 points. Students are reminded to include their name and matriculation number on each sheet and that less than 50% is required to pass the exam.
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)
4 views13 pages

Exam Oop W14 Inclusive Solution

The document outlines an examination for Object Oriented Programming at the University of Applied Science Ravensburg-Weingarten, scheduled for February 7, 2015. It includes various parts with questions related to programming concepts, class definitions, exception handling, and file operations, totaling 64 points. Students are reminded to include their name and matriculation number on each sheet and that less than 50% is required to pass the exam.
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/ 13

Examination in Object Oriented Programming

University of Applied Science Ravensburg-Weingarten


Prof. Dr. M. Zeller

Date, time February 07th, 2015, 10:30 – 12:00 (90 min)


Number of pages 13 pages (including title)
Resources All accepted resources

Study Progam Exam. No. Room


EI 2451 H061
EI 2608 H061

Name: Matriculation number:

Reminder:
• Please note name and matriculation number on each sheet.
• If you use additional sheets do not forget to note name and matriculation number on
them too.

leave blank, please:


Part 1 2 3 4 Sum
max. 27 12 5 20 64
Points
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 2 (13)
Name: Mat. no: February 07th, 2015

Preliminary note The examination is quite extensive. Don’t be scared – you may miss some
points an still get an A grading, you need less than 50 % to pass the exam.

Part 1

1.1 (11 Points)

Analyse the program given in section "Constructors".


What is the output of the program just after line 83 is executed? Note: there might be more
dotted lines than actually used.
What is the additional output of the program just after line 86 is executed?
What is the additional output of the program just after line 89 is executed?

in Animal()
in Mamal()
in Saddle()
in Horse()
--------- 1 -----------
Horse:
Name: Morningstar
Saddle olor: brown, size:2
--------- 2 -----------
Horse:
Name: Morningstar
Saddle olor: dark brown, size:3 Age: 11
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 3 (13)
Name: Mat. no: February 07th, 2015

1.2 (3 Points)

Now we change a part of the definition of class Saddle as follows:


publ i c c l a s s Saddle {
S t r i n g c o l o r = " brown " ;
private int size = 2;
:
:
}

Without any further changes the compiler now signals an error in class Horse, method
hangeSaddle. Which line(s) of the method cause(s) an error?
Change the method hangeSaddle so that it can be compiled and it achives the same effect
as the original method.
publ i c c l a s s Horse extends Mamal {
p r i v a t e Saddle theS a ddle = new Saddle ( ) ;

publ i c void changeSaddle ( S t r i n g newColor , i n t newSize ) {


theS a ddle . c o l o r = newColor ;
theS a ddle . s e t S i z e ( newSize ) ;
}
}

1.3 (4 Points)

Now we add the Class lass HorseAdmin in package admin.


Does this class compile? If not, list all lines (give the line number) that will cause the com-
piler to signal an error. Omit lines which do not cause any error.

Lines:
Horse aHorse = new Horse();
aHorse.name = name_ ;
if(aHorse.age < 20){
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 4 (13)
Name: Mat. no: February 07th, 2015

1.4 (9 Points)

Define a class Elephant as a subclass of class Mamal. The class contains a member lifespan
with the following properties:
⊲ It can hold an integer value
⊲ The value is only accessable within the class Elephant
⊲ The value is 70
⊲ The value can not be changed.
Complete the definition of member lifespan at the ellipsis.
Complete the definition of the constructor. The string-argument is used to set the name of
the elephant. The integer-argument should set the member age (how can you achive this?).
Complete the method getRelativeAge(). The return value is: the value of member age
times 100 divided by the value of member lifespan.
Creating and using an object of the class:
Elephant e l = new Elephant ( " Bob " , 5 0 ) ;
el . print ( ) ;

Output:

Elephant
Name: Bob
relative age: 71%

publ i c c l a s s Elepha nt extends Mamal {


private final int lifespan = 70;

Elepha nt ( S t r i n g name_ , i n t age_ ) {


setAge ( age_ ) ;
name = name_ ;
}
protected i n t getRelativeAge ( ) {
r e t u r n ( getAge ( ) ∗ 1 0 0 ) / l i f e s p a n ;
}
publ i c void p r i n t ( ) {
System . out . p r i n t l n ( " Elepha nt " ) ;
super . p r i n t ( ) ;
System . out . p r i n t l n ( " r e l a t i v e age : " + g e t R e l a t i v e A g e ( ) + "%" ) ;
}
}
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 5 (13)
Name: Mat. no: February 07th, 2015

Part 2

Analyse the program given in section Railroad.


Complete the given methods:

2.1 (4 points)

The method void addLo omotive() assigns a new value to the member theLo omotive.
The method void addWaggon() assigns a new value to the member theWaggons. The pre-
viusly stored reference should be added to the new waggon in order to form a linked list as
shown in the figure below.
The data structure after line 47 in the method main() is executed:
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 6 (13)
Name: Mat. no: February 07th, 2015

2.2 (8 points)

The method int getWeight() computes the weight of the train by summing up the weight
of the locomotive and the weight of all linked waggons. The method should not throw an
exception even if there is neither a locomotive nor any waggon linked to the train.
The output of the program is:

(1) Weight: 0
(2) Weight: 55
(3) Weight: 66

1 publ i c c l a s s T r a i n {
2 int trainID ;
3 Locomotive theLocomotive ;
4 Waggon theWaggons ;
5
6 T r a i n ( i n t id ) {
7 t r a i n I D = id ;
8 }
9
10 void addLocomotive ( Locomotive newLocomotive ) {
11 theLocomotive = newLocomotive ;
12 }
13
14 void addWaggon ( Waggon newWaggon ) {
15 newWaggon . f o l l o w e r = theWaggons ;
16 theWaggons = newWaggon ;
17 }
18
19 i n t getWeight ( ) {
20 i n t sum = 0 ;
21 i f ( theLocomotive ! = n u l l ) {
22 sum = theLocomotive . weight ;
23 }
24 Waggon currentWaggon = theWaggons ;
25 while ( currentWaggon ! = n u l l ) {
26 sum += currentWaggon . weight ;
27 currentWaggon = currentWaggon . f o l l o w e r ;
28 }
29 r e t u r n sum ;
30 }
31 }

Solution:
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 7 (13)
Name: Mat. no: February 07th, 2015

Objects of type TestComposition

value: 12 value: 13 value: 15

startNode left: left: left:


right: right: right:
value: 11
left:
right:
value: 14 value: 16
left: left:
right: right:
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 8 (13)
Name: Mat. no: February 07th, 2015

Part 3

3.1 (5 points) Exception Handling

Analyse the program given in section Exception.


The program defines some exception classes and a main class, Method bar() throws a num-
ber of different exceptions. In method foo() fill in the code for catching these exceptions.
The program reports the type of the exception. Note: Each of the exceptions thrown in the
program should be cought separately – sequence matters.

Note: The clauses for catching the LeftEx eption and for catching the RightEx eption
may be interchanged. It is crucial that both LeftEx eption and RightEx eption are caught
before CenterEx eption and CenterEx eption is caught before Ex eption.
32 void fo o ( i n t num) {
33 try {
34 i n t r e s u l t = bar (num ) ;
35 System . out . p r i n t l n ( " i n foo , r e s u l t : " + r e s u l t ) ;
36 } c a t c h ( L e f t E x c e p t i o n ex ) {
37 System . out . p r i n t l n ( " −−> cought L e f t E x c e p t i o n : " + ex . exNum ) ;
38 } c a t c h ( R i g h t E x c e p t i o n ex ) {
39 System . out . p r i n t l n ( " −−> cought R i g h t E x c e p t i o n " ) ;
40 } c a t c h ( Ce nt e r E xce p t io n ex ) {
41 System . out . p r i n t l n ( " −−> cought Ce nt e r E xce p t io n " ) ;
42 } c a t c h ( E xce p t io n ex ) {
43 System . out . p r i n t l n ( " −−> cought g e n e r a l E xce p t io n " ) ;
44 }
45 return ;
46 }
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 9 (13)
Name: Mat. no: February 07th, 2015

Part 4

This part refers to section "Collection and IO" of the program handout. A program stores
objects in an ArrayList, writes these objects to two different files and reads the objects from
the file.

4.1 (2 Points)

Define a member persList in the class PersonAdmin. This member should be typed as a
reference to an ArrayList that holds objects of type Person.
Further complete the Constructor of the class PersonAdmin so that the member persList
actually holds a reference to an ArrayList. The ArrayList should have an initial capacity
of 30.
Complete the program fragment at the ellipsis.
47 publ i c c l a s s PersonAdmin {
48 A rra yList <Person > p e r s L i s t ;
49
50 publ i c PersonAdmin ( ) {
51 p e r s L i s t = new A rra yList <Person > ( 3 0 ) ;
52 }
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 10 (13)
Name: Mat. no: February 07th, 2015

4.2 (2 Points)

The method call pao.initializePersonList(3); in the method main() creates random


objects an stores them in the ArrayList persList. Note: There is no need to analyse and
understand the method initializePersonList(). The output listing below shows the val-
ues of these objects during a certan run of the program. I. e. it shows the output of the first
call to pao.printPersons();.
The program runs up to line 51 // - - - 1 - - - .
Complete the output of the program so far (Note: There might be more doted lines than
you will need).
The call pao.persList.add(1, aPerson) adds the new person as second element of the list;
pao.persList.remove(3) removes the fourth (here last) entry of the list.

--- List of persons ---


Bob Mills, born in 1924
Don North, born in 1936
Claire Mills, born in 1920
--- List of persons ---
Bob Mills, born in 1924
Finn Pony, born in 2015
Don North, born in 1936

4.3 (3 Points)

The program creates a buffered output stream to save data into a file. Complete the method
getBufferedInputStream() at the ellipsis.
BufferedOutputStream getBufferedOutputStream ( S t r i n g fileName )
throws IOException {
F ile Ou t p u t S t r e am f o s = new F ile Ou t p u t S t r e am ( fileName ) ;
BufferedOutputStream bos = new BufferedOutputStream ( f o s ) ;
r e t u r n bos ;
}
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 11 (13)
Name: Mat. no: February 07th, 2015

4.4 (7 Points)

The program iterates through the persList and uses a DataOutputStream to save some data
of each object to a file. Method call: pao.savePersToFile("persFile.dat"). The method should
work for an arbitrary number of objects inside the persList.
It first saves the number of objects to write. Inside the loop it writes the value of the mem-
bers yearOfBirth, firstName and lastName to the output-stream. Complete the method
savePersToFile() at the ellipsis.
p u b lic void s a v e P e r s T o F i l e ( S t r i n g dataFileName ) {
DataOutputStream dos = n u ll ;
try {
BufferedOutputStream bos = getBufferedOutputStream ( dataFileName ) ;
dos = new DataOutputStream ( bos ) ;
dos . w r i t e I n t ( p e r s L i s t . s i z e ( ) ) ;
f o r ( i n t i = 0 ; i < p e r s L i s t . s i z e ( ) ; i ++) {
Person aPerson = p e r s L i s t . g e t ( i ) ;
dos . w r i t e S h o r t ( aPerson . y e a r O f B i r t h ) ;
dos . writeUTF ( aPerson . fir s t N ame ) ;
dos . writeUTF ( aPerson . lastName ) ;
}
} c a t c h ( IOException ex ) {
ex . p r i n t S t a c k T r a c e ( ) ;
}
try {
dos . c l o s e ( ) ;
} c a t c h ( IOException ex ) {
ex . p r i n t S t a c k T r a c e ( ) ;
}
return ;
}

4.5 (2 Points)

In line 55 the method printPersonsToFile() is called. It writes the data stored in the
objects of type Person to a file. The file is human readable e. g. in an editor:

Bob Mills 1924


. . . . . . .

Which type of stream do you use to write this file? Fill in the appropriate type and an
instruction to create the stream.
void p r i n t P e r s o n s T o F i l e ( S t r i n g fileName ) {
P r i n t S t r e a m pos = n u ll ;
try {
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 12 (13)
Name: Mat. no: February 07th, 2015

BufferedOutputStream bos = getBufferedOutputStream ( fileName ) ;


pos = new P r i n t S t r e a m ( bos ) ;

f o r ( i n t i = 0 ; i < p e r s L i s t . s i z e ( ) ; i ++) {
Person aPerson = p e r s L i s t . g e t ( i ) ;
pos . p r i n t ( aPerson . fir s t N ame ) ;
pos . p r i n t ( " " ) ;
pos . p r i n t ( aPerson . lastName ) ;
pos . p r i n t ( " " ) ;
pos . p r i n t l n ( aPerson . y e a r O f B i r t h ) ;
}
} c a t c h ( IOException ex ) {
ex . p r i n t S t a c k T r a c e ( ) ;
}
i f ( pos ! = n u ll ) {
pos . c l o s e ( ) ;
}
return ;
Examination OOP WS 2014 Prof. Dr. M. Zeller Page 13 (13)
Name: Mat. no: February 07th, 2015

4.6 (3 Points)

Later the progam reads data via a DataInputStream from the file and constructs new objects
from these data.
First it reads the number of data sets stored in the file. For each data set it reads a short
value and two strings. It then creates a new objct of type Person feeding the constructor
with the values read. Then it adds the object to the ArrayList persList.
Complete the method readPersFromFile at the ellipsis.
p u b lic void r e ad P e r s F r o mF ile ( S t r i n g dataFileName ) {
DataInputStream d i s = n u ll ;
try {
B u ffe r e d I np u t S t r e am b i s = g e t B u f f e r e d I n p u t S t r e a m ( dataFileName ) ;
d i s = new DataInputStream ( b i s ) ;
i n t persCnt = d i s . r e a d I n t ( ) ;
f o r ( i n t i = 0 ; i < persCnt ; i ++) {
s h o r t yob = d i s . r e ad S ho r t ( ) ;
S t r i n g fName = d i s . readUTF ( ) ;
S t r i n g lName = d i s . readUTF ( ) ;
Person aPerson = new Person ( fName , lName , yob ) ;
p e r s L i s t . add ( aPerson ) ;
}
} c a t c h ( IOException ex ) {
ex . p r i n t S t a c k T r a c e ( ) ;
}
try {
dis . close ( ) ;
} c a t c h ( IOException ex ) {
ex . p r i n t S t a c k T r a c e ( ) ;
}
return ;
}

4.7 (1 Point)

What happens during the call pao.persList.add(10, aPerson) in line 57?

The method call persList.add(10, aPerson); throws an exception since it is not allowed
to insert an element at a position greater than persList.size().

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy