Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Number of entries after call: 6 
List with duplicates removed: [0] mary [1] tom [2] james [3] hermione [4] james [5] harry 

APL.java

public class ArrayPositionalList implements PositionalList {

private static class ArrPos implements Position {

private int index;

private E element;

public ArrPos(E e, int i) {

index = i;

element = e;

}

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[] data;

private int size = 0;

public ArrayPositionalList() {

this(CAPACITY);

}

public ArrayPositionalList(int capacity) {

data = (ArrPos[]) 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

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);

}

data[0] = new ArrPos(e, 0);

size++;

return data[0];

}

return null;

}

@Override

public Position addLast(E e) {

if (size < data.length) {

data[size] = new ArrPos(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];

}

data[value] = new ArrPos(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, value + 1);

size++;

return data[value + 1];

}

return null;

}

throw new IllegalArgumentException();

}

@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, 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;

}

throw new IllegalArgumentException();

}

@Override

public String toString() {

String result = "";

for (int i = 0; i < size; i++) {

ArrPos l = data[i];

result = result + "[" + l.getIndex() + "] " + l.element.toString() + " ";

}

return result;

}

}

Position.java

public interface Position {

/**

* 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();

}

PositionalList.java

public interface PositionalList {

/**

* 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 first();

/**

* Returns the last Position in the list.

*

* @return the last Position in the list (or null, if empty)

*/

Position 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 before(Position 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 after(Position 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 addFirst(E e);

/**

* 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 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 addBefore(Position 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

*/

Position addAfter(Position 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 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 p) throws IllegalArgumentException;

}

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

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions

Question

3. Are our bosses always right? If not, what should we do?

Answered: 1 week ago