Question
Implement these methods: -int size() -boolean isEmpty() -int alphaCounter(char I) -ArrayList getAlphaList(char I) -T removeFirst() -T removeFirstByLetter(char I) -T removeLast() -T removeLastByLetter(char I) -------------------------------------------------------- import
Implement these methods:
-int size()
-boolean isEmpty()
-int alphaCounter(char I)
-ArrayList
-T removeFirst()
-T removeFirstByLetter(char I)
-T removeLast()
-T removeLastByLetter(char I)
--------------------------------------------------------
import ADTs.*; import DataStructures.*; import Exceptions.*; import java.util.ArrayList;
/** * An alphabetic list interface, that takes generic objects which * implement the AlphaKey interface. Users of this list structure * should be able to query/manipulate the list based on objects that start with * a particular letter * @version 18/2/2018 * @author clatulip * @param
private final int alpha = 26; // num letters in alphabet // helper array, keeps track of how many nodes per letter private int[] alphaCount; private LinearNode
/** * Constructor, sets up AlphaList and helper structures */ public AlphaList() { alphaCount = new int[alpha]; first = null; last = null; numNodes = 0; } /** * Adds the specified element to the AlphaList, maintaining the sorted * order of the list. * @param element T element to be added * @throws InvalidArgumentException */ @Override public void add(T element) throws InvalidArgumentException { if (element == null) { throw new InvalidArgumentException("add - element was null"); } String key = element.getKey().toUpperCase(); Character c = key.charAt(0); if (!validChar(c)) { throw new InvalidArgumentException("add - element had invalid key"); } int ascii = getASCII(c); LinearNode
if (current != null) { // adding to the middle current.setPrev(temp); } else { // adding to the end (current == null) // update the current last node last.setNext(temp); last = temp; } }
alphaCount[ascii]++; numNodes++; } /** * Returns (without removing) an ArrayList of T elements, whose key begins * with the specified letter * @param l char letter * @return ArrayList
/** * Returns the number of objects in the AlphaList whose key begins with the * specified letter * @param l char letter * @return int num objects beginning with letter * @throws InvalidArgumentException */ @Override public int alphaCount(char l) throws InvalidArgumentException { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
/** * Removes and returns the specified element, if found * @param element T element to look for * @return T element, if found, null otherwise * @throws EmptyCollectionException * @throws InvalidArgumentException */ @Override public T remove(T element) throws EmptyCollectionException, InvalidArgumentException { if (this.isEmpty()) { throw new EmptyCollectionException("remove"); } if (element == null) { throw new InvalidArgumentException("remove - element was null"); } char c = element.getKey().charAt(0); //check that the key is valid if (!validChar(c)) { throw new InvalidArgumentException("remove element " + "had invalid key"); } //if there are no elements that have a key that begins with this letter if (this.alphaCount(c) == 0) { return null; } //if only one element in the list, we can avoid iteration if (this.size() == 1) { //if it's not a match, return null. if (!first.getElement().getKey().equals(element.getKey())) { return null; } return removeFirst(); } //if it's the first element in the list if (first.getElement().equals(element)) { return removeFirst(); } //if it's the last element in the list if (last.getElement().equals(element)) { return removeLast(); } //if not first, last, or only element, we have to find it LinearNode
/** * Removes and returns the first item in the whole list * @return T element * @throws EmptyCollectionException */ @Override public T removeFirst() throws EmptyCollectionException { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } /** * Removes and returns the last element in the list whose key begins with * the specified letter * @param l char letter * @return T element * @throws EmptyCollectionException */ @Override public T removeFirstByLetter(char l) throws EmptyCollectionException, InvalidArgumentException { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
/** * Removes and returns the last item in the whole list * @return T element * @throws EmptyCollectionException */ @Override public T removeLast() throws EmptyCollectionException { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
/** * Removes and returns the last item in the list whose key begins with the * specified letter * @param l char letter * @return T element * @throws EmptyCollectionException */ @Override public T removeLastByLetter(char l) throws EmptyCollectionException, InvalidArgumentException { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
/** * Checks if alphaList is empty * @return true if empty, false otherwise */ @Override public boolean isEmpty() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }
@Override public int size() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }
// NB: this method we will use for testing // do not remove or change /** * For testing, this returns the alphaCount array * @return int[] alphaCount */ public int[] getAlphaCountArray() { return alphaCount; } /** * Converts a char to an index 0 - 25 * @param l * @return */ private int getASCII(char l) { return Character.toUpperCase(l) - 65; } /** * Validation method for passed-in characters * @param c * @return true/false if char is valid */ private boolean validChar(char c) { int a = this.getASCII(c); return (-1 < a && a < 26); } /** * Iterates to a node based on the passed-in char * @param c * @return a LinearNode */ private LinearNode
while (thisC.compareTo(target) != 0 && current != null) { current = current.getNext(); thisC = current.getElement().getKey().charAt(0); } return current; } @Override public String toString() { String s = ""; LinearNode
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started