December 1999
JP219 : JAVA PROGRAMMING

QUESTION 2

Total Marks: 15 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 2

(a)

One of the most important features of object oriented programming languages is the ability to design abstract data types .

Briefly explain the difference between an abstract data type and a primitive data type .

[2]
(b)

You are required to implement a Queue abstract data type.

(i)Define a class,Queue ,which contains the following member variables:
•A protected statically sized array of 100 integers,called DataList ;
•A boolean variable Empty ,which is used to indicate if the queue is empty;
•Two iterators Front and Back ,which can be used to hold the array indexes of the front and back of the queue. [3 marks ]


(ii)Write a constructor method for your class,which will initialise every element of the DataList to zero,initialise the Front and Back iterators to zero,and initialise the Empty variable to indicate an empty queue.[3 marks ]

(iii)Write a method,called PeekHead ,the signature of which is given below,which is a member of your Queue class.The method should return the data element at the front of the queue,without removing the element,and return zero if the queue is empty.
int PeekHead(); [3 marks ]

(iv)Write a method,called Add ,the signature of which is given below,which is a member of your Queue class.The method should add the element x to the Back of the queue if there is space,whilst ensuring that the Back iterator is correctly positioned.You should assume that there will always be space in the array for the element to be added.[4 marks ]
void Add(int x);

[13]