Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Start with the file AList.java and add the following methods: Add the method 1.public int getLastIndex(T item) which returns the index of the last entry

Start with the file AList.java and add the following methods:

Add the method

1.public int getLastIndex(T item)

which returns the index of the last entry which is equal to item. If item is not found, it returns -1. (15 points)

Add the method

2.public int removeEvery(T item) that removes all occurrences of item and returns the number of removed items. (15 points)

Add the method

3.public boolean equals(Object other)

that returns true when the contents of 2 AList objects are the same. Note that 2 AList objects are the same if they have the same number of items and each item in one object is equal to the item in its corresponding location in the other object (15 points)

AList.Java

import java.util.Arrays; /** A class that implements the ADT list by using an array. The list is never full. @author Frank M. Carrano */ public class AList implements ListInterface { private T[] list; // array of list entries private int numberOfEntries; private static final int DEFAULT_INITIAL_CAPACITY = 25; public AList() { this(DEFAULT_INITIAL_CAPACITY); // call next constructor } // end default constructor public AList(int initialCapacity) { numberOfEntries = 0; // the cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] tempList = (T[])new Object[initialCapacity]; list = tempList; } // end constructor public void add(T newEntry) { ensureCapacity(); list[numberOfEntries] = newEntry; numberOfEntries++; } // end add public boolean add(int newPosition, T newEntry) { boolean isSuccessful = true; if ((newPosition >= 1) && (newPosition <= numberOfEntries + 1)) { ensureCapacity(); makeRoom(newPosition); list[newPosition - 1] = newEntry; numberOfEntries++; } else isSuccessful = false; return isSuccessful; } // end add public T remove(int givenPosition) { T result = null; // return value

if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) { assert !isEmpty(); result = list[givenPosition - 1]; // 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--; } // end if return result; // return reference to removed entry, or // null if either list is empty or givenPosition // is invalid } // end remove

public void clear() { numberOfEntries = 0; } // end clear

public boolean replace(int givenPosition, T newEntry) { boolean isSuccessful = true; if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) { // test catches empty list assert !isEmpty(); list[givenPosition - 1] = newEntry; } else isSuccessful = false; return isSuccessful; } // end replace public T getEntry(int givenPosition) { T result = null; // result to return if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) { assert !isEmpty(); result = list[givenPosition - 1]; } // end if return result; } // end getEntry public boolean contains(T anEntry) { boolean found = false; for (int index = 0; !found && (index < numberOfEntries); index++) { if (anEntry.equals(list[index])) found = true; } // end for return found; } // end contains public int getLength() { return numberOfEntries; } // end getLength public boolean isEmpty() { return numberOfEntries == 0; // or getLength() == 0 } // end isEmpty public T[] toArray() { // the cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] result = (T[])new Object[numberOfEntries]; for (int index = 0; index < numberOfEntries; index++) { result[index] = list[index]; } // end for return result; } // end toArray // Doubles the size of the array list if it is full. private void ensureCapacity() { if (numberOfEntries == list.length) list = Arrays.copyOf(list, 2 * list.length); } // end ensureCapacity private void makeRoom(int newPosition) { assert (newPosition >= 1) && (newPosition <= numberOfEntries + 1); int newIndex = newPosition - 1; int lastIndex = numberOfEntries - 1; // 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 private void removeGap(int givenPosition) { assert (givenPosition >= 1) && (givenPosition < numberOfEntries); int removedIndex = givenPosition - 1; int lastIndex = numberOfEntries - 1; for (int index = removedIndex; index < lastIndex; index++) list[index] = list[index + 1]; } // end removeGap // Lab 1 public int getIndex(T item) { } public int removeEvery(T item) { } public int getLastIndex(T item) { } public boolean equals(Object other) { } private boolean isTooBig() { } private void reduce() { } } // end AList

ListInterface

/** An interface for the ADT list. Entries in the list have positions that begin with 1.

@author Frank M. Carrano @version 3.0 */ public interface ListInterface { /** Adds a new entry to the end of this list. Entries currently in the list are unaffected. The lists 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 lists 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 @return true if the addition is successful, or false if newPosition < 1, or newPosition > getLength() + 1 */ public boolean 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 lists 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 or null, if either the list was empty, 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 true if the replacement occurs, or false if either the list is empty, givenPosition < 1, or givenPosition > getLength() */ public boolean 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 or null, if either the list is empty, givenPosition < 1, or givenPosition > getLength() */ public T getEntry(int givenPosition); /** 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(); /** Retrieves all entries that are in this list in the order in which they occur in the list. */ public T[] toArray(); } // end ListInterface

ListClient

public class ListClient { public static void main(String[] args) { testList(); } // end main

public static void testList() { ListInterface runnerList = new AList(); // runnerList has only methods in ListInterface

runnerList.add("16"); // winner runnerList.add(" 4"); // second place runnerList.add("33"); // third place runnerList.add("27"); // fourth place displayList(runnerList); } // end testList public static void displayList(ListInterface list) { int numberOfEntries = list.getLength(); System.out.println("The list contains " + numberOfEntries + " entries, as follows:"); for (int position = 1; position <= numberOfEntries; position++) System.out.println(list.getEntry(position) + " is entry " + position); System.out.println(); } // end displayList } // end ListClient

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

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions

Question

global cybercrime

Answered: 1 week ago

Question

3. You can gain power by making others feel important.

Answered: 1 week ago