You are on page 1of 14

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


(2004 to 2007 Syllabus – referred and deferred
students only)

COURSE CODE COMP1307

Date and Time 3 hours

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
Page 1 of 14
Approved

1. (a) Study the Java class below. Does the Hello class follow the canonical
form of classes? Fully justify your answer, commenting on all the
requirements for classes to meet the canonical form. If the method
signatures are wrong then provide corrected code. If the content of a
method is wrong then state why and what it should do. You are not
required to provide the code for the content of any method.

class Hello implements java.io.Serializable, java.lang.Cloneable {


String message;

public Hello() {
message=null;
}

public void setMessage(String message) {


this.message = message;
}

public String toString() {


return "Hello";
}

public boolean cloneObject() {


return false;
}

public int hashCode() {


int hash = 0;
return hash;
}

public boolean equals(Object object) {


if (!(object instanceof Hello)) {
return false;
}
return true;
}
}

[12 marks]

(b) As an experienced web developer, you have been asked to give a


seminar to some junior developers about how the Struts framework
works. Describe the sequence of events that occurs in processing a user
request and all the Struts components involved.

[13 marks]

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 2 of 14
Approved

2. (a) Study the following code:

//Listing 1
public interface Stack<E> {
public int size();
public boolean isEmpty();
public boolean push (E element); //returns false if stack is full
public E pop(); //returns null if stack is empty
} //end code fragment Stack

//Listing 2
public class ArrayStack<E> implements Stack<E> {
protected int capacity;
public static final int CAPACITY = 1000;
protected E s[];
protected int top = -1;

public ArrayStack() {
this(CAPACITY);
}

public ArrayStack(int cap) {


capacity = cap;
s = (E[]) new Object[capacity]; // compiler may give warning but ok
}

public int size() {


return (top + 1);
}

public boolean isEmpty() {


return (top < 0);
}

public E pop() {
if (isEmpty()) return null;
else return s[top--];
}

public String toString() {


String str;
str = "[";
if (size() > 0) str += s[0];
if (size() > 1)
for (int i = 1; i <= size()-1; i++)
str += ", " + s[i];
return str + "]";
}
} //end code fragment ArrayStack

Question 2 is continued on the following page


___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 3 of 14
Approved

(i) Explain what this code does (both Listing 1 and Listing 2). In
particular, comment about the use of E, capacity and
CAPACITY.

[8 marks]

(ii) Compare and contrast Java interfaces and abstract classes.


What difference would it make to the code in Listing 2 if the
key word interface is replaced by abstract?

[5 marks]

(b) (i) Give an account of the different testing phases that are
commonly used in the development of software systems.

[6 marks]

(ii) Identify the testing phase of a Java software system where the
framework JUnit may be used, and describe briefly the way this
framework operates.

[6 marks]

3. (a) Discuss the “bounded queue” problem and briefly describe how thread
synchronization is used to solve the problem.

[12 marks]

(b) You are currently a consultant to a large company who is setting up a


web based system that will require at least 100 concurrent users
accessing a single database at peak times. However, they are unsure
which JDBC driver to use. As a first step you have been asked to write
a critical review comparing and contrasting the three most useful types
of JDBC driver you would consider appropriate in this scenario.
Clearly state which three you would recommend they consider and
why.

[13 marks]

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 4 of 14
Approved

4. (a) Examine the following code for a simple HTML document, which
contains a form with action set to run a servlet ListData on the server:

<HTML>
<HEAD>
</HEAD>
<BODY>
<H1><FORM NAME=form1 METHOD=GET
ACTION=http://stutomcat.cms.gre.ac.uk/cd05/servlet/ListData>
What's your name? <INPUT TYPE=TEXT NAME=username
SIZE=20><BR>
What's your user id? <INPUT TYPE=TEXT NAME=userid
SIZE=5><BR>
<P><INPUT TYPE=SUBMIT VALUE="Send fields to server">
</FORM></H1>
</BODY></HTML>

The Java code for the servlet is:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ListData extends HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
String name = req.getParameter("username");
String id = req.getParameter("userid");
out.println("<H3>");
out.println("You entered<BR>");
out.println("Name:&nbsp" + name + "<BR>");
out.println("User Id:&nbsp" + id + "<BR>");
out.println("</H3>");
out.close();
}
}

(i) Sketch the appearance of this page when it is loaded into a


browser.

[2 marks]

(ii) Describe what happens if the user inputs Sam Smith to the
name field and ss01 to the user id field and presses the submit
button.

[5 marks]

Question 4 is continued on the following page


___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 5 of 14
Approved

(iii) Rewrite the servlet ListData.java as a java server page


ListData.jsp which will have exactly the same effect.

[6 marks]

(b) Compare and contrast the use of servlets and java server pages. In your
answer, stress the relative advantages and disadvantages of these two
technologies.

[12 marks]

5. Study the following Java application. (The line numbers are for reference
purposes only)
01 import java.awt.*;
02 import javax.swing.*;
03
04 public class AandB extends JFrame {
05
06 JTextField aTxt = new JTextField(2);
07 JTextField bTxt = new JTextField(2);
08 JLabel message = new JLabel();
09 JButton aBtn = new JButton("a");
10 JButton bBtn = new JButton("b");
11 JButton resetBtn = new JButton("Reset");
12 int a = 0, b = 0;
13
14 public static void main(String[] args) { new AandB(); }
15
16 public AandB() {
17 JPanel top = new JPanel();
18 JPanel middle = new JPanel();
19 JPanel bottom = new JPanel();
20 ABHandler abh = new ABHandler();
21 setLayout(new BorderLayout());
22 setSize(200, 120);
23 setTitle("a and b");
24 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
25 top.setLayout(new FlowLayout());
26 top.add(aBtn);
27 top.add(resetBtn);
28 top.add(bBtn);
29 add("North", top);
30 middle.setLayout(new FlowLayout());
31 middle.add(aTxt);
32 middle.add(bTxt);
33 add("Center", middle);

Question 5 is continued on the following page

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 6 of 14
Approved

34 bottom.setLayout(new FlowLayout());
35 bottom.add(message);
36 add("South", bottom);
37 setResizable(false);
38 setVisible(true);
39 abh.update();
40 }
41
42 class ABHandler {
43
44 // update the display
45 public void update() {
46 aTxt.setText("" + a);
47 bTxt.setText("" + b);
48 if (a < b) message.setText("b is bigger");
49 else if (a > b) message.setText("a is bigger");
50 else message.setText("They are the same");
51 }
52 }
53 }

(a) This application uses layout managers FlowLayout and BorderLayout.


Describe these layout managers. Illustrate your answer by sketching the
appearance of the application‟s JFrame when it is first loaded.
[7 marks]

(b) The programmer doesn‟t understand how event handling works in Java.
He has included a nested class ABHandler as shown (lines 42 – 52) with
a method update. He has called this method at line 39 to set the display
to its initial state. In addition he wants:
1 to be added to a whenever the aBtn is clicked
1 to be added to b whenever the bBtn is clicked
a and b to be reset to 0 whenever the resetBtn is clicked
and then the update method to be called in each case.
What additional code is required to achieve this? (In your answer you
may refer to the line numbers in the given code.)
[8 marks]

(c) The given application could be criticized because it combines


presentation, event handling and „business logic‟ all in one unit. The
Model-View-Controller (MVC) design pattern avoids this. Give an
account of the MVC pattern and describe briefly how you would
restructure the application to conform to this pattern. (You are NOT
expected to write any code for this part).

[10 marks]

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 7 of 14
Approved

6. (a) The Java Collections framework defines a set of interfaces and classes,
designed to support the storage and retrieval of objects, using a variety
of data structures, algorithms and time-space complexities. Discuss
these three terms in the context of the collections framework.

[6 marks]

(b) An online music store (doing similar business as Apple iTunes Store)
requires the company, where you work as a senior software consultant,
to provide a solution that would record all the downloads of music such
as albums and tracks in real time 24 hours a day and seven days a
week. The recorded information must include title and count of
downloads of all the music they sell on-line. Compare and contrast two
data structures from the Java collection framework before choosing one
for solution. Justify your decision.
[7 marks]

(c) Critically evaluate two design patterns that you have learned about.
Your answer should include an example of where you might choose to
use each one.

[12 marks]

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 8 of 14
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


(Syllabus from Jan 2008)

COURSE CODE COMP1307

Date and Time 2 hours

You MUST answer question 1.


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

CALCULATORS AND OTHER ELECTRONIC DEVICES ARE NOT


PERMITTED

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 9 of 14
Approved

You must answer question one:

1. (a) Study the Java class below. Does the Hello class follow the canonical
form of classes? Fully justify your answer, commenting on all the
requirements for classes to meet the canonical form. If the method
signatures are wrong then provide corrected code. If the content of a
method is wrong then state why and what it should do. You are not
required to provide the code for the content of any method.

class Hello implements java.io.Serializable, java.lang.Cloneable {


String message;

public Hello() {
message=null;
}

public void setMessage(String message) {


this.message = message;
}

public String toString() {


return "Hello";
}

public boolean cloneObject() {


return false;
}

public int hashCode() {


int hash = 0;
return hash;
}

public boolean equals(Object object) {


if (!(object instanceof Hello)) {
return false;
}
return true;
}
}

[13 marks]

(b) Discuss the DAO pattern and explain its use of the Transfer Object
Pattern in J2EE applications.

[14 marks]

Question 1 is continued on the following page

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 10 of 14
Approved

(c) Below is example code for a singleton design pattern. Explain the
purpose of the pattern and when you would use it. Your answer should
include an explanation of how the code works.

public class Singleton {


private static Singleton instance = null;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}

[13 marks]

Answer any two questions from the following three questions:

2. (a) Discuss the “bounded queue” problem and briefly describe how thread
synchronization is used to solve the problem.

[12 marks]

(b) You are currently a consultant to a large company who is setting up a


web based system that will require at least 100 concurrent users
accessing a single database at peak times. However, they are unsure
which JDBC driver to use. What type of JDBC driver would you
recommend and why?

[6 marks]

(c) Compare and contrast the use of servlets and java server pages. In your
answer, stress the relative advantages and disadvantages of these two
technologies.

[12 marks]

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 11 of 14
Approved

3. (a) You are the senior developer in a website development team who are
currently developing a large J2EE application. The system you have
developed is running slowly. You have checked potential problems
such as replication issues and these would appear to be ok so you are
now looking at the code that your colleagues have written and you are
horrified! Their code is really inefficient and your line manager has
now asked you to prepare notes for a seminar to your colleagues on
how to write efficient code so they can make amendments. Prepare
notes for this seminar. Your answer should include a discussion of how
to make use of caching.
[14 marks]

(b) Critically evaluate two design patterns that you have learned about
EXCLUDING those mentioned in question 1 (DAO, TO and
Singleton). Your answer should include an example of where you
might choose to use each one.

[16 marks]

4. (a) Identify the testing phase of a Java software system where the
framework JUnit may be used, and describe briefly the way this
framework operates.

[5 marks]

(b) As an experienced web developer, you have been asked to give a


seminar to some junior developers about how the Struts framework
works. Describe the sequence of events that occurs in processing a user
request and all the Struts components involved.

[12 marks]

Question 4 is continued on the following page

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 12 of 14
Approved

(c) Study the following code:

//Listing 1
public interface Stack<E> {
public int size();
public boolean isEmpty();
public boolean push (E element); //returns false if stack is full
public E pop(); //returns null if stack is empty
} //end code fragment Stack

//Listing 2
public class ArrayStack<E> implements Stack<E> {
protected int capacity;
public static final int CAPACITY = 1000;
protected E s[];
protected int top = -1;

public ArrayStack() {
this(CAPACITY);
}

public ArrayStack(int cap) {


capacity = cap;
s = (E[]) new Object[capacity]; // compiler may give warning but ok
}

public int size() {


return (top + 1);
}

public boolean isEmpty() {


return (top < 0);
}

public E pop() {
if (isEmpty()) return null;
else return s[top--];
}

public String toString() {


String str;
str = "[";
if (size() > 0) str += s[0];
if (size() > 1)
for (int i = 1; i <= size()-1; i++)
str += ", " + s[i];
return str + "]";
}
} //end code fragment ArrayStack

Question 4 is continued on the following page


___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 13 of 14
Approved

(i) Explain what this code does (both Listing 1 and Listing 2). In
particular, comment about the use of E, capacity and
CAPACITY.

[8 marks]

(ii) Compare and contrast Java interfaces and abstract classes.


What difference would it make to the code in Listing 2 if the
key word interface is replaced by abstract?

[5 marks]

___________________________________________________________________
Object Oriented Software Development
COMP1307

Page 14 of 14

You might also like