You are on page 1of 7

APPROVED

EXAMINATION PAPER: ACADEMIC SESSION 2007/2008

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 8th December 2007 3 Hours

Bahrain 15:00 UAE 17:00


Hong Kong 18:30 Greece 15:00
Malta 14:00 London 13:00
Singapore 18:30 Malaysia 18:30
Tanzania 16:00 Syria 14:00
Trinidad 09:30 Zambia 15:00

Answer any FOUR of the following SIX questions.


Each question is worth 25 marks.
If you answer more than four questions, marks will ONLY be awarded for your
FOUR best answers.
CALCULATORS AND ELECTRONIC DEVICES ARE NOT PERMITTED

_________________________________________________________________
Object Oriented Software Development
COMP1307
APPROVED

1. a) As a senior software engineer, you know that it is important to write


classes that satisfy the canonical form. Study the following class:

public class AmICanonical {


int x, y;
String z;

public int toString() {


return x + y;
}

public Object clone() throws CloneNotSupportedException {


return super.clone();
}
}

i) The toString() method is required to satisfy the canonical form


of classes. Explain whether or not the toString() method in the
above example has been implemented correctly in order to
satisfy the canonical form. Your answer should include an
explanation of why the toString() method is required.
[5 Marks]

ii) Explain whether the clone() method in the above code satisfies
the canonical form of classes for this example and why.
[6 marks]

iii) What type of constructor is required for the above class to


satisfy the canonical form? Write the code for a possible
constructor.
[2 marks]

b) Using an example, explain what a race condition is and why a race


condition occurs, and explain how thread synchronization can be used
to prevent a race condition.
[12 marks]

2 a) “The name of a concrete Collections class in Java often gives useful


information about the class. The first part of the name often indicates
the data structure used in the implementation of the class, and the
second part of the name often indicates the most important interface
used. An example is the ArrayList class, where the data structure used
is an array and the main interface used is List.”

Briefly explain each of the terms above marked with italics.


[4 marks]
Question 2 continued on next page
___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 2 of 7
APPROVED

Question 2 continued

b) You are given the following code for the Employee class:

public class Employee {


public Employee(String n) {
name = n;
salary = 0;
}

public String toString() {


return "[name=" + name + ", salary=" + salary + "]";
}

public void setSalary(double s) {


salary = s;
}

private String name;


private double salary;
}

To test this class, the TestEmployee class is provided:

import java.util.*;

public class TestEmployee {


public static void main(String[] args) {
Map staff = new HashMap();
staff.put("5464", new Employee("Angela"));
staff.put("2546", new Employee("Harry"));
staff.put("7935", new Employee("Gary"));
staff.put("5527", new Employee("Frances"));
System.out.println(staff);
staff.remove("2546");
staff.put("5527", new Employee("Frances"));
System.out.println( (Employee) staff.get("7935"));
Set entries = staff.entrySet();
Iterator iter = entries.iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
String key = (String) entry.getKey();
Employee value = (Employee) entry.getValue();
System.out.println("key=" + key + ", value=" + value);
}
}
}

Explain in detail what happens when the TestEmployee code is run.


[13 marks]
Question 2 continued on next page
___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 3 of 7
APPROVED

Question 2 continued

c) Describe the characteristics of the TreeSet concrete collection class,


and describe a practical programming situation where this class would
be suitable for use.
[8 marks]

3. Struts make use of the MVC architecture.

a) Explain the MVC architecture including the benefits of using it.


[10 marks]

b) Explain how MVC is used in Struts. Your answer should include a


description of the key Struts components e.g. ActionMappings.
[15 marks]

4. a) Study the program code below, which involves the display of four
buttons:

import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame {
public static void main(String[] args) {
MyFrame f = new MyFrame();
f.setSize(250, 350);
Container c = f.getContentPane();
c.setLayout(new GridLayout(2,1));
for (int i=0; i < 2; i++) {
JPanel pan = new JPanel();
pan.setBackground(i == 0 ? Color.lightGray : Color.white);
for (int j=0; j < 2; j++) {
JButton b = new JButton("Button number: " + i + " " + j);
pan.add(b);
}
c.add(pan);
}
f.setVisible(true);
}
}

Explain how this program executes when the code is compiled and run,
and sketch the appearance of the output.
[8 marks]
Question 4 continued on next page
___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 4 of 7
APPROVED

Question 4 continued

b) Explain how the Composite design pattern is used in this code.


[4 marks]

c) Discuss how the event related to a button click can be handled, and add
code to the program listed in part a) so that if a button is clicked, a
message is displayed stating which button was clicked. (The message
could be displayed using the System.out.println() method).
[6 marks]

d) Explain, with some example code, how a nested class can be used to
handle the button click event and display the message “A button has
been clicked” to the user. (The message could be displayed using the
System.out.println() method).
[7 marks]

5. a) Writing bug-free software is challenging. Developers typically spend


more time testing and debugging code than writing it. It is therefore
important to specify the behaviour of code as precisely as possible, as
early as possible. The use of pre- and post-conditions is an important
part of specifying this behaviour. Explain the reasons for using pre-
and post-conditions, provide an example and discuss their benefits.
[10 marks]

b) Another developer on your team has recently learned how to write


servlet code and is doing all their Java development using this
technology. Write an explanation for your colleague as to why, and in
what circumstances, they should consider using JSP code, and in
particular tag libraries, instead. Your answer should include an
explanation of JSP object scope.
[15 marks]

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 5 of 7
APPROVED

6. a) Study the code for the Java program below and answer the questions
about it which follow:

public class JQ {
public static void main (String [] a) {
int array1 [] = {12, 13, 14};
int array2 [] = {12, 13, 14};

System.out.println("*** part 1 ***");


if (array1 == array2)
System.out.println("they're the same");
else
System.out.println("they're not the same");

System.out.println("*** part 2 ***");


for (int i = 0; i < array1.length; i++)
System.out.println("element" + i + " = " + array1[i]);

for (int i = 0; i < array1.length; i++)


array1[i] -= 10;

System.out.println("*** part 3 ***");


for (int i = 0; i < array1.length; i++)
System.out.println("element" + i + " = " + array1[i]);

System.out.println("*** part 4 ***");


for (int i = 0; i < array1[2]; i++)
System.out.println("element" + i + " = " + array1[i]);
}
}

(i) Write down exactly what output you think the program will
produce when it is executed. Explain exactly why you think the
program will produce the output that you predict.
[8 marks]

(ii) Write a new method of class JQ called sum() that takes array1
and array2 as parameters and calculates and returns the
combined sum of both of their contents i.e. it adds together all
the values in array1 and array2 and returns the sum to the
caller.
[5 marks]

Question 6 continued on next page

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 6 of 7
APPROVED

Question 6 continued

b) The code above uses arrays to store groups of data. Name another
possible data structure from the Collections Framework which could be
used instead, and comment on any advantages or disadvantages of
using this.
[5 marks]

c) Discuss two different approaches offered by Java to persistently


(permanently) store the data held in the arrays so that it is available for
re-use after this program has terminated. State why you might choose
to use each one.
[7 marks]

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 7 of 7

You might also like