0% found this document useful (0 votes)
236 views8 pages

Module Examination 2013 Object Oriented Java Programming

Uploaded by

Yagui
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)
236 views8 pages

Module Examination 2013 Object Oriented Java Programming

Uploaded by

Yagui
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/ 8

*M25013F3*

M250/E
Module Examination 2013

OBJECT ORIENTED JAVA


PROGRAMMING

Wednesday, 12 June 2013 10.00 am – 1.00 pm

Time allowed: 3 hours

This exam contains four questions worth 25 marks each. You should
attempt ALL questions.
All your answers must be written in the answer book(s) provided. You
should start each of the four questions on a new page. Indicate which
questions you have attempted in the spaces provided on the front cover.
You are advised not to cross through any work until you have replaced it
with another solution to the same question.
There are no marks awarded for commenting your code and you may
assume any import statements required, unless otherwise indicated.
You may assume that methods receive sensible values when a
message is sent unless otherwise indicated.
At the end of the examination
Check that you have written your personal identifier and examination
number on each answer book used. Failure to do so will mean that
your work cannot be identified.
Put all your used answer books together with your signed desk record
on top. Fasten them in the top left corner with the round paper fastener.
Attach this question paper to the back of the answer books with the flat
paper clip.

Copyright © 2013 The Open University


Question 1 (a) Briefly explain the difference between a reference type variable and a
(25 marks) primitive type variable.
(3 marks)
(b) Write a class called PostalAddress with the following features:
(i) Three private instance variables of type String called firstLine,
secondLine and postCode.
(ii) A constructor with three formal arguments all of type String, which are
used to initialise firstLine, secondLine and postCode respectively.
(iii) A public instance method called getDeliveryInfo() which takes no
arguments and returns a string containing the final three characters of
postCode.
(iv) A public instance method getAddress() which takes a single string
argument. If the argument matches the receiver's post code, the
method returns the concatenation of firstLine, secondLine and
postCode separated by spaces. If there is no match, the method
returns the string "no match".
(12 marks)
(c) Explain what is meant by data hiding and how it is implemented in Java.
Give all the examples of data hiding within the class PostalAddress.
(3 marks)
(d) Assume that the following statements are executed successfully:
PostalAddress postAddress; //1
postAddress = new PostalAddress("5 Beech Road", "Ipswich", "IP1 5HR"); //2
String deliveryInfo = postAddress.getDeliveryInfo(); //3
String address = postAddress.getAddress("MK7 6AA"); //4
String addressInCaps = address.toUpperCase(); //5
char charNum = address.charAt(0); //6
int num = charNum; //7

With reference to the above statements only, identify the lines of code
(state "none" if there are no such lines) in which
(i) reference variables are declared
(ii) primitive variables are declared
(iii) objects other than strings are created
(iv) arguments are passed to methods
(v) messages are sent
(vi) Boolean values are used
(vii) constructors are invoked
(7 marks)

2 M250 June 2013


Question 2 (a) Suppose that Trainable is an interface that specifies two methods with the
(25 marks) signatures pushUps(int) and marchToMiddle(). Neither of these methods
returns a value. Write down the Trainable interface.
(3 marks)
(b) Suppose that TrainableFrog is a subclass of the OU library class Frog and
that TrainableFrog implements the Trainable interface.
(i) Write down the header for the TrainableFrog class.
(ii) Write a constructor that initialises the inherited instance variables as for
the superclass. Instances of TrainableFrog are to be initialised to the
same state as instances of Frog.
(iii) When sent a pushUps(int) message an instance of TrainableFrog
executes the number of push ups indicated by the message’s
argument. Each push up consists of a jump, followed by a move one
stone to the right, and then a croak. Write the pushUps()method.
(iv) When sent a marchToMiddle() message an instance of
TrainableFrog alternately jumps and moves one stone in the
appropriate direction until it reaches stone 5. Write the
marchToMiddle()method.
(7 marks)
(c) Briefly explain two potential advantages of defining the Trainable interface.
(5 marks)
(d) Write down three similarities in the usages of abstract classes and
interfaces.
(6 marks)
(e) Suppose that TrainableFrog implements the interface Trainable and that
LearnerFrog is a subclass of TrainableFrog. Explain how LearnerFrog
can implement the interface Trainable. Justify your answer.
(4 marks)

M250 June 2013 TURN OVER 3


Question 3 Table 1 shows a catalogue of model numbers and names of light bulbs produced
(25 marks) by certain manufacturers.

Model Number Model Name

10 "Sparky"
200 "BrightGuy"
911 "Neverlast"
42 "Blinker"
Table 1 Catalogue of model numbers and names for light bulbs
Suppose that in a piece of software under development these tables are
implemented and managed by instances of a class called BulbCatalogue.
(a) The class BulbCatalogue needs a single private instance variable called
catalogue. Write down the declaration for catalogue, which should be
declared to be of a suitable interface type to hold an unsorted map with the
model number as the key.
(3 marks)
(b) Write a zero-argument constructor for BulbCatalogue that initialises
catalogue to reference an empty map of an appropriate class.
(3 marks)
(c) Write a public instance method called populateCatalogue() for the
BulbCatalogue class that takes no arguments and returns no value. This
method should simply add the four entries shown in Table 1 to the map
referenced by catalogue.
(2 marks)
(d) Write a public instance method for the BulbCatalogue class called
updateCatalogue() that takes no arguments and returns no value. This
method should update the model name for any model number in the map
less than 100 by appending the text " – economy" (as illustrated in Table 2
below). Your method should work irrespective of how many entries there are
in the map referenced by catalogue.

Model Number Model Name

10 "Sparky – economy"
200 "BrightGuy"
911 "Neverlast"
42 "Blinker – economy"
Table 2: After the method updateCatalogue() has been executed
(7 marks)

4 M250 June 2013


(e) A programmer suggests that an appropriate collection to implement the
catalogue described above would be an array of strings. Briefly explain two
reasons why this would be a poor proposal.
(4 marks)
(f) In part (a) above we called for a declaration of an interface type. Explain,
using a code fragment example, why declaring catalogue to be of an
interface type as opposed to a concrete class type is advantageous.
(4 marks)
(g) In what way might auto-boxing come into play in the use of the catalogue
collection?
(2 marks)

M250 June 2013 TURN OVER 5


Question 4 Consider a hypothetical utility class called AmphibianWriter that has a class
(25 marks) method with the following header:
public static void writeAmphibians(Collection<Amphibian> amphibians)
Note that this method uses a collection of objects of the OU library class
Amphibian described in your M250 Exam handbook.
You are given an incomplete version of the writeAmphibians() method below
and are required to complete this method so that it does the following:
 Prompts the user for a pathname which is then used to create a File object.
 Attempts to open a stream on that file.
 Writes the details of the amphibians held in the collection referenced by the
argument amphibians to the stream in CSV format.
Examples of lines that your method should produce in the file are as follows
(where the meaning of the tokens on each line is given in the table below):
HoverFrog,0,255,0,1,3
Toad,128,66,64,6
Frog,0,255,0,4
HoverFrog,0,255,0,3,1
Frog,0,255,0,4
...

Token

1 class name of the amphibian you are saving


2 amount of red in colour of the amphibian you are saving
3 amount of green in colour of the amphibian you are saving
4 amount of blue in colour of the amphibian you are saving
5 position of the amphibian you are saving
6 height of the amphibian you are saving. This token will only be present
in the line if the amphibian you are saving is an instance of HoverFrog.

Tips for determining tokens 1, 2, 3, 4 and 6:


 The following code is an example of how to get the class name of any object
as a string: someObject.getClass().getName();
 Given an instance of OUColour you can get the amount of red, green and
blue in that colour with the messages getRed(), getGreen() and
getBlue().
 When you detect that the class name of an amphibian is "HoverFrog", you
will need to explicitly cast that amphibian to HoverFrog before sending it a
getHeight() message.

6 M250 June 2013


Incomplete code for the method follows with comments indicating question parts:
public static void writeAmphibians(Collection<Amphibian> amphibians)
{
String pathName = //complete for part (a)(i)

File amphibianFile = //complete for part (a)(ii)

BufferedWriter bufferedFileWriter = null;

try
{
bufferedFileWriter = //complete for part (a)(iii)

//complete for part (b)

}
catch (IOException anException) //discuss in (c)(iv)
{
System.out.println("Error: " + anException);
}
finally
{
try
{
bufferedFileWriter.close();
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
}

(a) With reference to the commented lines in the code above and making use of
appropriate library classes from your Exam Handbook:
(i) Complete the assignment statement that begins:
String pathName =
(1 mark)
(ii) Complete the assignment statement that begins:
File amphibianFile =
(1 mark)

M250 June 2013 TURN OVER 7


(iii) Complete the assignment statement that begins:
bufferedFileWriter =
So that bufferedFileWriter is assigned an instance of
BufferedWriter.
(2 marks)
(b) Complete the try block for writeAmphibians(). Your code should write the
details of the amphibians held in the collection referenced by the argument
amphibians to the output stream in Comma Separated Value (CSV) format
as described at the start of this question.
(12 marks)
(c) (i) The stream referenced by bufferedFileWriter is opened within a
try block. Is there an alternative? Explain your answer.
(2 marks)
(ii) Briefly explain why it is good programming style for the stream to be
closed within a finally block as shown.
(2 marks)
(iii) Give two reasons why it is important that an attempt is made to close
the stream referenced by bufferedFileWriter.
(2 marks)
(iv) Suppose that in the line commented (c)(iv) instead of IOException, the
programmer had used a formal argument of type Throwable. Explain
whether the code would still compile and run, and if Throwable would
be an appropriate choice of exception to catch.
(3 marks)

[END OF QUESTION PAPER]

8 M250 June 2013

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