SESSION 3: Arraylist and Linkedlist CSC248
QUESTION 1
What is the output for this program segment?
ArrayList<Integer> theALData = new ArrayList<>();
for (int i = 7; i > 2; i--)
{
theALData.add(5 + i * 1 );
}
System.out.println(theALData);
int myValue = theALData.get(0);
for (int i = 1; i < theALData.size(); i++)
{
if (theALData.get(i) > myValue)
{
myValue = theALData.get(i);
}
}
System.out.println(myValue);
int eventSum = 0;
for (int i = 0; i < theALData.size(); i++)
{
if (theALData.get(i) % 2 == 0)
{
eventSum += theALData.get(i);
}
}
System.out.println(eventSum);
int divi3Counter = 0;
for (int i = 0; i < theALData.size(); i++)
{
if (theALData.get(i) % 3 == 0)
{
divi3Counter++;
}
}
System.out.println(divi3Counter);
}
}
QUESTION 2
Given the logical diagram of a linked named petLL:
petsLL
Three new pets are added into the petsLL which are cat, goat and turtle. Draw the
logical diagram of petsLL after the following segment is executed to add the new pets. (Note that
the new pets are added in order as listed above. While front, rear, current and previous are the reference
variable)
Node newNode = new Node <String>(insertItem);
current = front.next;
newNode.next = current.next;
current.next = newNode;
Node newNode = new Node <String>(insertItem);
newNode.next = front;
front = newNode;
Node newNode = new Node <String>(insertItem);
newNode.next = front.next;
front.next = newNode;
QUESTION 3
The following are the ADTs of Customer and Arraylist.
public class Customer
{
private String customerID;
private String name;
private int age;
private String category; // e.g., Regular, Premium, VIP
// Normal constructor
// Accessor methods: getCustomerID(), getName(), getAge(),
getCategory()
// Printer method: toString()
public class ArrayList {
public ArrayList() {…}
public void add(Customer data) {…}
public int size() {…}
public Customer get(int index) {…}
public Customer remove(int index) {…}
// Definition of other methods
}
Write program segments to answer the following questions:
a) Insert 20 records of Customer objects into an arraylist named customerAL.
b) Count and display the number of Premium customers.
c) Instantiate three arrayLists and move customers with the different category into the
corresponding arrayLists. (e.g., regularCustAL, premiumCustAL, VIPCustAL)
d) Calculate and display the average age of VIP customers whose age exceeds 30.
QUESTION 4
The following is the ADTs of Delivery and LinkedList.
public class Delivery
{
private String driverName;
private String placeName;
private String receiverName;
private int totalPrice;
private String phoneNumber;
//constructors
//setters
//getters
//printer
}
public class LinkedList
{
public LinkedList(){…}
public void insertAtBack(Object elem) {…}
public Object getFirst(){…}
public Object getNext(){…}
public Object removeFromFront(){…}
}
i) Write the program segment to insert 20 records of delivery into a LinkedList object named
deliveryLL.
ii) Display driverName, receiverName and phoneNumber from Jasin Melaka based on the
following format.
Driver Name Receiver Name Phone Number
xxxxx xxxxx xxxxx
iii) Search and remove record which its total price is less than RM 100 from deliveryLL and
move to a new linked list called smallBudget. If the record is found, display “Record has
been added” otherwise display “The record is not found”.
END OF ANSWER SCHEME