Anzeige
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.docx
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.docx
Anzeige
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.docx
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.docx
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
Anzeige
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.docx
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.docx
Nächste SlideShare
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Wird geladen in ... 3
1 von 13
Anzeige

Más contenido relacionado

Similar a JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx(20)

Más de GavinUJtMathist(20)

Anzeige

JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx

  1. JAVA Demonstrate the use of your APL in a PartB_Driver class by doing the following. Create a static method called removeConsecutiveDuplicates that removes any consecutive duplicate strings from an array positional list of Strings and returns the number of strings left after the removal. After calling the method, the positional list parameter should contain the same sequence of strings as before but with any consecutive duplicates removed. Illustrate your method using the following sets of strings and display the content of the list after executing the method. NOTE: You MUST use a combination/variation of ALL the add and set methods (AddFirst, AddLast, AddBefore, AddAfter, and set) in creating one of the following original lists. - harry ron tom tom tom hermione - harry harry tom ron mary harry - tom ron harry hermione mary - mary mary tom james hermione hermione james harry harry harry You MUST NOT use any other auxiliary data structure e.g., arrays in your implementation ofremoveConsecutiveDuplicates. Sample Output (for last set of Strings) Original positional list: [0] mary [1] mary [2] tom [3] james [4] hermione [5] hermione [6] james [7] harry [8] harry [9] harry APL.java public class ArrayPositionalList<E> implements PositionalList<E> { private static class ArrPos<E> implements Position<E> { private int index; private E element; public ArrPos(E e, int i) { index = i; element = e; }
  2. public E getElement() throws IllegalStateException { if (index == -1) { throw new IllegalStateException("Position no longer valid"); } return element; } public void setElement(E e) { element = e; } public int getIndex() { return index; } public void setIndex(int i) { index = i; } } public static final int CAPACITY = 16; private ArrPos<E>[] data; private int size = 0; public ArrayPositionalList() { this(CAPACITY); } public ArrayPositionalList(int capacity) {
  3. data = (ArrPos<E>[]) new ArrPos[capacity]; } @Override public int size() { return size; } @Override public boolean isEmpty() { return size == 0; } @Override public Position first() { if (!isEmpty()) return data[0]; return null; } @Override public Position last() { if (!isEmpty()) return data[size - 1]; return null; } @Override
  4. public Position before(Position p) throws IllegalArgumentException { if (p.getIndex() > 0 && p.getIndex() < size) { return data[p.getIndex() - 1]; } if (p.getIndex() == 0) return null; throw new IllegalArgumentException(); } @Override public Position after(Position p) throws IllegalArgumentException { if (p.getIndex() >= 0 && p.getIndex() < size - 1) { return data[p.getIndex() + 1]; } if (p.getIndex() == size - 1) return null; throw new IllegalArgumentException(); } @Override public Position addFirst(E e) { if (size < data.length) { for (int i = size - 1; i >= 0; i--) { data[i + 1] = data[i]; data[i + 1].setIndex(data[i + 1].getIndex() + 1);
  5. } data[0] = new ArrPos<E>(e, 0); size++; return data[0]; } return null; } @Override public Position addLast(E e) { if (size < data.length) { data[size] = new ArrPos<E>(e, size); size++; return data[size - 1]; } return null; } @Override public Position addBefore(Position p, E e) throws IllegalArgumentException { int value = p.getIndex(); if (size < data.length) { for (int i = size - 1; i >= value; i--) { data[i].setIndex(data[i].getIndex() + 1); data[i + 1] = data[i];
  6. } data[value] = new ArrPos<E>(e, value); size++; return data[value]; } return null; } @Override public Position addAfter(Position p, E e) throws IllegalArgumentException { int value = p.getIndex(); if (value >= 0 && value < size) { if (size < data.length) { for (int i = size - 1; i > value; i--) { data[i].setIndex(data[i].getIndex() + 1); data[i + 1] = data[i]; } data[value + 1] = new ArrPos<E>(e, value + 1); size++; return data[value + 1]; } return null; } throw new IllegalArgumentException();
  7. } @Override public E set(Position p, E e) throws IllegalArgumentException { int value = p.getIndex(); if (value >= 0 && value < size) { E item = (E) data[value].getElement(); data[value] = new ArrPos<E>(e, value); return item; } else { throw new IllegalArgumentException(); } } @Override public E remove(Position p) throws IllegalArgumentException { int value = p.getIndex(); if (value >= 0 && value < size) { E item = (E) data[value].getElement(); for (int i = value; i < size - 1; i++) { data[i].setIndex(data[i].getIndex() - 1); data[i] = data[i + 1]; } return item; }
  8. throw new IllegalArgumentException(); } @Override public String toString() { String result = ""; for (int i = 0; i < size; i++) { ArrPos<E> l = data[i]; result = result + "[" + l.getIndex() + "] " + l.element.toString() + " "; } return result; } } Position.java public interface Position<E> { /** * Returns the element stored at this position. * * @return the stored element * @throws IllegalStateException if position no longer valid */ E getElement() throws IllegalStateException; public int getIndex(); }
  9. PositionalList.java public interface PositionalList<E> { /** * Returns the number of elements in the list. * @return number of elements in the list */ int size(); /** * Tests whether the list is empty. * @return true if the list is empty, false otherwise */ boolean isEmpty(); /** * Returns the first Position in the list. * * @return the first Position in the list (or null, if empty) */ Position<E> first(); /** * Returns the last Position in the list. * * @return the last Position in the list (or null, if empty) */
  10. Position<E> last(); /** * Returns the Position immediately before Position p. * @param p a Position of the list * @return the Position of the preceding element (or null, if p is first) * @throws IllegalArgumentException if p is not a valid position for this list */ Position<E> before(Position<E> p) throws IllegalArgumentException; /** * Returns the Position immediately after Position p. * @param p a Position of the list * @return the Position of the following element (or null, if p is last) * @throws IllegalArgumentException if p is not a valid position for this list */ Position<E> after(Position<E> p) throws IllegalArgumentException; /** * Inserts an element at the front of the list. * * @param e the new element * @return the Position representing the location of the new element */ Position<E> addFirst(E e); /**
  11. * Inserts an element at the back of the list. * * @param e the new element * @return the Position representing the location of the new element */ Position<E> addLast(E e); /** * Inserts an element immediately before the given Position. * * @param p the Position before which the insertion takes place * @param e the new element * @return the Position representing the location of the new element * @throws IllegalArgumentException if p is not a valid position for this list */ Position<E> addBefore(Position<E> p, E e) throws IllegalArgumentException; /** * Inserts an element immediately after the given Position. * * @param p the Position after which the insertion takes place * @param e the new element * @return the Position representing the location of the new element * @throws IllegalArgumentException if p is not a valid position for this list
  12. */ Position<E> addAfter(Position<E> p, E e) throws IllegalArgumentException; /** * Replaces the element stored at the given Position and returns the replaced element. * * @param p the Position of the element to be replaced * @param e the new element * @return the replaced element * @throws IllegalArgumentException if p is not a valid position for this list */ E set(Position<E> p, E e) throws IllegalArgumentException; /** * Removes the element stored at the given Position and returns it. * The given position is invalidated as a result. * * @param p the Position of the element to be removed * @return the removed element * @throws IllegalArgumentException if p is not a valid position for this list */ E remove(Position<E> p) throws IllegalArgumentException; }
Anzeige