April 2000
JP219 : JAVA PROGRAMMING

QUESTION 2

Total Marks: 15 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 2

Do not award half marks. Do not deduct marks for trivial syntactic errors. Alternative correct answers should be given credit, unless otherwise indicated in the marking scheme.

(a)In Java,objects are always passed by reference Briefly explain what is meant by passing an object by reference,and identify how this differs to the way in which primitive data types are passed.[2 marks ]
Encapsulation adds to the quality of code because:
•Passing objects by reference means that changes made by the calling method are reflected in the object after the method has finished.(1 mark)
•Primitive data types are passed by value,which means that changes are not
reflected after the method has finished.(1 mark)
[2 marks ]

(b)Assume the following definition of the dynamic queue data type,DynamicQueue
public Class DynamicQueue {
   public QueueElement HeadOfQueue;
   public DynamicQueue() {
      HeadOfQueue= NULL;
   }
}
(i)Define the class QueueElement such that it contains the following members,
ensuring that the members are given appropriate access permissions:
•The Data which is of type String
•A reference NextQueueElement which is used to refer to the next
QueueElement in the queue;
•Accessor functions to return these two members.
[5 marks ]
(i)A sample definition of QueueElement follows:
public class QueueElement {
  protected String Data;
  protected QueueElement NextQueueElement;
  public String GetDataElement() {return Data;}
  public QueueElement GetNextQueueElement() {return NextQueueElement};
}
And the following marking scheme should be used:
•A correctly structured class statement;(1 mark)
•Declaration of the NextQueueElement member;(1 mark)
•Declaration of the Data member;(1 mark)
•Defining the GetDataElement() accessor method;(1 mark)
•Defining the GetNextQueueElement() accessor method.(1 mark)
[5 marks ]

(ii)Add to your QueueElement class a constructor function which takes as a
parameter an initial value of the Data when the object is constructed,and will
set the reference to the NextQueueElement to NULL [3marks ]
A sample definition of the constructor function follows:
public QueueElement(String MyString) {
  DataElement= new String(MyString);
  NextQueueElement= NULL;
}
And the following marking scheme should be used:
•Allocating the DataElelement member;(1 mark)
•And initialising it with the String constructor function;(1 mark)
•Setting the NextQueueElement reference to NULL (1 mark)
[3 marks ]

(iii)Write a method,called PopHead the signature of which is given below.The
method should be a member ofthe class DynamicQueue andshouldremovethe
element at the head of the queue,returning the string encapsulated in that
element.
public String PopHead(); [5 marks ]
A sample definition of PopHead follows:
public String PopHead() {
   String rvalue;
   rvalue= new String(HeadOfQueue.DataElement);
   HeadOfQueue= HeadOfQueue.NextQueueElement;
   return rvalue;
}
And the following marking scheme should be used:
•Declaring the local variables rvalue (1 mark)
•Allocating the rvalue object;(1 mark)
•With the value stored at HeadOfQueue.DataElement (1 mark)
•Updating the reference to the HeadOfQueue (1 mark)
•Returning the string.(1 mark)
[5 marks ]