TEST CSC248 Dis2021 A
TEST CSC248 Dis2021 A
TEST CSC248 Dis2021 A
INSTRUCTIONS TO CANDIDATES
2. Answer ALL questions in English. Start each answer on a new page. You are given THREE
(3) hours to answer these questions, therefore spend them wisely.
Name
Student ID
Phone No.
Group
Lecturer’s Name
Campus/Branch
Part B 60
a) For each of the following problems, choose the data structure to solve the problem and
explain why your answer works better than other options:
i) Given runners’ names in 1st, 2nd,..., nth places, the announcer needs to announce
the awards in reversed order, which means the first place is announced last.
(2.5 marks)
Answer:
ii) Trucks drive onto a toll bridge from one end and exit from the other end of the bridge.
iii)
(2.5 marks)
Answer:
b) Briefly explain what happens when insertion is performed in ArrayList and LinkedList.
(5 marks)
Answer:
Array List works on Array and when we add an element in middle of the list, Array
List need to update the index of all subsequent elements. It also shifts the element
currently at that position (if any) and any subsequent elements to the right in order
to give a place to a new element.
Linked List works on doubly linked list algorithm. Insertions can be made without
moving any elements; only the links are altered (adjust the address of the previous
and next elements).
animals.add("CAT");
animals.add("FISH");
animals.add("DOG");
animals.add("ELEPHANT");
animals.add("MOUSE");
animals.add("HORSE");
#aaa
for(int i =0;i<animals.size();i++){
for(int j=0; j<animals.size()-1; j++){
if(animals.get(j).compareTo(animals.get(j+1))<0)//***
{
String value = animals.get(j);
animals.set(j, animals.get(j+1));
animals.set((j+1), value);
}
}
}
for(int i =0;i<animals.size();i++)
System.out.println(animals.get(i));
MOUSE
HORSE
FISH
ELEPHANT
DOG
CAT
ii) Identify and explain the algorithm used after the line statement //#####C in the
program above.
(3 marks)
Answer:
if(animals.get(j).compareTo(animals.get(j+1))<0)//***
(2 marks)
Answer:
Comparing two element in the list weather data in index j is smaller than
element j+1.
iv) Rewrite the code in (iii) if you want to display the elements in different order.
(2 marks)
Answer:
if(animals.get(j).compareTo(animals.get(j+1))>0)
System.out.println(numList.get(3) * 2);
if (numList.get(i) % 2 == 0)
numList.remove(i);
System.out.println(numList);
ANSWER
22 -- 2m
//normal constructor
//Accessor methods:
//getBrand(),getType(),getPrice(),getMaterial()
//printer method: toString()
}
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 6 CS/ DEC 2021 / CSC248
Assume that 50 objects of Bicycle have been inserted into an ArrayList named bicycleAL.
Write a program segment for each of the following:
0.5m
for (i=0; i< bicycleAL.size(); i++)
{ 1m
Bicycle b = (Bicycle) bicycleAL.get(i);
1m
if (b.getType().equalsIgnoreCase("folding")) 1m
if (b.getMaterial()== ‘B’ ) 0.5m
count++;
}
System.out.println("The number of steel folding bike " 0.5m
+count);
(5 marks)
(5.5 marks)
Answer
ArrayList aluminiumAL = new ArrayList(); 0.5m
if (b1.getMaterial() == ‘A’)
aluminiumAL.add(b1); 0.5m
i--;
} 1m
iii) Calculate and display the total price of Polygon bike in steelAL which price is
more than RM3000.
(4.5 marks)
Answer
double total = 0.0; 0.5m
0.5m
for (i=0; i< steelAL.size(); i++)
{ 1m
Bicycle b2 = (Bicycle) steelAL.get(i);
0.5m
if (b2.getBrand().equalsIgnoreCase(“Polygon”))
0.5m
if (b2.getPrice() > 3000) 1m
total = total + b2.getPrice();
} 0.5m
System.out.println("The total price is " + total);
10 5 60 11 40 33 20 55
Number is 13
Number is 30
Number is 5 //0.25M each
Number is 20
Number is 16
Number is 10
//constructor
//mutator : setType(String), setArea(String), setNumUnit(int),
setPrice(double)
//retriever : getType(), getArea(), getNumUnit(), getPrice()
//processor
//printer
}
Berjaya Enterprise is offering discount to their customers. The discount rates are shown in the
table below:
i) Write the program segment to input 50 House objects and store them into a
LinkedList object named houseLL.
(3.5 marks)
Answer:
ii) Get all semi-D and terrace houses from houseLL and store them into LinkedList
objects named semiDLL and terraceLL respectively. Otherwise, store them into
another LinkedList object named otherLL.
(5 marks)
Answer:
if(hs.getType().equals(“semiD”)) //1M
semiDLL.insertAtBAck(hs);
else if (hs.getType().equals (“terrace”)) //1M
terraceLL.insertAtBAck(hs);
else //1M
otherLL.insertAtBAck(hs);
hs = (House)houseLL.getNext(); //0.75M
}
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 11 CS/ DEC 2021 / CSC248
iii) Calculate and display the price after discount for each house in terraceLL.
(7.5 marks)
Answer:
System.out.println(hs.toString()); //1M
System.out.println(“Price after discount: RM” +
price);
hs = (House)terraceLL.getFirst(); //0.75M
}