Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the following Java code. Implement all the methods from the ListInterface interface for the AList class (some have already been implemented).

I need help with the following Java code.

Implement all the methods from the ListInterface interface for the AList class (some have already been implemented). You can reuse the existing methods' implementations already defined in these three classes.

Additionally, you need to override by implementing the equals and toStringmethods of Object class. Two lists are equal if they both have same number of entries and each entry is at the same location in both lists. toString method should return string representation of entries separated by commas in a square brackets, for example, [a, b, c], or just square brackets [ ] if no entry is available in a list. (Note the space after each comma, and space in between two brackets when list is empty.)

Note: You can reuse your codes whenever applicable, as well as any or all parts of AList.java. While implementing new methods if O(1) implementation is possible, then you need to implement this version of each method.

/**

* An interface for the ADT list. Entries in a list have positions that begin

* with 1.

*/

public interface ListInterface

{

/**

* Adds a new entry to the end of this list. Entries currently in the list are

* unaffected. The list's size is increased by 1.

* @param newEntry: The object to be added as a new entry.

*/

public void add(T newEntry);

/**

* Adds a new entry at a specified position within this list. Entries

* originally at and above the specified position are at the next higher

* position within the list. The list's size is increased by 1.

* @param newPosition

* An integer that specifies the desired position of the new entry.

* @param newEntry

* The object to be added as a new entry.

* @throws IndexOutOfBoundsException

* if either newPosition < 1 or newPosition > getLength() + 1.

*/

public void add(int newPosition, T newEntry);

/**

* Removes the entry at a given position from this list. Entries originally at

* positions higher than the given position are at the next lower position

* within the list, and the list's size is decreased by 1.

*

* @param givenPosition

* An integer that indicates the position of the entry to be removed.

* @return A reference to the removed entry.

* @throws IndexOutOfBoundsException

* if either givenPosition < 1 or givenPosition > getLength().

*/

public T remove(int givenPosition);

/** Removes all entries from this list. */

public void clear();

/**

* Replaces the entry at a given position in this list.

*

* @param givenPosition

* An integer that indicates the position of the entry to be replaced.

* @param newEntry

* The object that will replace the entry at the position

* givenPosition.

* @return The original entry that was replaced.

* @throws IndexOutOfBoundsException

* if either givenPosition < 1 or givenPosition > getLength().

*/

public T replace(int givenPosition, T newEntry);

/**

* Retrieves the entry at a given position in this list.

*

* @param givenPosition

* An integer that indicates the position of the desired entry.

* @return A reference to the indicated entry.

* @throws IndexOutOfBoundsException

* if either givenPosition < 1 or givenPosition > getLength().

*/

public T getEntry(int givenPosition);

/**

* Retrieves all entries that are in this list in the order in which they occur

* in the list.

*

* @return A newly allocated array of all the entries in the list. Note: If the

* list is empty, the returned array is empty.

*/

public T[] toArray();

/**

* Sees whether this list contains a given entry.

*

* @param anEntry

* The object that is the desired entry.

* @return True if the list contains anEntry, or false if not.

*/

public boolean contains(T anEntry);

/**

* Gets the length of this list.

*

* @return The integer number of entries currently in the list.

*/

public int getLength();

/**

* Sees whether this list is empty.

*

* @return True if the list is empty, or false if not.

*/

public boolean isEmpty();

/**

* Adds a new entry to the beginning of this collection.

*

* @param newEntry

* The object to be added as a new entry.

*/

public void addFirst(T newEntry);

/**

* Adds a new entry to the end of this collection.

*

* @param newEntry

* The object to be added as a new entry.

*/

public void addLast(T newEntry);

/**

* Removes and returns the first entry in this collection.

*

* @return A reference to the removed entry or null, if the list was empty.

*/

public T removeFirst();

/**

* Removes and returns the last entry in this collection.

*

* @return A reference to the removed entry or null, if the list was empty.

*/

public T removeLast();

/**

* Retrieves the first entry in this collection.

*

* @return A reference to the first entry or null, if the list is empty.

*/

public T getFirst();

/**

* Retrieves the last entry in this collection.

*

* @return A reference to the last entry or null, if the list is empty.

*/

public T getLast();

/**

* Moves the entry located at position i in this list, to

* a new location j in this list. This operation is possible

* only if list is not empty and we have at least max(i,j) entries.

*

* @param i Current list entry's location.

* @param j New location of entry previously located at i.

* @throws IndexOutOfBoundsException if either

* i or j is <1 or i j> getLength()

*

*/

public void place(int i, int j);

/**

* Swaps the entries located at positions i and j.

* This operation is possible only if list is not empty

* and we have at least max(i,j) entries.

*

* @param i Previous location of entry to be put at location j.

* @param j Previous location of entry to be put at location i.

* @throws IndexOutOfBoundsException if either

* i or j is <1 or i j> getLength()

*/

public void swap(int i, int j);

/**

* All entries located at positions starting from "start" and ending

* at "end", inclusive, are copied to a new ListInterface and returned.

* Our list contents do not change.

*

* Operation is possible only if we have at least "end" entries.

* "start" and "end" can be equal, but "end" cannot be less than

* "start".

*

* @param start First location of entry to be copied to a new ListInterface.

* @param end Lost location of entry to be copied to a new ListInterface.

* @throws IllegalArgumentException if "start">"end"

* @throws IndexOutOfBoundsException if either

* "start" or "end" is <1 or "start" "end"> getLength()

*

*/

public ListInterface subList(int start, int end);

/**

* If "items" is not null then add all of its entries to our

* list starting at the specified "index", i.e., first entry of "items"

* will be placed at location "index" in our list and so on.

* All of our entries in this list will be shifted starting

* at that position.

*

*

* @param index Location of copied first entry in "items" to be

* in current list.

* @param items List of items to be copied to this current list.

* @throws IndexOutOfBoundsException if either

* "index" < 1 or "index" > getLength()+1

*/

public void addAll(int index, ListInterface items);

/**

* Returns the index/location of the n'th occurrence of the specified

* entry in this list, or -1 if this list does not contain

* the n'th occurrence of this element. Entry can be null.

*

* @param n Integer value indicating n'th copy of this object.

* @param anObject The object which n'th copy needs to be looked up.

* This object can be null.

* @throws IndexOutOfBoundsException if either

* n < 1 or n > getLength()

*/

public int nthIndexOf(int n, T anObject);

/**

* Removes the n'th occurrence of the specified

* element in this list. Returns true if entry's n'th

* occurrence successfully removed, and false if

* the list was empty, or specified

* entry's n'th occurrence did not exist.

*

*

* @param n Integer value indicating n'th copy of this object in this list.

* @param anObject The object which n'th copy needs to be looked up, and

* if exists removed.

* @throws IndexOutOfBoundsException if either

* n < 1 or n > getLength()

* @return true if removed, and false otherwise.

*/

public boolean removeNth(int n, T anObject);

/**

* Returns how many times anObject occurs in this list.

*

* @param anObject Object looked up.

* @return Number of occurrences of anObject in this list.

*/

public int getFrequencyOf(T anObject);

} // end ListInterface

The code the class AList

/**

A class that implements the ADT list by using a resizable array.

Entries in a list have positions that begin with 1.

Duplicate entries are allowed.

@author Frank M. Carrano

@author Timothy M. Henry

@version 4.0

*/

import java.util.Arrays;

public class AList implements ListInterface

{

private T[] list; // Array of list entries; ignore list[0]

private int numberOfEntries;

private boolean initialized = false;

private static final int DEFAULT_CAPACITY = 5;

private static final int MAX_CAPACITY = 10000;

public AList()

{

this(DEFAULT_CAPACITY);

} // end default constructor

public AList(int initialCapacity)

{

// Is initialCapacity too small?

if (initialCapacity < DEFAULT_CAPACITY)

initialCapacity = DEFAULT_CAPACITY;

else // Is initialCapacity too big?

checkCapacity(initialCapacity);

// The cast is safe because the new array contains null entries

@SuppressWarnings("unchecked")

T[] tempList = (T[]) new Object[initialCapacity + 1];

list = tempList;

numberOfEntries = 0;

initialized = true;

} // end constructor

public int getSize()

{

return list.length;

}

public void add(T newEntry)

{

checkInitialization();

list[numberOfEntries + 1] = newEntry;

numberOfEntries++;

ensureCapacity();

// add(numberOfEntries + 1, newEntry); // ALTERNATE CODE

} // end add

public void clear()

{ /* < Implementation deferred > */

for (int index = 1; index <= numberOfEntries; index++)

{

list[index + 1] = null;

} // end for

} // end clear

public T[] toArray()

{

checkInitialization();

// The cast is safe because the new array contains null entries

@SuppressWarnings("unchecked")

T[] result = (T[]) new Object[numberOfEntries]; // Unchecked cast

for (int index = 0; index < numberOfEntries; index++)

{

result[index] = list[index + 1];

} // end for

return result;

} // end toArray

public int getLength()

{

return numberOfEntries;

} // end getLength

public boolean isEmpty()

{

return numberOfEntries == 0; // Or getLength() == 0

} // end isEmpty

// Doubles the capacity of the array list if it is full.

// Precondition: checkInitialization has been called.

private void ensureCapacity()

{

int capacity = list.length - 1;

if (numberOfEntries >= capacity)

{

int newCapacity = 2 * capacity;

checkCapacity(newCapacity); // Is capacity too big?

list = Arrays.copyOf(list, newCapacity + 1);

} // end if

} // end ensureCapacity

/*

* < This class will define checkCapacity, checkInitialization, and two more

* private methods that will be discussed later. >

*/

private void checkInitialization()

{

if (!initialized)

throw new SecurityException("AList object is not initialized properly!");

}

private void checkCapacity(int capacity)

{

if (capacity > MAX_CAPACITY)

throw new IllegalStateException(

"Attempt to create a list whose capacity " + "exceeds allowed maximum of " + MAX_CAPACITY);

}

// Precondition: The array list has room for another entry.

public void add(int newPosition, T newEntry)

{

checkInitialization();

if ((newPosition >= 1) && (newPosition <= numberOfEntries + 1))

{

if (newPosition <= numberOfEntries)

makeRoom(newPosition);

list[newPosition] = newEntry;

numberOfEntries++;

ensureCapacity(); // Ensure enough room for next add

} else

throw new IndexOutOfBoundsException("Given position of add's new entry is out of bounds.");

} // end add

// Makes room for a new entry at newPosition.

// Precondition: 1 <= newPosition <= numberOfEntries + 1;

// numberOfEntries is list's length before addition;

// checkInitialization has been called.

private void makeRoom(int newPosition)

{

assert (newPosition >= 1) && (newPosition <= numberOfEntries + 1);

int newIndex = newPosition;

int lastIndex = numberOfEntries;

// Move each entry to next higher index, starting at end of

// list and continuing until the entry at newIndex is moved

for (int index = lastIndex; index >= newIndex; index--)

list[index + 1] = list[index];

} // end makeRoom

public T remove(int givenPosition)

{

checkInitialization();

if ((givenPosition >= 1) && (givenPosition <= numberOfEntries))

{

assert !isEmpty();

T result = list[givenPosition]; // Get entry to be removed

// Move subsequent entries towards entry to be removed,

// unless it is last in list

if (givenPosition < numberOfEntries)

removeGap(givenPosition);

numberOfEntries--;

return result;

} else

throw new IndexOutOfBoundsException("Illegal position given to remove operation.");

} // end remove

// Shifts entries that are beyong the entry to be removed to the

// next lower position.

// Precondition: 1 <= givenPosition < numberOfEntries;

// numberOfEntries is list's length before removal;

// checkInitialization has been called.

private void removeGap(int givenPosition)

{

assert (givenPosition >= 1) && (givenPosition < numberOfEntries);

int removedIndex = givenPosition;

int lastIndex = numberOfEntries;

for (int index = removedIndex; index < lastIndex; index++)

list[index] = list[index + 1];

}

public T replace(int givenPosition, T newEntry)

{

checkInitialization();

if ((givenPosition >= 1) && (givenPosition <= numberOfEntries))

{

assert !isEmpty();

T originalEntry = list[givenPosition];

list[givenPosition] = newEntry;

return originalEntry;

} else

throw new IndexOutOfBoundsException("Illegal position given to replace operation.");

} // end replace

public T getEntry(int givenPosition)

{

checkInitialization();

if ((givenPosition >= 1) && (givenPosition <= numberOfEntries))

{

assert !isEmpty();

return list[givenPosition];

} else

throw new IndexOutOfBoundsException("Illegal position given to getEntry operation.");

} // end getEntry

public boolean contains(T anEntry)

{

checkInitialization();

boolean found = false;

int index = 1;

while (!found && (index <= numberOfEntries))

{

if (anEntry.equals(list[index]))

found = true;

index++;

} // end while

return found;

} // end contains

} // end AList

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago