You are on page 1of 6

EXAMINATION PAPER: ACADEMIC SESSION 2008/2009

Campus Maritime Greenwich

School Computing and Mathematical Sciences

Department Computer Science

Level Three

TITLE OF PAPER Object Oriented Software Development

COURSE CODE COMP1307

Date and Time Saturday 6th December 2008 (2 hours)

BAHRAIN 15:00 BOTSWANA 15:00


GREECE 15:00 HONG KONG 18:30
KENYA 16:00 LONDON 13:00
MALAYSIA 18:30 MALTA 14:00
MAURITIUS 18:00 SAUDI ARABIA 16:00
SINGAPORE 18:30 SRI LANKA 18:30
SOUTH AFRICA 15:00 SYRIA 14:00
TANZANIA 16:00 TRINIDAD 09:30
UAE 17:00 ZAMBIA 15:00

You MUST answer question 1 which is worth 40 marks.

Answer TWO questions from the remaining THREE questions, questions 2 to 4 which are
worth 30 marks each. If you answer all THREE questions from questions 2 to 4, marks will
ONLY be awarded for your TWO best answers.

CALCULATORS AND OTHER ELECTRONIC DEVICES ARE NOT PERMITTED


Approved

1. a. Explain how Java offers support for contracts via the use of assertions,
and state the advantages and disadvantages of using them. Your answer
should include an example of an assertion.
[8 marks]

b. (i) Explain the principle of substitutability of subtypes and state its


advantages clearly. Your answer should include an example (in
English, not using Java code).
[6 marks]

(ii) Study the code below. Will it compile? Explain your answer in
detail. If you don’t think the code will compile, identify a
change you could make so that it would compile. Line numbers
are included to assist you with referring to specific code in your
explanation.

[6 marks]

1. class W {}
2. class Y {}
3. class X extends W {}
4. class Z extends Y {}
5. public class Substitute {
6. public static void main(String[] args) {
7. W w;
8. Y y;
9. X x;
10. Z z;
11. x = new X();
12. z = new Z();
13. y = z;
14. x = w;
15. }
16. }

c. You are currently designing a library application to allow various


products e.g. books, CDs, DVDs to be taken on loan from the library.
The type of products change frequently and you wish to design your
code so that classes representing new library products can be plugged
into the code without most of the system needing to know the names of
the classes. Identify a commonly used design pattern that could help you
solve this problem and give an outline description of how the pattern
could be applied in this case.

[10 marks]

Question one continued on the next page.

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 2 of 6
Approved

d. The Front Controller design pattern is one of the core J2EE design
patterns used in many systems. Critically discuss this design pattern
outlining the advantages and disadvantages of it.
[10 marks]

2. a) Why are global variables and/or methods (functions) generally


considered harmful? Justify with an example why you might sometimes
still use a class method rather than an instance method.

[5 marks]

b) Consider the following program. What will happen when it is run?

class Q2b {
public static void main(String[] args) {
int x = 3;
int y = x;
x = 4;
System.out.println("y = " + y);
Point p = new Point(3.5, 4.5);
Point q = p;
q.x = 13.0;
System.out.println("point x is " + p.x);
}
}

class Point {
double x; double y;
Point(double x, double y) { this.x=x; this.y=y; }
}
[3 marks]

c) Discuss two ways in which Java threads can be created and run.
Explain, with an appropriate application example for each, how Java
threads can be used in the following situations. You answer should also
include potential problems:
i) The use of “synchronized” to manage thread safety when
parallel processing with shared critical data;
ii) Prioritisation of threads.
[14 marks]

d) As a senior experienced Java programmer, what points would you


make in a presentation to your junior programmers which critically
discusses when to use JSPs and when to use Servlets in a typical web
application?
[8 marks]
___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 3 of 6
Approved

3. a) Developing large scale systems is challenging and two strategies that


can help are replication and clustering. Discuss with an example how
these two strategies can be used in large application design, and
describe any issues that may arise.
[15 marks]

b) i) Give an account of Exception Handling in Java. Illustrate your


answer by considering the following program, which may give
rise to three different types of exception. Describe how you
would rewrite this program in such a way that each of these
exceptions could be handled.

class Q3b
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a / b;
System.out.println("The answer is " + c);
}
}
[12 marks]

ii) Java distinguishes between run-time exceptions and run-time


errors. Explain why this distinction is made, and give an
example of a typical run-time error.
[3 marks]

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 4 of 6
Approved

4. a) i) Explain how the layout managers FlowLayout and


BorderLayout may be used to position user interface components in
a Java graphical user interface (GUI) component. Illustrate your
answer with reference to the following application. A description in
English only is required – do NOT write detailed Java code.

[7 marks]

ii) Identify, and explain the operation of, a layout manager


suitable for the following GUI component:

[3 marks]

iii) In the application shown in part i), when the user clicks the Car
button, the Cars count is to be increased by 1, and when the user
clicks the Van button, the Vans count is to be increased by 1.
Describe how this may be achieved with reference to the event
handling mechanism of Java. Again, a description in English only is
required – do NOT write detailed Java code.
[5 marks]

Question four is continued over the page.

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 5 of 6
Approved

b) i) What do you understand by the term “harness testing”? The example


below includes some test cases for testing the class Calculator. Identify
the further test that should be included to complete the test harness.

public class Harness {


public static void main(String[] args) {
Calculator calc;

// test 1
calc = new Calculator(1.5, 3.5);
System.out.println("Test 1. Expected value 5.0: " +
calc.operate(1));

// test 2
calc = new Calculator(5.4, 4.4)
System.out.println("Test 2. Expected value 1.0: " +
calc.operate(2));
}
}

// the class under test


class Calculator {
private double a, b;

public Calculator(double a, double b) {


this.a = a;
this.b = b;
}

// method to return a + b, a - b, a * b or a / b depending on value


// of parameter op. If op is not 1, 2, 3 or 4 or if an attempt
// is made to divide by 0, the result returned is
// Double.NaN (Not a Number).
public double operate(int op) {
switch (op) {
case 1 : return a + b;
case 2 : return a - b;
case 3 : return a * b;
case 4 : if (b == 0) return Double.NaN;
else return a / b;
default : return Double.NaN;
}
}
}

[7 marks]

ii) Give a brief account of the unit testing framework JUnit,


explaining how it enables programmers to set up test harnesses
for the public methods and constructors of given java classes. In
your answer, comment on the use it makes of reflection.

[8 marks]

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 6 of 6

You might also like