Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

LAB 4: ARRAYS

Project naming convention


 Create a project named: pLab04_Arrays_ID
 With ID stands for student’s identify, p is short for project
 Ex: If your student’s ID is 9600278, so your project name will be:
pLab04_Arrays_9600278
 Pay attention to “_” (underscore)
Package naming convention: Each project has one or many packages.
 Each package has a specific name
 Each Lab has one package name. Remember to name the packages as follow: labNo_ID
 With No is a number of each lab, ID stands for student’s identify
 Ex: lab01_9600278, lab02_9600278
 Remember to use lowercase l
Class naming convention (Files): Each package has one or many classes
 Each class has a specific name
 Remember to name the classes (classname) as follow:
LabNo_ID and TestLabNo_ID
 With No is a number of each lab, ID stands for student’s identify
 Ex: Lab01_9600278, TestLab01_9600278, Lab02_9600278,
TestLab02_9600278
 Remember to capitalize each word L, T and L

1. Create a class named ArrayList that has fields:


- int n;
- int []a = new a[10];
Implement following methods:
- public void add(int element)
Appends the specified element to the end of this list.
Parameters:
e - element to be appended to this list

- public int size()


Returns the number of elements in this list.
- public int get(int index)
Returns the element at the specified position in this list.
Parameters:
index - index of the element to return
Returns:
the element at the specified position in this list
Throws:
Print out to console the message “out of range” if the index is out of range
(index < 0 || index >= size())

- public void add(int index,int element)


Inserts the specified element at the specified position in this list. Shifts the
element currently at that position (if any) and any subsequent elements to the
right (adds one to their indices).
Parameters:
index - index at which the specified element is to be inserted
element - element to be inserted
Throws:
Print out to console the message “out of range” if the index is out of range
(index < 0 || index > size())

- public boolean contains(int element)


Returns true if this list contains the specified element. Otherwise return false
Parameters:
element - element whose presence in this list is to be tested
- public void clear()
Removes all of the elements from this list. The list will be empty after this call
returns.
- public int indexOf(int element)
Returns the index of the first occurrence of the specified element in this list, or
-1 if this list does not contain the element.
Parameters:
element - element to search for
- public int lastIndexOf(int element)
Returns the index of the last occurrence of the specified element in this list, or -
1 if this list does not contain the element.
Parameters:
element - element to search for

- public boolean isEmpty()


Returns true if this list contains no elements.

- public void remove(int index)


Removes the element at the specified position in this list. Shifts any subsequent
elements to the left (subtracts one from their indices).
Parameters:
index - the index of the element to be removed
Returns:
the element that was removed from the list
Throws:
Print out to console the message “out of range” if the index is out of range
(index < 0 || index >= size())

- public void remove(int element)


Removes the first occurrence of the specified element from this list, if it is
present. If the list does not contain the element, it is unchanged.
Parameters:
element - element to be removed from this list, if present

- public int set(int index,int element)


Replaces the element at the specified position in this list with the specified
element.
Parameters:
index - index of the element to replace
element - element to be stored at the specified position
Returns:
the element previously at the specified position
Throws:
Print out to console the message “out of range” if the index is out of range
(index < 0 || index >= size())

- public int[] subList(int fromIndex,int toIndex)


Returns a view of the portion of this list between the specified fromIndex,
inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the
returned list is empty)
Parameters:
fromIndex - low endpoint (inclusive) of the subList
toIndex - high endpoint (exclusive) of the subList
Returns:
a view of the specified range within this list
Throws:
Print out to console the message “out of range” if endpoint index value out of
range (fromIndex < 0 || toIndex > size)
Print out to console the message “fromIndex greater than toIndex” if the
endpoint indices are out of order (fromIndex > toIndex)

- public String toString()


Returns a string representation of this collection. The string representation
consists of a list of the collection's elements in the order they are returned by its
iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by
the characters ", " (comma and space). Elements are converted to strings
Returns:
a string representation of this collection

- public void trimToSize()


Trims the capacity of this Array instance to be the list's current size. An
application can use this operation to minimize the storage of an ArrayList
instance.
2. Create a class named TestArrayList to call mehods from class ArrayList. Class
TestArray has the following fields:
- ArrayList arr = new ArrayList();
Implement the following methods which will invoke methods of Array class for
processing:
- Input n integer numbers of an Array object (arr)
- Output an Array object
- Caculate the sum of array elements which is divisible by 2 and 5.
- Caculate the last minimum sum of two consecutive elements
- Caculate the average of odd array elements
- Count the number of prime array elements
- Check if the given array is a symmetric array or not
- Check if the given array is increasing
- Delete negative array elements
- Given an increasing array. Insert into that array x value to make it remain
increasing
- Reverse the array elements
- Find the longest increasing subsequence of an integer sequence
- Find the longest subsequence which is the longest arithmetic progression of an
integer sequence
- Find the longest sawtooth sequence in an integer sequence
- Sort the array to make it increasing
- Sort the given array to make a new one that contains even and divisible by 3
elements at the beginning, odd and divisible by 3 elements at the end and the
remains in the middle.

3. Create a class named TestArray1 with the following fields:


- Array m1 = new Array();
- Array m2 = new Array();
Implement the following methods which will invoke methods of Array class for
processing:
- Compute m3 = m1 + m2 by adding together the pairs of corresponding
elements, the results will be put in m3
- Sort m1, m2 to make them increasing
- Merge m1 and m2 to make increasing array m3
- Compute m3 that m3 = m1 \ m2
- Compute m3 that m3 = m2 \ m1
- Compute m3 that m3 = m1  m2
- Compute m3 that m3 = m1  m2

You might also like