Question
[Java] You are going to to work with a partially filled array in this problem. Write a class to manage an array of Strings. Call
[Java] You are going to to work with a partially filled array in this problem. Write a class to manage an array of Strings. Call the class WordProcessor. WordProcessor constructor takes no parameters but initializes the array so it can hold 8 elements. Define a constant for 8, perhaps INITIAL_CAPACITY.
Provide methods:
public void add(String toAdd) adds the given string at the end of the partially filled array.
public void add(String toAdd, int index) adds the String at the specified index. Grow the array by doubling its size if necessary and then add the element
public void removeAdjacentDuplicates() If elements at adjacent indexes are equal, remove one
private void growIfNeeded() checks to see if the array is full. If it is, it doubles the size of the array and copies the elements to a the new array in the same order.
public String toString() returns a string representation of the array. The string starts with "[" and ends with "]" The elements of the array are separated by commas. There should be no comma at the beginning or end of the list.
Use the following file:
WordProcessorTester.java
public class WordProcessorTester { public static void main(String[] args) { WordProcessor processor = new WordProcessor(); processor.add("Mary"); processor.add("had"); processor.add("a"); processor.add("a"); processor.add("a"); processor.add("a"); processor.add("little"); processor.add("lamb"); System.out.println(processor.toString()); System.out.println("Expected: [Mary, had, a, a, a, a, little, lamb]"); processor.add("Its"); processor.add("fleece"); processor.add("was"); processor.add("was"); processor.add("white"); processor.add("as"); processor.add("snow"); System.out.println(processor.toString()); System.out.println("Expected: [Mary, had, a, a, a, a, little, lamb, Its, fleece, was, was, white, as, snow]"); processor.removeAdjacentDuplicates(); System.out.println(processor.toString()); System.out.println("Expected: [Mary, had, a, little, lamb, Its, fleece, was, white, as, snow]"); processor.add("very", 3); System.out.println(processor.toString()); System.out.println("Expected: [Mary, had, a, very, little, lamb, Its, fleece, was, white, as, snow]"); } }
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