SlideShare a Scribd company logo
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;
}
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) {
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
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>(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];
}
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();
}
@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;
}
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();
}
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)
*/
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);
/**
* 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
*/
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;
}
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx

More Related Content

PDF
Hi,I have added the methods and main class as per your requirement.pdf
PDF
ReversePoem.java ---------------------------------- public cl.pdf
PDF
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
PDF
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
PDF
i am looking for help on the method AddSorted and the method Copy only.pdf
PDF
Given below is the completed implementation of MyLinkedList class. O.pdf
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PDF
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
Hi,I have added the methods and main class as per your requirement.pdf
ReversePoem.java ---------------------------------- public cl.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
i am looking for help on the method AddSorted and the method Copy only.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf

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

PDF
For the code below complete the preOrder() method so that it perform.pdf
PDF
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
PDF
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
PDF
For this lab you will complete the class MyArrayList by implementing.pdf
PDF
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
DOCX
Please complete all the code as per instructions in Java programming.docx
DOCX
Note- Can someone help me with the private E get(int index- int curren (1).docx
PDF
Note- Can someone help me with the Public boolean add(E value) method.pdf
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
PDF
public class MyLinkedListltE extends ComparableltEgtg.pdf
PDF
we using java code DynamicArrayjava Replace all .pdf
PDF
The LinkedList1 class implements a Linked list. class.pdf
PDF
Complete the class ArraySet1java which implements the SetA.pdf
DOCX
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
PPTX
Java Generics
PDF
File LinkedList.java Defines a doubly-l.pdf
PDF
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
PDF
package ADTs public interface CollectionADTltTgt .pdf
PDF
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
DOCX
Write a program to find the number of comparisons using the binary se.docx
For the code below complete the preOrder() method so that it perform.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
Please complete all the code as per instructions in Java programming.docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the Public boolean add(E value) method.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
we using java code DynamicArrayjava Replace all .pdf
The LinkedList1 class implements a Linked list. class.pdf
Complete the class ArraySet1java which implements the SetA.pdf
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Generics
File LinkedList.java Defines a doubly-l.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
package ADTs public interface CollectionADTltTgt .pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Write a program to find the number of comparisons using the binary se.docx
Ad

More from GavinUJtMathist (20)

DOCX
License plate and driver's license are used as identifiers for individ.docx
DOCX
Let X be uniformly distributed on -1-1-- Find the density of Y-Xk for.docx
DOCX
Let X-Y be (discrete) random variables- (1) What is the value of i-1mj.docx
DOCX
Let X be a random variable distributed as Poisson distribution with me.docx
DOCX
Let V-W-X- and Y be independent- standard normal random variables- a-.docx
DOCX
Let the remaining lifetime at birth random variable T0 be exponential.docx
DOCX
Let A and B be two events defined on a sample space S such that p(A)-0.docx
DOCX
Leslie-17- mother of 2 weeks old Milton- is a single mother who is new.docx
DOCX
Lee Manufacturing's value of operations is equal to $900 million after.docx
DOCX
Lauren is a college sophomore majoring in business- This semester Laur.docx
DOCX
Lab 8- The Endospore Stain Qovitien 1 at 1 In each column below- welec.docx
DOCX
Know the risk groups- symptoms- signs- causes- preventions and treatme.docx
DOCX
Kindly answer all questions Thank you so much Question 3- Answer the.docx
DOCX
Kedzie Kord Company reported market price of its share as $ 50 per sha.docx
DOCX
Key MetricsStep 1- Write a sentence describing a takeaway from each co.docx
DOCX
John and Lynne have been working together as managers at British Imper.docx
DOCX
k-k-M-1A-1 Tome kinis the fue departisint- Then calousate the percenta.docx
DOCX
Journal entry worksheet Income taxes for the year total $42-000 but wo.docx
DOCX
java programming clemictis of the array mytust Seanner console - mew S.docx
DOCX
John resigned from an assurance engagement because his independence wa.docx
License plate and driver's license are used as identifiers for individ.docx
Let X be uniformly distributed on -1-1-- Find the density of Y-Xk for.docx
Let X-Y be (discrete) random variables- (1) What is the value of i-1mj.docx
Let X be a random variable distributed as Poisson distribution with me.docx
Let V-W-X- and Y be independent- standard normal random variables- a-.docx
Let the remaining lifetime at birth random variable T0 be exponential.docx
Let A and B be two events defined on a sample space S such that p(A)-0.docx
Leslie-17- mother of 2 weeks old Milton- is a single mother who is new.docx
Lee Manufacturing's value of operations is equal to $900 million after.docx
Lauren is a college sophomore majoring in business- This semester Laur.docx
Lab 8- The Endospore Stain Qovitien 1 at 1 In each column below- welec.docx
Know the risk groups- symptoms- signs- causes- preventions and treatme.docx
Kindly answer all questions Thank you so much Question 3- Answer the.docx
Kedzie Kord Company reported market price of its share as $ 50 per sha.docx
Key MetricsStep 1- Write a sentence describing a takeaway from each co.docx
John and Lynne have been working together as managers at British Imper.docx
k-k-M-1A-1 Tome kinis the fue departisint- Then calousate the percenta.docx
Journal entry worksheet Income taxes for the year total $42-000 but wo.docx
java programming clemictis of the array mytust Seanner console - mew S.docx
John resigned from an assurance engagement because his independence wa.docx
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Computing-Curriculum for Schools in Ghana
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Presentation on HIE in infants and its manifestations
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
Computing-Curriculum for Schools in Ghana
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Presentation on HIE in infants and its manifestations
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
RMMM.pdf make it easy to upload and study
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Supply Chain Operations Speaking Notes -ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
102 student loan defaulters named and shamed – Is someone you know on the list?
human mycosis Human fungal infections are called human mycosis..pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Abdominal Access Techniques with Prof. Dr. R K Mishra
Final Presentation General Medicine 03-08-2024.pptx

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