Lab Assignment 2 - ArrayList
Lab Assignment 2 - ArrayList
Question 1
Declare a list to hold integer numbers. Then you have to do the following operation
i. Add new element into a list
ii. Delete element from a list
iii. The number of elements in a list
iv. Calculate the total of number in a list
v. Print all elements in a list
Write a program to solve the problem by using ArrayList class. Use an appropriate menu
selection to perform the option as given above.
Question 2
Declare a list to hold a collection string of name. Then you have to do the following
operation
i. Add new element into a list
ii. Delete element from a list
iii. The number of elements in a list
iv. To determine either the name exist or not in a list
v. Sort the list of names in ascending order
vi. Print all the name in a list
Write a program to solve the problem by using ArrayList class. Use an appropriate menu
selection to perform the option as given above.
1|Page
Question 3
}
public class ArrayList
{
//default constructor
public ArrayList ()
//insert at back
public boolean add (Object elem)
//remove element based on object
public boolean remove (Object elem)
//return element from the specified location
public Object get (int index)
//replace with specified element at specified location
public Object set (int index, Object elem)
//return size of list
public int size();
Write a complete program for the Product ADT. Then, by using the ArrayList ADT as
given above, write a java application to solve the following problems.
2|Page
d) Update the record where the productName is equal to Pen. If the record exists
replace its current value of quantity with 30 and price with RM 1.00
respectively, otherwise display an appropriate message.
e) Remove all records for total price is more than RM 1000 and store them into
listProduct2. Total price is calculated by quantity multiply by price.
f) Display all records from listProduct1 and listProduct2.
3|Page