Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class ArrayList > implements List { private boolean isSorted; T[] a; private int noOfElements; @SuppressWarnings(unchecked) public ArrayList() { noOfElements = 0; a = (T[])

image text in transcribed

image text in transcribed

image text in transcribed

public class ArrayList> implements List

{

private boolean isSorted;

T[] a;

private int noOfElements;

@SuppressWarnings("unchecked")

public ArrayList()

{

noOfElements = 0;

a = (T[]) new Comparable[2];

isSorted = true;

}

@Override

public boolean add(T element) {

if(element==null)

return false;

else

{

int i=0;

while(a[i] != null && i

i++;

if(a[i] == null)

a[i] = element;

else if(i == a.length)

{

a = newArray();

a[i] = element;

noOfElements++;

}

isSorted = false;

return true;

}

}

@Override

public boolean add(int index, T element) {

if(element == null)

return false;

else

{

if(index = a.length) // index out of bounds

return false;

if(a[index] == null)

a[index] = element;

else

{

int i;

if(a[size() - 1] != null) // if last element is not null, means array is full

{

a = newArray();

i = size()/2;

}

else

i = noOfElements - 1;

while(i != index)

{

a[i] = a[i-1];

i--;

}

a[i] = element;

noOfElements++;

}

isSorted = false;

return true;

}

}

@Override

public void clear() {

for(int i = 0 ; i

a[i] = null;

noOfElements = 0;

}

@Override

public boolean contains(T element) {

for(int i=0;i

if(a[i]==element)

return true;

return false;

}

@Override

public T get(int index) {

if(index = a.length)

return null;

else

return a[index];

}

@Override

public int indexOf(T element) {

for(int i=0;i

if(a[i]==element)

return i;

return -1;

}

@Override

public boolean isEmpty() {

if(noOfElements == 0)

return true;

return false;

}

@Override

public int lastIndexOf(T element) {

for(int i = noOfElements-1 ; i >= 0 ; i--)

if(a[i] == element)

return i;

return -1;

}

@Override

public T set(int index, T element) {

if(element == null || index = a.length)

return null;

T temp = a[index];

a[index] = element;

return temp;

}

@Override

public int size() {

return noOfElements;

}

@Override

public void sort(boolean order) {

if(!isSorted)

{

if(order) // ascending

{

}

else // descending

{

}

}

else

{

if(!order) // descending

{

}

}

}

@Override

public boolean remove(T element) {

int i=0;

while(i

{

if(a[i] == element)

{

int j = i;

while(j

{

a[j] = a[j+1];

j++;

}

return true;

}

i++;

}

return false;

}

@Override

public T remove(int index) {

if(index = a.length)

return null;

T temp = a[index];

int i=0;

while(i

{

if(i == index)

{

int j = i;

while(j

{

a[j] = a[j+1];

j++;

}

return temp;

}

}

return null;

}

@SuppressWarnings("unchecked")

private T[] newArray()

{

int size = 2*a.length;

T[] array = (T[])new Comparable[size];

for(int i=0;i

array[i] = a[i];

return array;

}

In addition to the methods described in the List interface, the LinkedList class should contain a private class variable isSorted. This should be initialized to true in the constructor (because it is sorted if it has no elements) and updated when the list is sorted, or more elements You may implement your linked list as a headed list, i.e., the first node in the list is a 'dummy' node and the second node is the first element of the list, or a non-headed list, i.e., the first node is the first element of the list. Depending on how you choose to implement your list, there will be some small nuances. CSCI 1933 PROJECT 3 Due: Friday, March 23th, 2018 are added or set. For the purposes of this class, isSorted is only true if the list is sorted in ascending order Initially and after a call to clear ), the size should be zero and your list should be empty . In sort (), do not use an array or ArrayList to sort the elements. You are required to sort the values using only the linked list data structure. You can move nodes or swap values but you cannot use an array to store values while sorting. Depending on your implementation, remember that after sorting, the former first node may not be the current first node. After you have implemented your LinkedList class, include junit tests that test all functionality. 2 An Elephant Herd Slightly modify your class ElephantHerd from Project 2 to use an underlying LinkedList rather than an ArrayList. Then verify that the methods still work as intended. If you did the previous project correctly, this step should only require the modification of one line of code In addition to the methods described in the List interface, the LinkedList class should contain a private class variable isSorted. This should be initialized to true in the constructor (because it is sorted if it has no elements) and updated when the list is sorted, or more elements You may implement your linked list as a headed list, i.e., the first node in the list is a 'dummy' node and the second node is the first element of the list, or a non-headed list, i.e., the first node is the first element of the list. Depending on how you choose to implement your list, there will be some small nuances. CSCI 1933 PROJECT 3 Due: Friday, March 23th, 2018 are added or set. For the purposes of this class, isSorted is only true if the list is sorted in ascending order Initially and after a call to clear ), the size should be zero and your list should be empty . In sort (), do not use an array or ArrayList to sort the elements. You are required to sort the values using only the linked list data structure. You can move nodes or swap values but you cannot use an array to store values while sorting. Depending on your implementation, remember that after sorting, the former first node may not be the current first node. After you have implemented your LinkedList class, include junit tests that test all functionality. 2 An Elephant Herd Slightly modify your class ElephantHerd from Project 2 to use an underlying LinkedList rather than an ArrayList. Then verify that the methods still work as intended. If you did the previous project correctly, this step should only require the modification of one line of code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions