0% found this document useful (0 votes)
90 views4 pages

Question Arraylist and Linkedlist

This document contains questions and solutions related to ArrayLists and LinkedLists in Java. Question 1 involves adding elements to an ArrayList and performing various operations like finding maximum value, summing even elements, and counting elements divisible by 3. Question 2 involves drawing the logical diagram of a LinkedList after adding new nodes. Question 3 provides the ADTs for Customer and ArrayList and asks to insert Customer objects, count by category, and sort into multiple ArrayLists. Question 4 similarly provides the ADTs for Delivery and LinkedList and asks to insert records, display selected fields, and search/remove records between two LinkedLists.

Uploaded by

AISYAH FARHANA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views4 pages

Question Arraylist and Linkedlist

This document contains questions and solutions related to ArrayLists and LinkedLists in Java. Question 1 involves adding elements to an ArrayList and performing various operations like finding maximum value, summing even elements, and counting elements divisible by 3. Question 2 involves drawing the logical diagram of a LinkedList after adding new nodes. Question 3 provides the ADTs for Customer and ArrayList and asks to insert Customer objects, count by category, and sort into multiple ArrayLists. Question 4 similarly provides the ADTs for Delivery and LinkedList and asks to insert records, display selected fields, and search/remove records between two LinkedLists.

Uploaded by

AISYAH FARHANA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like