La Trobe University, Bendigo

EXAMINATION JUNE 2004

INT21OOP Object-Oriented Programs

Examiner:  Mary Martin                                                                 Time Allowed: 2.5 hours

     ___________________________________________________________________

Instructions to Candidates:

1. Answer all questions.

2. Full marks = 80.

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.

     All parts of this question refer to the classes shown in the Class Diagram given below. The code for all classes depicted are also given.

//class Car

public class Car extends Vehicle{

    public Car(){

        this("Holden, Astra", "4 Cylinder-Petrol", 4);

    }

    public Car(String m, String e, int w){

        super(m,e,w);

    }

    public String promoMessage(){

        return "Holden drives you...further!";

    }

}

//class Bus

public class Bus extends Vehicle{

    public Bus(){

        this("Denning", "Detroit Diesel",12);

    }

    public Bus(String m, String p, int mb){

        super(m,p,mb);

    }

    public String promoMessage(){

        return "Denning...does delightful driving!";

    }

}

//abstract class Vehicle

public abstract class Vehicle implements Comparable{

    private String manufacturer;

    private String engineType;

    private int numWheels;

    public Vehicle(String m, String e, int w){

        manufacturer = m;

        engineType = e;

        numWheels = w;

    }

    public void setManufacturer(String m){

        manufacturer = m;

    }

    public void setEngineType(String e){

        engineType = e;

    }

    public void setNumWheels(int w){

        numWheels = w;

    }

    public String getManufacturer(){

        return manufacturer;

    }

    public String getEngineType(){

        return engineType;

    }

    public int getNumWheels(){

        return numWheels;

    }

    public int compareTo(Object o){

        Vehicle other = (Vehicle)o;

        if (this.manufacturer == other.getManufacturer())

             return this.engineType.compareTo(other.getEngineType());

    }

    public String toString(){

        return  manufacturer +", "+engineType+", with "+numWheels+" wheels";

    }

    public abstract String promoMessage();

}


//test class VehicleTest

public class VehicleTest

{

    static int size = 10;
    public static void main(String[] args){

        if (args.length > 0)

            size = Integer.parseInt(args[0]);

        Vehicle thePark[] = new Vehicle[size];

        Car aCar = new Car();

        Bus aBus = new Bus();

        thePark[0] = aCar;

        thePark[1] = aBus;

        thePark[2] = new Car("Ford","6 Cylinder",4);

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

            if (thePark[i]!=null)

                System.out.println(thePark[i].toString());

            else

                System.out.println(ÒParkÓ +(i+1)+ Òis empty!Ó);

        }

        Vehicle checkCar = new Car();

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

            if(thePark[i]!=null)

                if ((checkCar.compareTo(thePark[i]))==0)

                    System.out.println(checkCar.promoMessage());

        }  

    }   

}


Parts a), b), c), d), e) and f) of this question refer to code in the classes Vehicle, Car and Bus.

a)     In the code segment: public abstract class Vehicle { What  does the reserved word abstract mean ?

b)    In the statement: public abstract String promoMessage(); What  does the reserved word abstract mean ?

c)     What does the code segment Car extends Vehicle  mean? What fundamental Object-Oriented concept is implemented by this code?

d)    Using the class Vehicle explain the effect of the modifiers public and private.

e)  Explain what super means and how the following statement executes:                                        super(m,p,mb);

f)     Explain what this means how the following statement executes:                                                                           this("Denning", "Detroit Diesel",12);

Parts g), h), i) and j) of this question refer to the code defined in the class called VehicleTest  that in turn tests some of the methods of the classes Vehicle, Car and Bus by creating objects of type Car and Bus and sending messages to them.

g)     The testing class VehicleTest uses two repetition constructs to process the array of Vehicle objects. The objects stored in the array are in fact objects of type Car and Bus yet the compiler does not generate a syntax error. Why? In your explanation identify the fundamental concept that this code demonstrates.

h)     Show the output that is generated when VehicleTest is executed with:

a.     The command line argument is entered with a value of Ò5Ó

b.     No command line argument is entered

c.     The command line argument is entered with a value of ÒmÓ

i)      One section of the test code may generate an exception causing the program to stop. Identify the statement that causes this and the type of Exception that may be generated.

j)      Modify the testing code so that the exception is caught and managed by the program.

(2 + 2 + 2 + 2 + 2 + 2 + 3 + (4 + 3 + 3) + 2 + 3 = 30 marks)


Question 2.

a)     What is a design pattern? Briefly explain why it is advisable to follow design patterns when implementing programming solutions.

b)    Gamma, Helm, Johnson & Vlissides [1995] described a basic set of 23 patterns and grouped them into three categories. Identify the three major categories of design patterns.

c)     For two design patterns you have implemented this semester:

a.     Identify the design pattern and its purpose.

b.     Explain how you used it in your assignment application.

d)    The Class Diagram below is taken from an example discussed in class. It shows the relationships between the various classes of the objects that interact to provide the applet called Sort2 (and represented by the Sort2 class shown below) and implement three design patterns. Name two of the design patterns implemented with this set of classes and identify the classes which implement them.

(3 + 3 + (4 + 4) + (3 + 3) = 20 marks)

Question 3.

a)     The example classes given below implement the client/server model of process communication that is common in distributed computing. Describe the basic function of the client process and the basic function of the server process.

b)    Briefly describe what happens when the class CounterServer is executed. In your explanation identify the statements that provide socket-based communication.

c)     Briefly describe what happens when the class CounterClient is executed. In your explanation include a description of how the connection is established with the server process.

d)    Show the output (Graphical Output and file ) obtained from executing the client process

a.     the first time

b.    subsequent times

// Example 12.4 from Subject TextBook

// CounterServer

import java.io.*;

import java.net.*;

public class CounterServer{

   public static void main(String[] args){

       int i = 1;

       try{

            InputStream fin = new FileInputStream("Counter.dat");

            DataInputStream din = new DataInputStream(fin);

            i = din.readInt()+1;

            din.close();

        }catch(IOException e){e.printStackTrace();}

        try{

            ServerSocket s = new ServerSocket(8190);

            while(true){

                Socket incoming = s.accept();

                DataOutputStream out =

                new DataOutputStream(incoming.getOutputStream());

                System.out.println("Count: "+i);

                out.writeInt(i);

                incoming.close();

                OutputStream fout = new FileOutputStream("Counter.dat");

                DataOutputStream dout = new DataOutputStream(fout);

                dout.writeInt(i);

                dout.close();

                out.close();

                i++;             

            }

        }catch(Exception e){e.printStackTrace();}

    }

}

import javax.swing.*;

import java.net.*;

import java.awt.*;

import java.io.*;

//class Counter

public class Counter extends JApplet

{

   protected int count=0;

   protected Font font = new Font("Serif",Font.BOLD,24);

   public void init()

   {

      URL url = getDocumentBase();

      try{

            Socket t = new Socket(url.getHost(), 8190);

            DataInputStream in = new DataInputStream(t.getInputStream());

            count = in.readInt();

        } catch (Exception e){

            e.printStackTrace();

        }

   }

   public void paint(Graphics g)

   {

      // simple text displayed on applet

      int x = 0, y = font.getSize();

      g.setColor(Color.blue);

      g.drawString("You are Visitor:"+ count, x, y);

   }

}

(4 + 5 + 5 + (3 + 3) = 20 marks)

Question 4.

a)     Why is the development of the software component desirable?

b)    If a class is to be considered a component what requirements must that class meet?

c)     Briefly identify two major commercial component models
.

d)    In 2000 the international Standards organization, OMG (Object Management Group) proposed the MDA (Model Driven Architecture) to replace OMA (Object Management Architecture) as an approach to software engineering. Briefly explain what this means and what led to this major change.

(2 + 3 + 2 + 3 = 10 marks)