Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Here it the code form class being referred to: public interface StringListInterface { public void add(String s); public void add(int i, String s) throws IndexOutOfBoundsException;
Here it the code form class being referred to:
public interface StringListInterface { public void add(String s); public void add(int i, String s) throws IndexOutOfBoundsException; public String remove(int i) throws IndexOutOfBoundsException; public String get(int i) throws IndexOutOfBoundsException; public int size(); }
public class StringArrayList implements StringListInterface { String[] array; int size; }
public StringArrayList() { array = new String[10]; size = 0; }
public int size() { return size; } @Override public String get(int i) throws IndexOutOfBoundsException { return array[i]; }
public String get(int i) throws IndexOutOfBoundsException { if (i >= size or ipublic void add(String s) { if (size == array.length) { enlarge(); } array[size] = s; size++; }For each question, write the method assuming that it's part of the stringArrayList we started in lecture. The code is in the notes, linked to from the Schedule page, if you missed class. A. (2 points) Write a public void set(int i, string s) method that sets the element of the list at index i to the value s. That is, it should overwrite the existing element at i. Be sure to handle exceptional conditions appropriately B. (2 points) Using a foreach loop, write a public boolean contains (String s) method that searches the list for an occurrence of a value equivalent to s (be careful with the kind of equality you use!). Return true to indicate that a string equivalent to s is contained in the stringArrayList, and return false to indicate that there is no value in our stringArrayList that is equivalent to s The only variable you may declare is the string declared in the foreach loop: for (string v: array) 1... >. Do not declare any other variables (you will lose points)!! C. (2 points) Write a public int last Indexof (string s) method that searches the list for the last occurrence of a value equivalent to s (be careful with what kind of equality you use!), and returns its index. Return -1 if the value is not found D. (1 point each) Suppose you instantiate a new stringArraytist. What are the size and the length of the backing array: 1. Right after instantiating the list. 2. After you add() 10 items to this list. 3. After you remove(0) five times from this list. 4. After you add() 10 more items to this list. Assume each step happens in sequence (that is, for part 3, you've instantiated the list, added 10 items, and then removed five)
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