April 2000
JP219 : JAVA PROGRAMMING

QUESTION 2

Total Marks: 15 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 2

(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 ]

(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 ]
(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 ]
(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 ]