You are going to to work with a partially filled array in this problem. Write a
ID: 3818485 • Letter: Y
Question
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:
1. public void add(String toAdd) adds the given string at the end of the partially filled array.
2. 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
3. public void removeAdjacentDuplicates() If elements at adjacent indexes are equal, remove one
4. 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.
5. 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.
The WordProcessorTester was given:
WordProcessorTester.java
Explanation / Answer
Instead of using String array and giving it a initial Capacity, The simplest way would be to use a ArrayList because it can dynamically increase the size of the array.
import java.util.ArrayList;
import java.util.List;
public class WordProcessor {
Integer INITIAL_CAPACITY=8;
List<String> list = new ArrayList<String>(INITIAL_CAPACITY);
public void add(String toAdd){
list.add(toAdd);
}
public void add(String toAdd, int index){
list.add(index,toAdd);
}
public void removeAdjacentDuplicates(){
Object[] stringList = list.toArray();
for (Object element : stringList) {
if (list.indexOf(element) != list.lastIndexOf(element)) {
list.remove(list.lastIndexOf(element));
}
}
}
public String toString(){
String arrayString=list.toString(); //Converts to a String
return arrayString;
}
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]");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.