SlideShare a Scribd company logo
1 of 20
Download to read offline
JAVA
LAB #8 - ARRAY BASED LISTS
The next exercise is based on this implemetation for an UnorderedArrayList of integers:
//Interface: ArrayListADT
//works for int
public interface ArrayListADT {
public boolean isEmpty(); //Method to determine whether the list is empty.
public boolean isFull(); //Method to determine whether the list is full.
public int listSize(); //Method to return the number of elements in the list.
public int maxListSize(); //Method to return the maximum size of the list.
public void print(); //Method to output the elements of the list.
public boolean isItemAtEqual(int location, int item); //Method to determine whether item is
the same as the item in the list at location.
public void insertAt(int location, int insertItem); //Method to insert insertItem in the list at
the position
public void insertEnd(int insertItem); //Method to insert insertItem at the end of the list.
public void removeAt(int location); //Method to remove the item from the list at location.
public int retrieveAt(int location); //Method to retrieve the element from the list at location.
public void replaceAt(int location, int repItem); //Method to replace the element in the list at
location with repItem.
public void clearList(); //Method to remove all the elements from the list.
public int search(int searchItem); //Method to determine whether searchItem is in the list.
public void remove(int removeItem); //Method to remove an item from the list.
}
//Class: ArrayListClass implements
//Interface: ArrayListADT
public abstract class ArrayListClass implements ArrayListADT {
protected int length; //to store the length of the list
protected int maxSize; //to store the maximum size of the list
protected int[] list; //array to hold the list elements
//Default constructor
public ArrayListClass() {
maxSize = 100;
length = 0;
list = new int[maxSize];
}
//Alternate Constructor
public ArrayListClass(int size) {
if(size <= 0) {
System.err.println("The array size must be positive. Creating an array of size 100.");
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new int[maxSize];
}
public boolean isEmpty() {
return (length == 0);
}
public boolean isFull() {
return (length == maxSize);
}
public int listSize() {
return length;
}
public int maxListSize() {
return maxSize;
}
public void print() {
for (int i = 0; i < length; i++)
System.out.print(list[i] + " ");
System.out.println();
}
public boolean isItemAtEqual(int location, int item) {
if (location < 0 || location >= length) {
System.err.println("The location of the item to be compared is out of range.");
return false;
}
return list[location]== item;
}
public void clearList() {
for (int i = 0; i < length; i++)
list[i] = 0;
length = 0;
System.gc(); //invoke the Java garbage collector
}
public void removeAt(int location) {
if (location < 0 || location >= length)
System.err.println("The location of the item to be removed is out of range.");
else {
for(int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
}
public int retrieveAt(int location) {
if (location < 0 || location >= length) {
System.err.println("The location of the item to be retrieved is out of range.");
return 0;
}
else
return list[location];
}
public abstract void insertAt(int location, int insertItem);
public abstract void insertEnd(int insertItem);
public abstract void replaceAt(int location, int repItem);
public abstract int search(int searchItem);
public abstract void remove(int removeItem);
}
//Class: UnorderedArrayList extends
//Super class: ArrayListClass
public class UnorderedArrayList extends ArrayListClass {
public UnorderedArrayList() {
super();
}
public UnorderedArrayList(int size) {
super(size);
}
//Bubble Sort
public void bubbleSort() {
for (int pass = 0; pass < length - 1; pass++) {
for (int i = 0; i < length - 1; i++) {
if (list[i] > list[i + 1]) {
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
}
}
//implementation for abstract methods defined in ArrayListClass
//unordered list --> linear search
public int search(int searchItem) {
for(int i = 0; i < length; i++)
if(list[i] == searchItem)
return i;
return -1;
}
public void insertAt(int location, int insertItem) {
if (location < 0 || location >= maxSize)
System.err.println("The position of the item to be inserted is out of range.");
else if (length >= maxSize)
System.err.println("Cannot insert in a full list.");
else {
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //shift right
list[location] = insertItem;
length++;
}
}
public void insertEnd(int insertItem) {
if (length >= maxSize)
System.err.println("Cannot insert in a full list.");
else {
list[length] = insertItem;
length++;
}
}
public void replaceAt(int location, int repItem) {
if (location < 0 || location >= length)
System.err.println("The location of the item to be replaced is out of range.");
else
list[location] = repItem;
}
public void remove(int removeItem) {
int i;
if (length == 0)
System.err.println("Cannot delete from an empty list.");
else {
i = search(removeItem);
if (i != -1)
removeAt(i);
else
System.out.println("Cannot delete! The item to be deleted is not in the list.");
}
}
}
Add to class UnorderedArrayList a new method called scaleByK that should replace every
integer of value k with k copies of itself. For example, if the list is: [2, 4, -2, 5, 3, 0, 7] before the
method is invoked, it should be [2, 2, 4, 4, 4, 4, 5, 5, 5, 5, 5, 3, 3, 3, 7, 7, 7, 7, 7, 7, 7 ] after the
method executes. Note that the method should remove from the list all 0s and negative values.
Test the method using this client:
//Testing the method scaleByK added to the user created UnorderedArrayList class
public class Lab8_1 {
public static final int SIZE = 100;
public static void main(String[] args) {
UnorderedArrayList list = new UnorderedArrayList(SIZE);
list.insertEnd(2);
list.insertEnd(4);
list.insertEnd(-2);
list.insertEnd(5);
list.insertEnd(3);
list.insertEnd(0);
list.insertEnd(7);
System.out.println("The original list is: ");
list.print();
System.out.println("The list after method call is: ");
list.scaleByK();
list.print();
}
}
1.2. Same problem. This time use the ArrayList class in Java. Write scaleByK as a client method
and use the print method provided.
//Testing the method scaleByK using the Java ArrayList class
import java.util.ArrayList;
public class Lab8_2 {
public static void main(String[] args) {
ArrayList list = new ArrayList ();
list.add(2);
list.add(4);
list.add(-2);
list.add(5);
list.add(3);
list.add(0);
list.add(7);
System.out.println("The original list is: ");
print(list);
System.out.println("The list after method call is: ");
scaleByK(list);
print(list);
}
public static void scaleByK(ArrayList list) {
...
}
public static void print(ArrayList< Integer> someList) {
for(Integer i:someList)
System.out.print(i + " ");
System.out.println();
}
}
The next exercise is based on this implemetation for an OrderedArrayList of integers:
//Interface: ArrayListADT
public interface ArrayListADT {
//same as above, you already have it!
}
//Class: ArrayListClass implements
//Interface: ArrayListADT
public abstract class ArrayListClass implements ArrayListADT {
//same as above, you already have it!
}
//Class: OrderedArrayList extends
//Super class: ArrayListClass
public class OrderedArrayList extends ArrayListClass{
public OrderedArrayList() {
super();
}
public OrderedArrayList(int size) {
super(size);
}
//implementation for abstract methods defined in ArrayListClass
//ordered list --> binary search
public int search(int item) {
int first = 0;
int last = length - 1;
int middle = -1;
while (first <= last) {
middle = (first + last) / 2;
if (list[middle] == item)
return middle;
else
if (list[middle] > item)
last = middle - 1;
else
first = middle + 1;
}
return -1;
}
public void insert(int item) {
int loc;
boolean found = false;
if (length == 0) //list is empty
list[length++] = item; //insert item and increment length
else if (length == maxSize) //list is full
System.err.println("Cannot insert in a full list.");
else {
for (loc = 0; loc = item) {
found = true;
break;
}
}
//starting at the end, shift right
for (int i = length; i > loc; i--)
list[i] = list[i - 1];
list[loc] = item; //insert in place
length++;
}
}
/* Another version for insert:
public void insert(int item) {
int loc;
boolean found = false;
if (length == 0) //list is empty
list[length++] = item; //insert item and increment length
else if (length == maxSize) //list is full
System.err.println("Cannot insert in a full list.");
else {
int i = length - 1;
while (i >= 0 && list[i]> item) {
list[i + 1] = list[i];
i--;
}
list[i + 1] = item; // Insert item
length++;
}
} */
public void insertAt(int location, int item) {
if (location < 0 || location >= maxSize)
System.err.println("The position of the item to be inserted is out of range.");
else if (length == maxSize) //the list is full
System.err.println("Cannot insert in a full list.");
else {
System.out.println("Cannot do it, this is a sorted list. Doing insert in place (call to
insert).");
insert(item);
}
}
public void insertEnd(int item) {
if (length == maxSize) //the list is full
System.err.println("Cannot insert in a full list.");
else {
System.out.println("Cannot do it, this is a sorted list. Doing insert in place (call to
insert).");
insert(item);
}
}
public void replaceAt(int location, int item) {
//the list is sorted!
//is actually removing the element at location and inserting item in place
if (location < 0 || location >= length)
System.err.println("The position of the item to be replaced is out of range.");
else {
removeAt(location);//method in ArrayListClass
insert(item);
}
}
public void remove(int item) {
int loc;
if (length == 0)
System.err.println("Cannot delete from an empty list.");
else {
loc = search(item);
if (loc != -1)
removeAt(loc);//method in ArrayListClass
else
System.out.println("The item to be deleted is not in the list.");
}
}
/*Another version for remove:
public void remove(T item) {
int loc;
if (length == 0)
System.err.println("Cannot delete from an empty list.");
else {
loc = search(item);
if (loc != -1) {
for(int i = loc; i < length - 1; i++)
list[i] = list[i + 1]; //shift left
length--;
}
else
System.out.println("The item to be deleted is not in the list.");
}
} */
}
2.1. Add to class OrderedArrayList a new method called removeDuplicates that should eliminate
any duplicates from a sorted list. For example, if the list is: [2, 2, 2, 2, 5, 5, 8, 9, 9, 9 ] before the
method is invoked, it should be [2, 5, 8, 9 ] after the method executes. Test the method using this
client:
//Testing the method removeDuplicates added to the user created OrderedArrayList class
public class Lab8_3 {
public static void main(String[] args) {
OrderedArrayList list = new OrderedArrayList();
list.insert(8);
list.insert(2);
list.insert(2);
list.insert(9);
list.insert(5);
list.insert(9);
list.insert(2);
list.insert(9);
list.insert(2);
list.insert(5);
System.out.println("The original list is: ");
list.print();
System.out.println("The list after method call is: ");
list.removeDuplicates();
list.print();
}
}
2.2. Same problem. This time use the ArrayList class in Java. Write removeDuplicates as a client
method and use the print method provided.
//Testing the method removeDuplicates using the Java ArrayList class
import java.util.ArrayList;
public class Lab8_4 {
public static void main(String[] args) {
ArrayList list = new ArrayList< Integer>();
list.add(2);
list.add(2);
list.add(2);
list.add(5);
list.add(5);
list.add(8);
list.add(9);
list.add(9);
System.out.println("The original list is: ");
print(list);
System.out.println("The list after method call is: ");
removeDuplicates(list);
print(list);
}
public static void removeDuplicates(ArrayList list) {
...
}
public static void print(ArrayList< Integer> someList) {
for(Integer i:someList)
System.out.print(i + " ");
System.out.println();
}
· (modified) UnorderedArrayList class including the method scaleByK
· (modified) OrderedArrayList class including the method removeDuplicates
· Lab8_2 and Lab8_4 programs using the Java ArrayList class
Solution
//here is the java file which contains required method and all other file is same as in question
/***************************** Unordered List
************************************/
import java.util.ArrayList;
import java.util.List;
public class UnorderedArrayList extends ArrayListClass {
public UnorderedArrayList() {
super();
}
public UnorderedArrayList(int size) {
super(size);
}
//Bubble Sort
public void bubbleSort() {
for (int pass = 0; pass < length - 1; pass++) {
for (int i = 0; i < length - 1; i++) {
if (list[i] > list[i + 1]) {
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
}
}
//implementation for abstract methods defined in ArrayListClass
//unordered list --> linear search
public int search(int searchItem) {
for(int i = 0; i < length; i++)
if(list[i] == searchItem)
return i;
return -1;
}
public void insertAt(int location, int insertItem) {
if (location < 0 || location >= maxSize)
System.err.println("The position of the item to be inserted is out of range.");
else if (length >= maxSize)
System.err.println("Cannot insert in a full list.");
else {
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //shift right
list[location] = insertItem;
length++;
}
}
public void insertEnd(int insertItem) {
if (length >= maxSize)
System.err.println("Cannot insert in a full list.");
else {
list[length] = insertItem;
length++;
}
}
public void replaceAt(int location, int repItem) {
if (location < 0 || location >= length)
System.err.println("The location of the item to be replaced is out of range.");
else
list[location] = repItem;
}
public void remove(int removeItem) {
int i;
if (length == 0)
System.err.println("Cannot delete from an empty list.");
else {
i = search(removeItem);
if (i != -1)
removeAt(i);
else
System.out.println("Cannot delete! The item to be deleted is not in the list.");
}
}
public void scaleByK(){
int j=0;
int[] newList = new int[maxSize];;
for(int i = 0; i < length; i++)
{
if(list[i]>0){
for(int k = 0 ; k list = new ArrayList ();
public static void main(String[] args) {
ArrayList list = new ArrayList ();
list.add(2);
list.add(4);
list.add(-2);
list.add(5);
list.add(3);
list.add(0);
list.add(7);
System.out.println("The original list is: ");
print(list);
System.out.println("The list after method call is: ");
scaleByK(list);
print(list);
}
public static void scaleByK(ArrayList list) {
ArrayListnewList = new ArrayList();
for(int item : list){
if( item>0){
System.out.println(item);
for(int i = 0 ; i someList) {
for(Integer i:someList)
System.out.print(i + " ");
System.out.println();
}
}
/***********************************Ordered
List***************************************/
public class OrderedArrayList extends ArrayListClass{
public OrderedArrayList() {
super();
}
public OrderedArrayList(int size) {
super(size);
}
//implementation for abstract methods defined in ArrayListClass
//ordered list --> binary search
public int search(int item) {
int first = 0;
int last = length - 1;
int middle = -1;
while (first <= last) {
middle = (first + last) / 2;
if (list[middle] == item)
return middle;
else
if (list[middle] > item)
last = middle - 1;
else
first = middle + 1;
}
return -1;
}
public void insert(int item) {
int loc;
boolean found = false;
if (length == 0) //list is empty
list[length++] = item; //insert item and increment length
else if (length == maxSize) //list is full
System.err.println("Cannot insert in a full list.");
else {
for (loc = 0; loc = item) {
found = true;
break;
}
}
//starting at the end, shift right
for (int i = length; i > loc; i--)
list[i] = list[i - 1];
list[loc] = item; //insert in place
length++;
}
}
/* Another version for insert:
public void insert(int item) {
int loc;
boolean found = false;
if (length == 0) //list is empty
list[length++] = item; //insert item and increment length
else if (length == maxSize) //list is full
System.err.println("Cannot insert in a full list.");
else {
int i = length - 1;
while (i >= 0 && list[i]> item) {
list[i + 1] = list[i];
i--;
}
list[i + 1] = item; // Insert item
length++;
}
} */
public void insertAt(int location, int item) {
if (location < 0 || location >= maxSize)
System.err.println("The position of the item to be inserted is out of range.");
else if (length == maxSize) //the list is full
System.err.println("Cannot insert in a full list.");
else {
System.out.println("Cannot do it, this is a sorted list. Doing insert in place (call to insert).");
insert(item);
}
}
public void insertEnd(int item) {
if (length == maxSize) //the list is full
System.err.println("Cannot insert in a full list.");
else {
System.out.println("Cannot do it, this is a sorted list. Doing insert in place (call to insert).");
insert(item);
}
}
public void replaceAt(int location, int item) {
//the list is sorted!
//is actually removing the element at location and inserting item in place
if (location < 0 || location >= length)
System.err.println("The position of the item to be replaced is out of range.");
else {
removeAt(location);//method in ArrayListClass
insert(item);
}
}
public void remove(int item) {
int loc;
if (length == 0)
System.err.println("Cannot delete from an empty list.");
else {
loc = search(item);
if (loc != -1)
removeAt(loc);//method in ArrayListClass
else
System.out.println("The item to be deleted is not in the list.");
}
}
/*Another version for remove:
public void remove(T item) {
int loc;
if (length == 0)
System.err.println("Cannot delete from an empty list.");
else {
loc = search(item);
if (loc != -1) {
for(int i = loc; i < length - 1; i++)
list[i] = list[i + 1]; //shift left
length--;
}
else
System.out.println("The item to be deleted is not in the list.");
}
} */
public void removeDuplicates(){
int j,i;
for (i = 0; i < length; i++) {
for (j = i + 1; j < length;) {
if (list[j] == list[i]) {
for (int k = j; k < length; k++) {
list[k] = list[k + 1];
}
length--;
} else
j++;
}
}
}
}
/***************************LAB8_4***********************************/
import java.util.ArrayList;
public class Lab8_4 {
public static void main(String[] args) {
ArrayList list = new ArrayList< Integer>();
list.add(2);
list.add(2);
list.add(2);
list.add(5);
list.add(5);
list.add(8);
list.add(9);
list.add(9);
System.out.println("The original list is: ");
print(list);
System.out.println("The list after method call is: ");
removeDuplicates(list);
print(list);
}
public static void removeDuplicates(ArrayList list) {
int count = list.size();
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (list.get(i).equals(list.get(j)))
{
list.remove(j--);
count--;
}
}
}
}
public static void print(ArrayList< Integer> someList) {
for(Integer i:someList)
System.out.print(i + " ");
System.out.println();
}
}

More Related Content

Similar to JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf

Illegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdfIllegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdfgopalk44
 
Please add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxPlease add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxStewartt0kJohnstonh
 
Pleae help me with this C++ question, ill upvote thanks.Write the .pdf
Pleae help me with this C++ question, ill upvote thanks.Write the .pdfPleae help me with this C++ question, ill upvote thanks.Write the .pdf
Pleae help me with this C++ question, ill upvote thanks.Write the .pdfvinodagrawal6699
 
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdfI am stuck on parts E and FExercise 1      NumberListTester.java.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdfRAJATCHUGH12
 
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdf(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdfssuserc77a341
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfravikapoorindia
 
I need help creating a parametized JUnit test case for the following.pdf
I need help creating a parametized JUnit test case for the following.pdfI need help creating a parametized JUnit test case for the following.pdf
I need help creating a parametized JUnit test case for the following.pdffonecomp
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdffashioncollection2
 
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfStep 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfaloeplusint
 
Engineering lecture ppt by venay magen
Engineering lecture ppt by venay magenEngineering lecture ppt by venay magen
Engineering lecture ppt by venay magenvenaymagen19
 
lec6.ppt
lec6.pptlec6.ppt
lec6.pptcawarir
 
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdfpublic static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdfarihanthtoysandgifts
 
For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdffashiongallery1
 
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docxJAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docxGavinUJtMathist
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdffirstchoiceajmer
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdffootstatus
 
In C++Add the function min as an abstract function to the classar.pdf
In C++Add the function min as an abstract function to the classar.pdfIn C++Add the function min as an abstract function to the classar.pdf
In C++Add the function min as an abstract function to the classar.pdffantoosh1
 

Similar to JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf (20)

Illegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdfIllegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdf
 
Please add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxPlease add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docx
 
Pleae help me with this C++ question, ill upvote thanks.Write the .pdf
Pleae help me with this C++ question, ill upvote thanks.Write the .pdfPleae help me with this C++ question, ill upvote thanks.Write the .pdf
Pleae help me with this C++ question, ill upvote thanks.Write the .pdf
 
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdfI am stuck on parts E and FExercise 1      NumberListTester.java.pdf
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
 
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdf(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
I need help creating a parametized JUnit test case for the following.pdf
I need help creating a parametized JUnit test case for the following.pdfI need help creating a parametized JUnit test case for the following.pdf
I need help creating a parametized JUnit test case for the following.pdf
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdf
 
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfStep 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
 
Engineering lecture ppt by venay magen
Engineering lecture ppt by venay magenEngineering lecture ppt by venay magen
Engineering lecture ppt by venay magen
 
lec6.ppt
lec6.pptlec6.ppt
lec6.ppt
 
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdfpublic static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
public static ArrayListInteger doArrayListInsertAtMedian(int nu.pdf
 
For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docxJAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
 
In C++Add the function min as an abstract function to the classar.pdf
In C++Add the function min as an abstract function to the classar.pdfIn C++Add the function min as an abstract function to the classar.pdf
In C++Add the function min as an abstract function to the classar.pdf
 
Array list
Array listArray list
Array list
 

More from arpaqindia

In the year 2000, a halophile was isolated from a 250 million year o.pdf
In the year 2000, a halophile was isolated from a 250 million year o.pdfIn the year 2000, a halophile was isolated from a 250 million year o.pdf
In the year 2000, a halophile was isolated from a 250 million year o.pdfarpaqindia
 
In in the image seen here, in which direction would you expect water .pdf
In in the image seen here, in which direction would you expect water .pdfIn in the image seen here, in which direction would you expect water .pdf
In in the image seen here, in which direction would you expect water .pdfarpaqindia
 
How does the movement of DNA fragments on the gel and the movemen.pdf
How does the movement of DNA fragments on the gel and the movemen.pdfHow does the movement of DNA fragments on the gel and the movemen.pdf
How does the movement of DNA fragments on the gel and the movemen.pdfarpaqindia
 
Genetics Homework Problem 1 You are mapping genes in a species of f.pdf
Genetics Homework Problem 1 You are mapping genes in a species of f.pdfGenetics Homework Problem 1 You are mapping genes in a species of f.pdf
Genetics Homework Problem 1 You are mapping genes in a species of f.pdfarpaqindia
 
For f(x) =2x, find a formula for the Riemann sum obtained by dividin.pdf
For f(x) =2x, find a formula for the Riemann sum obtained by dividin.pdfFor f(x) =2x, find a formula for the Riemann sum obtained by dividin.pdf
For f(x) =2x, find a formula for the Riemann sum obtained by dividin.pdfarpaqindia
 
Fibroblasts are the typical cells of simple squamous epithelium. Dens.pdf
Fibroblasts are the typical cells of simple squamous epithelium. Dens.pdfFibroblasts are the typical cells of simple squamous epithelium. Dens.pdf
Fibroblasts are the typical cells of simple squamous epithelium. Dens.pdfarpaqindia
 
Explain the difference between an observational study and a designed.pdf
Explain the difference between an observational study and a designed.pdfExplain the difference between an observational study and a designed.pdf
Explain the difference between an observational study and a designed.pdfarpaqindia
 
Discuss the recruitment and selection procedure done by human resour.pdf
Discuss the recruitment and selection procedure done by human resour.pdfDiscuss the recruitment and selection procedure done by human resour.pdf
Discuss the recruitment and selection procedure done by human resour.pdfarpaqindia
 
Do The statement of stockholders equity explains changes in equity.pdf
Do The statement of stockholders equity explains changes in equity.pdfDo The statement of stockholders equity explains changes in equity.pdf
Do The statement of stockholders equity explains changes in equity.pdfarpaqindia
 
Describe what is meant by “radical cure” and identify the drug that .pdf
Describe what is meant by “radical cure” and identify the drug that .pdfDescribe what is meant by “radical cure” and identify the drug that .pdf
Describe what is meant by “radical cure” and identify the drug that .pdfarpaqindia
 
Describe the nutritional modes of organisms based upon where they obt.pdf
Describe the nutritional modes of organisms based upon where they obt.pdfDescribe the nutritional modes of organisms based upon where they obt.pdf
Describe the nutritional modes of organisms based upon where they obt.pdfarpaqindia
 
Decasper and Spence (1986) measured babies sucking rates as they lis.pdf
Decasper and Spence (1986) measured babies sucking rates as they lis.pdfDecasper and Spence (1986) measured babies sucking rates as they lis.pdf
Decasper and Spence (1986) measured babies sucking rates as they lis.pdfarpaqindia
 
Corhin Eckles and Connor Hayes have recieved walkie talkies for Chri.pdf
Corhin Eckles and Connor Hayes have recieved walkie talkies for Chri.pdfCorhin Eckles and Connor Hayes have recieved walkie talkies for Chri.pdf
Corhin Eckles and Connor Hayes have recieved walkie talkies for Chri.pdfarpaqindia
 
Consider three genes in a hypothetical bird. One controls the color o.pdf
Consider three genes in a hypothetical bird. One controls the color o.pdfConsider three genes in a hypothetical bird. One controls the color o.pdf
Consider three genes in a hypothetical bird. One controls the color o.pdfarpaqindia
 
Answer the following questions What is the domain of logarithmic fun.pdf
Answer the following questions What is the domain of logarithmic fun.pdfAnswer the following questions What is the domain of logarithmic fun.pdf
Answer the following questions What is the domain of logarithmic fun.pdfarpaqindia
 
are organic compounds that bind very tightly to iron ions for transp.pdf
are organic compounds that bind very tightly to iron ions for transp.pdfare organic compounds that bind very tightly to iron ions for transp.pdf
are organic compounds that bind very tightly to iron ions for transp.pdfarpaqindia
 
Book Business Its Legal, Ethical, Global Environment 10th Edition.pdf
Book Business Its Legal, Ethical, Global Environment 10th Edition.pdfBook Business Its Legal, Ethical, Global Environment 10th Edition.pdf
Book Business Its Legal, Ethical, Global Environment 10th Edition.pdfarpaqindia
 
According the United States (U.S.) Centers for Disease Control and P.pdf
According the United States (U.S.) Centers for Disease Control and P.pdfAccording the United States (U.S.) Centers for Disease Control and P.pdf
According the United States (U.S.) Centers for Disease Control and P.pdfarpaqindia
 
9. For some non-Mendelian inheritance patterns, the genotype of an i.pdf
9. For some non-Mendelian inheritance patterns, the genotype of an i.pdf9. For some non-Mendelian inheritance patterns, the genotype of an i.pdf
9. For some non-Mendelian inheritance patterns, the genotype of an i.pdfarpaqindia
 
You have mated a phenotypically wild-type female of the genotype Xy+.pdf
You have mated a phenotypically wild-type female of the genotype Xy+.pdfYou have mated a phenotypically wild-type female of the genotype Xy+.pdf
You have mated a phenotypically wild-type female of the genotype Xy+.pdfarpaqindia
 

More from arpaqindia (20)

In the year 2000, a halophile was isolated from a 250 million year o.pdf
In the year 2000, a halophile was isolated from a 250 million year o.pdfIn the year 2000, a halophile was isolated from a 250 million year o.pdf
In the year 2000, a halophile was isolated from a 250 million year o.pdf
 
In in the image seen here, in which direction would you expect water .pdf
In in the image seen here, in which direction would you expect water .pdfIn in the image seen here, in which direction would you expect water .pdf
In in the image seen here, in which direction would you expect water .pdf
 
How does the movement of DNA fragments on the gel and the movemen.pdf
How does the movement of DNA fragments on the gel and the movemen.pdfHow does the movement of DNA fragments on the gel and the movemen.pdf
How does the movement of DNA fragments on the gel and the movemen.pdf
 
Genetics Homework Problem 1 You are mapping genes in a species of f.pdf
Genetics Homework Problem 1 You are mapping genes in a species of f.pdfGenetics Homework Problem 1 You are mapping genes in a species of f.pdf
Genetics Homework Problem 1 You are mapping genes in a species of f.pdf
 
For f(x) =2x, find a formula for the Riemann sum obtained by dividin.pdf
For f(x) =2x, find a formula for the Riemann sum obtained by dividin.pdfFor f(x) =2x, find a formula for the Riemann sum obtained by dividin.pdf
For f(x) =2x, find a formula for the Riemann sum obtained by dividin.pdf
 
Fibroblasts are the typical cells of simple squamous epithelium. Dens.pdf
Fibroblasts are the typical cells of simple squamous epithelium. Dens.pdfFibroblasts are the typical cells of simple squamous epithelium. Dens.pdf
Fibroblasts are the typical cells of simple squamous epithelium. Dens.pdf
 
Explain the difference between an observational study and a designed.pdf
Explain the difference between an observational study and a designed.pdfExplain the difference between an observational study and a designed.pdf
Explain the difference between an observational study and a designed.pdf
 
Discuss the recruitment and selection procedure done by human resour.pdf
Discuss the recruitment and selection procedure done by human resour.pdfDiscuss the recruitment and selection procedure done by human resour.pdf
Discuss the recruitment and selection procedure done by human resour.pdf
 
Do The statement of stockholders equity explains changes in equity.pdf
Do The statement of stockholders equity explains changes in equity.pdfDo The statement of stockholders equity explains changes in equity.pdf
Do The statement of stockholders equity explains changes in equity.pdf
 
Describe what is meant by “radical cure” and identify the drug that .pdf
Describe what is meant by “radical cure” and identify the drug that .pdfDescribe what is meant by “radical cure” and identify the drug that .pdf
Describe what is meant by “radical cure” and identify the drug that .pdf
 
Describe the nutritional modes of organisms based upon where they obt.pdf
Describe the nutritional modes of organisms based upon where they obt.pdfDescribe the nutritional modes of organisms based upon where they obt.pdf
Describe the nutritional modes of organisms based upon where they obt.pdf
 
Decasper and Spence (1986) measured babies sucking rates as they lis.pdf
Decasper and Spence (1986) measured babies sucking rates as they lis.pdfDecasper and Spence (1986) measured babies sucking rates as they lis.pdf
Decasper and Spence (1986) measured babies sucking rates as they lis.pdf
 
Corhin Eckles and Connor Hayes have recieved walkie talkies for Chri.pdf
Corhin Eckles and Connor Hayes have recieved walkie talkies for Chri.pdfCorhin Eckles and Connor Hayes have recieved walkie talkies for Chri.pdf
Corhin Eckles and Connor Hayes have recieved walkie talkies for Chri.pdf
 
Consider three genes in a hypothetical bird. One controls the color o.pdf
Consider three genes in a hypothetical bird. One controls the color o.pdfConsider three genes in a hypothetical bird. One controls the color o.pdf
Consider three genes in a hypothetical bird. One controls the color o.pdf
 
Answer the following questions What is the domain of logarithmic fun.pdf
Answer the following questions What is the domain of logarithmic fun.pdfAnswer the following questions What is the domain of logarithmic fun.pdf
Answer the following questions What is the domain of logarithmic fun.pdf
 
are organic compounds that bind very tightly to iron ions for transp.pdf
are organic compounds that bind very tightly to iron ions for transp.pdfare organic compounds that bind very tightly to iron ions for transp.pdf
are organic compounds that bind very tightly to iron ions for transp.pdf
 
Book Business Its Legal, Ethical, Global Environment 10th Edition.pdf
Book Business Its Legal, Ethical, Global Environment 10th Edition.pdfBook Business Its Legal, Ethical, Global Environment 10th Edition.pdf
Book Business Its Legal, Ethical, Global Environment 10th Edition.pdf
 
According the United States (U.S.) Centers for Disease Control and P.pdf
According the United States (U.S.) Centers for Disease Control and P.pdfAccording the United States (U.S.) Centers for Disease Control and P.pdf
According the United States (U.S.) Centers for Disease Control and P.pdf
 
9. For some non-Mendelian inheritance patterns, the genotype of an i.pdf
9. For some non-Mendelian inheritance patterns, the genotype of an i.pdf9. For some non-Mendelian inheritance patterns, the genotype of an i.pdf
9. For some non-Mendelian inheritance patterns, the genotype of an i.pdf
 
You have mated a phenotypically wild-type female of the genotype Xy+.pdf
You have mated a phenotypically wild-type female of the genotype Xy+.pdfYou have mated a phenotypically wild-type female of the genotype Xy+.pdf
You have mated a phenotypically wild-type female of the genotype Xy+.pdf
 

Recently uploaded

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 

JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf

  • 1. JAVA LAB #8 - ARRAY BASED LISTS The next exercise is based on this implemetation for an UnorderedArrayList of integers: //Interface: ArrayListADT //works for int public interface ArrayListADT { public boolean isEmpty(); //Method to determine whether the list is empty. public boolean isFull(); //Method to determine whether the list is full. public int listSize(); //Method to return the number of elements in the list. public int maxListSize(); //Method to return the maximum size of the list. public void print(); //Method to output the elements of the list. public boolean isItemAtEqual(int location, int item); //Method to determine whether item is the same as the item in the list at location. public void insertAt(int location, int insertItem); //Method to insert insertItem in the list at the position public void insertEnd(int insertItem); //Method to insert insertItem at the end of the list. public void removeAt(int location); //Method to remove the item from the list at location. public int retrieveAt(int location); //Method to retrieve the element from the list at location. public void replaceAt(int location, int repItem); //Method to replace the element in the list at location with repItem. public void clearList(); //Method to remove all the elements from the list. public int search(int searchItem); //Method to determine whether searchItem is in the list. public void remove(int removeItem); //Method to remove an item from the list. } //Class: ArrayListClass implements //Interface: ArrayListADT public abstract class ArrayListClass implements ArrayListADT { protected int length; //to store the length of the list protected int maxSize; //to store the maximum size of the list protected int[] list; //array to hold the list elements //Default constructor public ArrayListClass() { maxSize = 100; length = 0; list = new int[maxSize];
  • 2. } //Alternate Constructor public ArrayListClass(int size) { if(size <= 0) { System.err.println("The array size must be positive. Creating an array of size 100."); maxSize = 100; } else maxSize = size; length = 0; list = new int[maxSize]; } public boolean isEmpty() { return (length == 0); } public boolean isFull() { return (length == maxSize); } public int listSize() { return length; } public int maxListSize() { return maxSize; } public void print() { for (int i = 0; i < length; i++) System.out.print(list[i] + " "); System.out.println(); } public boolean isItemAtEqual(int location, int item) { if (location < 0 || location >= length) { System.err.println("The location of the item to be compared is out of range."); return false; } return list[location]== item; }
  • 3. public void clearList() { for (int i = 0; i < length; i++) list[i] = 0; length = 0; System.gc(); //invoke the Java garbage collector } public void removeAt(int location) { if (location < 0 || location >= length) System.err.println("The location of the item to be removed is out of range."); else { for(int i = location; i < length - 1; i++) list[i] = list[i + 1]; length--; } } public int retrieveAt(int location) { if (location < 0 || location >= length) { System.err.println("The location of the item to be retrieved is out of range."); return 0; } else return list[location]; } public abstract void insertAt(int location, int insertItem); public abstract void insertEnd(int insertItem); public abstract void replaceAt(int location, int repItem); public abstract int search(int searchItem); public abstract void remove(int removeItem); } //Class: UnorderedArrayList extends //Super class: ArrayListClass public class UnorderedArrayList extends ArrayListClass { public UnorderedArrayList() { super(); }
  • 4. public UnorderedArrayList(int size) { super(size); } //Bubble Sort public void bubbleSort() { for (int pass = 0; pass < length - 1; pass++) { for (int i = 0; i < length - 1; i++) { if (list[i] > list[i + 1]) { int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; } } } } //implementation for abstract methods defined in ArrayListClass //unordered list --> linear search public int search(int searchItem) { for(int i = 0; i < length; i++) if(list[i] == searchItem) return i; return -1; } public void insertAt(int location, int insertItem) { if (location < 0 || location >= maxSize) System.err.println("The position of the item to be inserted is out of range."); else if (length >= maxSize) System.err.println("Cannot insert in a full list."); else { for (int i = length; i > location; i--) list[i] = list[i - 1]; //shift right list[location] = insertItem; length++; } } public void insertEnd(int insertItem) {
  • 5. if (length >= maxSize) System.err.println("Cannot insert in a full list."); else { list[length] = insertItem; length++; } } public void replaceAt(int location, int repItem) { if (location < 0 || location >= length) System.err.println("The location of the item to be replaced is out of range."); else list[location] = repItem; } public void remove(int removeItem) { int i; if (length == 0) System.err.println("Cannot delete from an empty list."); else { i = search(removeItem); if (i != -1) removeAt(i); else System.out.println("Cannot delete! The item to be deleted is not in the list."); } } } Add to class UnorderedArrayList a new method called scaleByK that should replace every integer of value k with k copies of itself. For example, if the list is: [2, 4, -2, 5, 3, 0, 7] before the method is invoked, it should be [2, 2, 4, 4, 4, 4, 5, 5, 5, 5, 5, 3, 3, 3, 7, 7, 7, 7, 7, 7, 7 ] after the method executes. Note that the method should remove from the list all 0s and negative values. Test the method using this client: //Testing the method scaleByK added to the user created UnorderedArrayList class public class Lab8_1 { public static final int SIZE = 100; public static void main(String[] args) {
  • 6. UnorderedArrayList list = new UnorderedArrayList(SIZE); list.insertEnd(2); list.insertEnd(4); list.insertEnd(-2); list.insertEnd(5); list.insertEnd(3); list.insertEnd(0); list.insertEnd(7); System.out.println("The original list is: "); list.print(); System.out.println("The list after method call is: "); list.scaleByK(); list.print(); } } 1.2. Same problem. This time use the ArrayList class in Java. Write scaleByK as a client method and use the print method provided. //Testing the method scaleByK using the Java ArrayList class import java.util.ArrayList; public class Lab8_2 { public static void main(String[] args) { ArrayList list = new ArrayList (); list.add(2); list.add(4); list.add(-2); list.add(5); list.add(3); list.add(0); list.add(7); System.out.println("The original list is: "); print(list); System.out.println("The list after method call is: "); scaleByK(list); print(list); }
  • 7. public static void scaleByK(ArrayList list) { ... } public static void print(ArrayList< Integer> someList) { for(Integer i:someList) System.out.print(i + " "); System.out.println(); } } The next exercise is based on this implemetation for an OrderedArrayList of integers: //Interface: ArrayListADT public interface ArrayListADT { //same as above, you already have it! } //Class: ArrayListClass implements //Interface: ArrayListADT public abstract class ArrayListClass implements ArrayListADT { //same as above, you already have it! } //Class: OrderedArrayList extends //Super class: ArrayListClass public class OrderedArrayList extends ArrayListClass{ public OrderedArrayList() { super(); } public OrderedArrayList(int size) { super(size); } //implementation for abstract methods defined in ArrayListClass //ordered list --> binary search public int search(int item) { int first = 0; int last = length - 1; int middle = -1;
  • 8. while (first <= last) { middle = (first + last) / 2; if (list[middle] == item) return middle; else if (list[middle] > item) last = middle - 1; else first = middle + 1; } return -1; } public void insert(int item) { int loc; boolean found = false; if (length == 0) //list is empty list[length++] = item; //insert item and increment length else if (length == maxSize) //list is full System.err.println("Cannot insert in a full list."); else { for (loc = 0; loc = item) { found = true; break; } } //starting at the end, shift right for (int i = length; i > loc; i--) list[i] = list[i - 1]; list[loc] = item; //insert in place length++; } } /* Another version for insert: public void insert(int item) { int loc; boolean found = false;
  • 9. if (length == 0) //list is empty list[length++] = item; //insert item and increment length else if (length == maxSize) //list is full System.err.println("Cannot insert in a full list."); else { int i = length - 1; while (i >= 0 && list[i]> item) { list[i + 1] = list[i]; i--; } list[i + 1] = item; // Insert item length++; } } */ public void insertAt(int location, int item) { if (location < 0 || location >= maxSize) System.err.println("The position of the item to be inserted is out of range."); else if (length == maxSize) //the list is full System.err.println("Cannot insert in a full list."); else { System.out.println("Cannot do it, this is a sorted list. Doing insert in place (call to insert)."); insert(item); } } public void insertEnd(int item) { if (length == maxSize) //the list is full System.err.println("Cannot insert in a full list."); else { System.out.println("Cannot do it, this is a sorted list. Doing insert in place (call to insert)."); insert(item); } } public void replaceAt(int location, int item) { //the list is sorted!
  • 10. //is actually removing the element at location and inserting item in place if (location < 0 || location >= length) System.err.println("The position of the item to be replaced is out of range."); else { removeAt(location);//method in ArrayListClass insert(item); } } public void remove(int item) { int loc; if (length == 0) System.err.println("Cannot delete from an empty list."); else { loc = search(item); if (loc != -1) removeAt(loc);//method in ArrayListClass else System.out.println("The item to be deleted is not in the list."); } } /*Another version for remove: public void remove(T item) { int loc; if (length == 0) System.err.println("Cannot delete from an empty list."); else { loc = search(item); if (loc != -1) { for(int i = loc; i < length - 1; i++) list[i] = list[i + 1]; //shift left length--; } else System.out.println("The item to be deleted is not in the list."); } } */
  • 11. } 2.1. Add to class OrderedArrayList a new method called removeDuplicates that should eliminate any duplicates from a sorted list. For example, if the list is: [2, 2, 2, 2, 5, 5, 8, 9, 9, 9 ] before the method is invoked, it should be [2, 5, 8, 9 ] after the method executes. Test the method using this client: //Testing the method removeDuplicates added to the user created OrderedArrayList class public class Lab8_3 { public static void main(String[] args) { OrderedArrayList list = new OrderedArrayList(); list.insert(8); list.insert(2); list.insert(2); list.insert(9); list.insert(5); list.insert(9); list.insert(2); list.insert(9); list.insert(2); list.insert(5); System.out.println("The original list is: "); list.print(); System.out.println("The list after method call is: "); list.removeDuplicates(); list.print(); } } 2.2. Same problem. This time use the ArrayList class in Java. Write removeDuplicates as a client method and use the print method provided. //Testing the method removeDuplicates using the Java ArrayList class import java.util.ArrayList; public class Lab8_4 { public static void main(String[] args) { ArrayList list = new ArrayList< Integer>(); list.add(2); list.add(2);
  • 12. list.add(2); list.add(5); list.add(5); list.add(8); list.add(9); list.add(9); System.out.println("The original list is: "); print(list); System.out.println("The list after method call is: "); removeDuplicates(list); print(list); } public static void removeDuplicates(ArrayList list) { ... } public static void print(ArrayList< Integer> someList) { for(Integer i:someList) System.out.print(i + " "); System.out.println(); } · (modified) UnorderedArrayList class including the method scaleByK · (modified) OrderedArrayList class including the method removeDuplicates · Lab8_2 and Lab8_4 programs using the Java ArrayList class Solution //here is the java file which contains required method and all other file is same as in question /***************************** Unordered List ************************************/ import java.util.ArrayList; import java.util.List; public class UnorderedArrayList extends ArrayListClass { public UnorderedArrayList() { super(); }
  • 13. public UnorderedArrayList(int size) { super(size); } //Bubble Sort public void bubbleSort() { for (int pass = 0; pass < length - 1; pass++) { for (int i = 0; i < length - 1; i++) { if (list[i] > list[i + 1]) { int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; } } } } //implementation for abstract methods defined in ArrayListClass //unordered list --> linear search public int search(int searchItem) { for(int i = 0; i < length; i++) if(list[i] == searchItem) return i; return -1; } public void insertAt(int location, int insertItem) { if (location < 0 || location >= maxSize) System.err.println("The position of the item to be inserted is out of range."); else if (length >= maxSize) System.err.println("Cannot insert in a full list."); else { for (int i = length; i > location; i--) list[i] = list[i - 1]; //shift right list[location] = insertItem; length++; } } public void insertEnd(int insertItem) {
  • 14. if (length >= maxSize) System.err.println("Cannot insert in a full list."); else { list[length] = insertItem; length++; } } public void replaceAt(int location, int repItem) { if (location < 0 || location >= length) System.err.println("The location of the item to be replaced is out of range."); else list[location] = repItem; } public void remove(int removeItem) { int i; if (length == 0) System.err.println("Cannot delete from an empty list."); else { i = search(removeItem); if (i != -1) removeAt(i); else System.out.println("Cannot delete! The item to be deleted is not in the list."); } } public void scaleByK(){ int j=0; int[] newList = new int[maxSize];; for(int i = 0; i < length; i++) { if(list[i]>0){ for(int k = 0 ; k list = new ArrayList (); public static void main(String[] args) { ArrayList list = new ArrayList (); list.add(2);
  • 15. list.add(4); list.add(-2); list.add(5); list.add(3); list.add(0); list.add(7); System.out.println("The original list is: "); print(list); System.out.println("The list after method call is: "); scaleByK(list); print(list); } public static void scaleByK(ArrayList list) { ArrayListnewList = new ArrayList(); for(int item : list){ if( item>0){ System.out.println(item); for(int i = 0 ; i someList) { for(Integer i:someList) System.out.print(i + " "); System.out.println(); } } /***********************************Ordered List***************************************/ public class OrderedArrayList extends ArrayListClass{ public OrderedArrayList() { super(); } public OrderedArrayList(int size) { super(size); }
  • 16. //implementation for abstract methods defined in ArrayListClass //ordered list --> binary search public int search(int item) { int first = 0; int last = length - 1; int middle = -1; while (first <= last) { middle = (first + last) / 2; if (list[middle] == item) return middle; else if (list[middle] > item) last = middle - 1; else first = middle + 1; } return -1; } public void insert(int item) { int loc; boolean found = false; if (length == 0) //list is empty list[length++] = item; //insert item and increment length else if (length == maxSize) //list is full System.err.println("Cannot insert in a full list."); else { for (loc = 0; loc = item) { found = true; break; } } //starting at the end, shift right for (int i = length; i > loc; i--) list[i] = list[i - 1]; list[loc] = item; //insert in place length++;
  • 17. } } /* Another version for insert: public void insert(int item) { int loc; boolean found = false; if (length == 0) //list is empty list[length++] = item; //insert item and increment length else if (length == maxSize) //list is full System.err.println("Cannot insert in a full list."); else { int i = length - 1; while (i >= 0 && list[i]> item) { list[i + 1] = list[i]; i--; } list[i + 1] = item; // Insert item length++; } } */ public void insertAt(int location, int item) { if (location < 0 || location >= maxSize) System.err.println("The position of the item to be inserted is out of range."); else if (length == maxSize) //the list is full System.err.println("Cannot insert in a full list."); else { System.out.println("Cannot do it, this is a sorted list. Doing insert in place (call to insert)."); insert(item); } } public void insertEnd(int item) { if (length == maxSize) //the list is full System.err.println("Cannot insert in a full list."); else { System.out.println("Cannot do it, this is a sorted list. Doing insert in place (call to insert)."); insert(item);
  • 18. } } public void replaceAt(int location, int item) { //the list is sorted! //is actually removing the element at location and inserting item in place if (location < 0 || location >= length) System.err.println("The position of the item to be replaced is out of range."); else { removeAt(location);//method in ArrayListClass insert(item); } } public void remove(int item) { int loc; if (length == 0) System.err.println("Cannot delete from an empty list."); else { loc = search(item); if (loc != -1) removeAt(loc);//method in ArrayListClass else System.out.println("The item to be deleted is not in the list."); } } /*Another version for remove: public void remove(T item) { int loc; if (length == 0) System.err.println("Cannot delete from an empty list."); else { loc = search(item); if (loc != -1) { for(int i = loc; i < length - 1; i++) list[i] = list[i + 1]; //shift left length--; }
  • 19. else System.out.println("The item to be deleted is not in the list."); } } */ public void removeDuplicates(){ int j,i; for (i = 0; i < length; i++) { for (j = i + 1; j < length;) { if (list[j] == list[i]) { for (int k = j; k < length; k++) { list[k] = list[k + 1]; } length--; } else j++; } } } } /***************************LAB8_4***********************************/ import java.util.ArrayList; public class Lab8_4 { public static void main(String[] args) { ArrayList list = new ArrayList< Integer>(); list.add(2); list.add(2); list.add(2); list.add(5); list.add(5); list.add(8); list.add(9);
  • 20. list.add(9); System.out.println("The original list is: "); print(list); System.out.println("The list after method call is: "); removeDuplicates(list); print(list); } public static void removeDuplicates(ArrayList list) { int count = list.size(); for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { if (list.get(i).equals(list.get(j))) { list.remove(j--); count--; } } } } public static void print(ArrayList< Integer> someList) { for(Integer i:someList) System.out.print(i + " "); System.out.println(); } }