Examiner: Mary Martin Time Allowed: 3 hours
___________________________________________________________________
Instructions to Candidates:
1. Answer all questions.
2. Full marks = 100.
3. Not all questions carry equal marks.
4. Figures to the right indicate marks.
5. Make suitable assumptions where necessary.
6. Clearly state any assumptions made.
Question 1.
Parts a), b) and c) of this question refer to the Java code given below. The
code defines classes called Shape, Square and Circle.
//Shape.java
public class Shape {
protected int x, y;
public Shape (int ix, int iy){
x=ix; y = iy;
}
public String describe(){
return "unknown shape";
}
}
//Square.java
public class Square extends Shape{
protected int side;
public Square(int ix, int iy, int is){
super(ix, iy); side = is;
}
public String describe(){
return "square with side " + side;
}
}
//Circle.java
public class Circle extends Shape{
protected int radius;
public Circle(int ix, int iy, int ir){
super(ix, iy); radius = ir;
}
public String describe(){
return "circle with radius " + radius;
}
}
a) What does the code Circle extends Shape mean? What fundamental Object-Oriented
concept is implemented by this code?
b) What is the effect of the
modifiers public and protected ?
c) What does the statement super(ix, iy); do?
Parts d), e), f), g) and h) of this
question refer to the Java code given below. The code defines a class called
Q12001 which tests the classes Shape,Square and Circle defined in the first
part of this question.
//Q12001.java
import java.awt.*;
import java.util.*;
public class Q12001 extends Frame {
private Shape form;
private Vector theShapes;
public Q12001() {
super("Q12001");
theShapes = new Vector();
form = new Circle(100, 250, 50);
theShapes.addElement(form);
form = new Square(200, 250, 100);
theShapes.addElement(form);
setSize(300,300);
setVisible(true);
}
public void paint(Graphics g){
if(theShapes != null){
int h= 100, v=100;
int size = theShapes.size();
for (int i=0; i < size;i++){
g.drawString(((Shape)theShapes.elementAt(i)).describe(),h,v);
v+=20;
}
}
}
public static void main(String args[]) {
new Q12001();
}
d) When the program executes, how many Shape objects are created and when will
that happen?
e) Describe, statement by statement, what happens when the paint method is executed.f)
Within the body of the paint method is a statement which demonstrates polymorphism.
Identify the statement and explain what polymorphism is. What features of the
language make polymorphism possible?
g) Why is the code (Shape) required in the statement:
g.drawString(((Shape)theShapes.elementAt(i)).describe(),h,v);
h) For the classes given and using the symbols provided in Appendix A, draw
an object model to show the objects of the program just after the Q12001 object
has been created.
The code below defines an abstract class Shape which could be used as an alternative
to the Shape class given in the first part of this question.
public abstract class Shape{
protected int x, y;
public Shape (int ix, int iy){
x = ix; y = iy;
}
abstract String describe();
}
i) Explain the effect for both occurrences of the modifier abstract
j) What would be an advantage of using the above implementation of the Shape
class?
k) There is another mechanism in the Java language for providing the ability
to implement different implementations of the same method. Identify this mechanism
and show how you would alter the code of classes which extend Shape in the first
part of this question to implement it.
(2 + 4 + 2 + 2 + 5 + 4 + 2 + 4 + 4 + 2 + 4 = 35 marks)
Question 2.
Parts a), b) c) d) and e) of this question refer to the Java code given below.
import java.awt.*;
import java.awt.event.*;
public class CannonWorldV2 extends Frame{
public static final int FWIDTH = 600;
public static final int FHEIGHT = 400;
private CannonDisplay cannonDisplay;
private Button fire;
private Scrollbar slider;
private int angle=45;
private CannonWorldV2(){
setSize(FWIDTH,FHEIGHT);
setTitle("Cannon Game");
cannonDisplay = new CannonDisplay();
add("Center",cannonDisplay);
fire = new Button("fire");
add("North", fire);
slider = new Scrollbar(Scrollbar.VERTICAL, angle, 5, 0, 90);
add("East", slider);
fire.addActionListener(new FireButtonListener());
slider.addAdjustmentListener(new ScrollBarListener());
addWindowListener(new WindowDestroyer());
}
public static void main (String argv[]){
CannonWorldV2 world = new CannonWorldV2();
world.setVisible(true);
}
private class WindowDestroyer extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(1);
}
}
private class FireButtonListener implements ActionListener{
public void actionPerformed (ActionEvent e){
cannonDisplay.doDisplay(angle);
}
}
private class ScrollBarListener implements AdjustmentListener{
public void adjustmentValueChanged (AdjustmentEvent e){
angle = slider.getValue();
cannonDisplay.displayAngle(angle);
}
}
}
a) Explain the purpose of the statements: import java.awt.*; import java.awt.event.*; b) Explain the effect of the modifiers: static final c) Identify the object (by its name or type) which: I. has responsibility for the display within the main window frame II. controls the value of the angle variable III. responds to a mouse click on a button IV. responds to a mouse click in the window close box
d) There are three classes within the class CannonWorldV2. What are these classes called? Why are they used?
e) Identify two design patterns in the code and explain what problem each design pattern is solving.
f) Briefly define what a design pattern is. Identify and briefly describe two other design patterns that you may have used in assignment work this semester.
(2 + 2 + 4 + 2 + 4 + 6 = 20 marks)
Question 3.
a) The Java Application Programming Interface (API) provides the class Thread
and the interface Runnable to implement lightweight concurrency. What does lightweight
concurrency mean? Give an example of when it would be beneficial to use lightweight
concurrency.
b) When lightweight concurrency is used, methods of the involved classes often
have the modifier synchronized in their definition. What is the effect of this
keyword? Give an example of when a method might be defined as synchronized.
(4 + 4 = 8 marks)
Question 4.
a) What requirements must be met if a class is to be considered as a component?
Are the awt classes from the Java’s Application Programming Interface
(API) components?
b) You can use a component builder like Sun's beanbox at three quite different
levels of programming. Briefly identify and describe those three levels of programming.
c) What is the name given to Sun's component model? Name another popular component
model.
d) Briefly define the term Software Engineering, its aims and its relationship
to the Object-Oriented approach to programming.
(4 + 3 + 2 + 3 = 12 marks)
Question 5.
La Trobe University, Bendigo has commissioned you to develop an application
which will allow the School of Business & Technology staff to keep track
of their student’s assignments. To do this, you must maintain a database
of details about each assignment. The interface to your Student Assignment Management
System (SAMS) will be a Graphical User Interface (GUI).
Each assignment is described by an assignment number, the name of the student
who has done the assignment, the subject that it was done for and the result
(a percentage mark).
The data for the application will be stored externally in files or a database
(doesn't matter which). The application will load the necessary data when the
program starts up.
The SAMS must allow for displays and printouts of
- all assignments currently with the lecturer,
- an assignment's individual details.
The system must also allow for adding an assignment, deleting an assignment
and modifying the details of an assignment.
a) Identify the objects necessary to create the application (including internal and external data structures if necessary) and assign class names to them. Of these classes identify any which you could expect to find in an existing class library, and explain why you could expect to find them in a class library.
b) For the classes you would not expect to find in a class library, describe them by listing for each:
- class name,
- super (or base) classes (if any),
- sub (or derived) classes (if any),
- instance variables (including data type)
- methods (including a description of its purpose)
c) Identify the inheritance or client relationships which exist between each of the objects in the system by describing them in the following format:
<objecta> ‘is a’ type/kind of <objectb> for an inheritance relationship, and
<objecta> ‘has a/many’ <objectb> for a client relationship
(5 + 15 + 5 = 25 marks)