Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CONFIDENTI CS/JUL

QUESTION 1 (20 marks)

NF AutoExtremeCar is a car company in Penang. This company sells good quality cars
from various models and keeps the details of its cars using sequential lists (ArrayList). The
following are the ADTs of Car and ArrayList.

public class Car


{
String pNo // plate Number, eg: WDF133
String model // eg: Honda Civic, Toyota Vios, Proton X70
etc. int year; // year of manufacture
double price;

// normal constructors
// mutators and accessors
// toString() printer
}

public class ArrayList {


public ArrayList () //default constructor
public boolean add (Object item) //insert at back
public Object get (int index) //return element from the specified
//location
public Object remove (Object item) //remove specified element
public Object set (int index, Object item)//replace with
//specified element at
//specified
location public int size() //return the size of the list

/*** Definition of the other methods ***/


}

Write a Java application program segment to do the following tasks:

a) Declare and create an object of ArrayList named carList. Then write


statements to get TEN (10) car objects from user and insert into carList. Assume
Scanner object has been declared and created.
(3.5 marks)
// Declare and create an object of ArrayList named carList
Arraylist carList = new ArrayList();

// To get TEN (10) car objects from user and insert into carList
for (int i = 0; i < 10; i++)

b) Update the car plate number from “DDC14” to “DDE14”. After updated, display the
car particulars with that new plate Number.
(3 marks)

c) Remove all car objects that were manufactured in year 2020 and insert them into
another ArrayList named newList.
(3.5 marks)
1
CONFIDENTI CS/JUL

Given ADTs of CounterService and Queue classes:

public class
CounterService{ private
String waitID;
private String caterogy; //Personal, Business
private char serviceType; //A-Banking, B-Other services

public CounterService(String, String, char);


public String getWaitID();
public String
getCategory(); public char
getServiceType();
}

public class
Queue{ public
Queue();
public void

Given a Queue object named customerList has been created and inserted with customer
details. Write java program segments to perform the tasks below.

d) Copy customer details from the customerList into two (2) Queue objects named
waitListA and waitListB according to the service type as in the following table.
The original elements are remained in the customerList using a temporary
Queue object.

Waiting List Service Type


waitListA Banking

waitListB Other services

(6 marks)

e) Display the customer details with category of business from customerList.

(4 marks)

2
CONFIDENTI CS/JUL

QUESTION 2 (20 marks)

a) Write a program fragment which uses a stack to convert a decimal number to its
equivalent octal representation. You could use the following example as a guideline.

For example, to convert 825 to octal form, manually this is how the conversion is
done: 825 is divided by 8 continuously, then the remainder of each division is taken in
reverse order to form the octal number.

Division Remainder
i. 825 / 8 = 103 1
ii. 103 / 8 = 12 7
iii. 12/ 8 = 1 4
iv. 1/8 = 0 1

Thus, the octal form for 825 is 1471 (the remainder in reverse order)
(6 Marks)

b) What is the output of the following program? (Assume no error in the program)

public class Exercise6 {


public static void main (String [] args){
StackClass s1 = new StackClass ();
StackClass s2 = new StackClass ();
String list [] = {“winter”, “Spring”, “ Summer”, “ Fall”,
“Cold”, “warm”, “Hot”, “Holiday”};

for (int i = 0 ; i < 8; i++)


s1.push (new StringElement (list [i])) ; mystery (s1, s2);
while (!s2.isEmptyStack ())
{

System.out.print (s2.top() + “ “ );
s2.pop ();
}
System.out.println ();
}
public static void mystery (StackClass s, StackClass t){ while(!s.isEmptyStack(
t.push (s.top ()) ; s.pop ( ) ;
}
}
} // end main

(4 Marks)

3
CONFIDENTI CS/JUL

c) Given the following method 'foo', determined the returned value of foo(22,8). You
MUST show the tracing of this recursive function.

public static int foo(int a, int b)


{
if( a < b ) {
return b;
}
int diff = a – b;
return foo( diff, b-1) + foo( diff, b+1);
}
(10 Marks)

You might also like