Homework Solution: Complete a StringList class that will behave like ArrayList but will only store String objects….

    Complete a StringList class that will behave like ArrayList but will only store String objects. Code structure below.
    public class StringList {
    /**
    * TODO: Declare private data members including an array of String.
    */
    /**
    * Construct a new StringList with a default size of 10.
    */
    public StringList() {
    //TODO: Complete constructor.
    }
    /**
    * Construct a new StringList with size provided by the parameter.
    * @param size
    */
    public StringList(int size) {
    //TODO: Complete constructor.
    }
    //TODO: Consider one or more private helper methods.
    /**
    * Add the new item to the end of the list.
    *
    * If the array is full it will be resized.
    *
    * Return true if successful and false otherwise.
    * @param item
    * @return
    */
    public boolean add(String item) {
    //TODO: Replace with your code.
    return false;
    }
    /**
    * Add a new item at the given position.
    *
    * If the position is out of range, return false.
    * Otherwise, insert the item in the specified position
    * and return true to indicate success.
    *
    * If the array is full it will be resized.
    * @param item
    * @param position
    * @return
    */
    public boolean add(String item, int position) {
    //TODO: Replace with your code.
    return false;
    }
    /**
    * Remove the item at the position specified. Make sure to
    * shift all other items up to ensure no "empty" spaces in the
    * array.
    *
    * If position is out of range, return null. Otherwise, return
    * the item removed.
    *
    * @param position
    * @return
    */
    public String remove(int position) {
    //TODO: Replace with your code;
    return null;
    }
    /**
    * Remove the first instance of item. Make sure to
    * shift all other items up to ensure no "empty" spaces in the
    * array.
    *
    * If the item does not appear in the array return false, otherwise
    * return true.
    *
    * @param position
    * @return
    */
    public boolean remove(String item) {
    //TODO: Replace with your code.
    return false;
    }
    /**
    * Return true if the array contains item and false otherwise.
    *
    * @param item
    * @return
    */
    public boolean contains(String item) {
    //TODO: Replace with your code.
    return false;
    }
    /**
    * Return the number of valid items in the array.
    *
    * @return
    */
    public int size() {
    //TODO: Replace with your code.
    return -1;
    }
    /**
    * Return true if the list is empty and false otherwise.
    * @return
    */
    public boolean isEmpty() {
    //TODO: Replace with your code.
    return false;
    }
    /**
    * Return the item at position index.
    *
    * If index is out of range, return null.
    *
    * @param index
    * @return
    */
    public String get(int index) {
    //TODO: Replace with your code.
    return null;
    }
    /**
    * Return the String that comes first alphabetically.
    *
    * If the list is empty, return null.
    *
    * @return
    */
    public String smallest() {
    //TODO: Replace with your code.
    return null;
    }
    /**
    * Return the String that comes last alphabetically.
    *
    * If the list is empty, return null.
    *
    * @return
    */
    public String largest() {
    //TODO: Replace with your code.
    return null;
    }
    /**
    * Return the index of the given item.
    *
    * If the item does not exist, return -1;
    *
    * @param item
    * @return
    */
    public int indexOf(String item) {
    //TODO: Replace with your code.
    return -1;
    }
    /**
    * Return the last index of the given item.
    *
    * If the item appears more than once you will return
    * the index where it appears last.
    *
    * If the item does not exist in the array return -1;
    * @param item
    * @return
    */
    public int lastIndexOf(String item) {
    //TODO: Replace with your code.
    return -1;
    }
    /**
    * Return a String representation of the array.
    *
    * A newline (n) character will be appended after each String.
    *
    * ["a", "b", null] would be translated to the following String:
    * "anbn"
    *
    * If the list is empty return the empty string.
    *
    */
    public String toString() {
    //TODO: Replace with your code.
    return null;
    }
    /**
    * Return an array of String that contains all items that appear multiple
    * times in the array data member.
    *
    * If an item appears more than once in the original it will appear only
    * once in the result.
    *
    *
    * Example 1: ["a", "b", "c"] -> []
    * Example 2: ["a", "a", "c"] -> ["a"]
    * Example 2: ["a", "b", "a", "a", "b"] -> ["a", "b"]
    *
    * @return
    */
    public String[] duplicates() {
    //TODO: Replace with your code.
    return null;
    }
    }

    Expert Answer

     
    public class StringList { /** * TODO: Declare private data members including an array of String.

    Complete a StringRegister tabulate that earn behave enjoy AttireRegister beside earn simply ammunition String objects.

    Principle construction adown.

    public tabulate StringRegister {
    /**
    * TODO: Declare special facts constituents including an attire of String.
    */
    /**
    * Construct a innovating StringRegister with a absence largeness of 10.
    */
    public StringList() {
    //TODO: Complete doer.
    }
    /**
    * Construct a innovating StringRegister with largeness supposing by the parameter.
    * @param largeness
    */
    public StringList(int largeness) {
    //TODO: Complete doer.
    }
    //TODO: Consider single or past special abettor methods.
    /**
    * Apppurpose the innovating ace to the object of the register.
    *
    * If the attire is unmeasured it earn be resized.
    *
    * Come-back penny if fortunate and counterfeit inadequately.
    * @param ace
    * @return
    */
    public boolean append(String ace) {
    //TODO: Replace with your principle.
    come-back counterfeit;
    }
    /**
    * Apppurpose a innovating ace at the absorbed posture.
    *
    * If the posture is quenched of order, come-back counterfeit.
    * Inadequately, introduce the ace in the definitive posture
    * and come-back penny to mark achievement.
    *
    * If the attire is unmeasured it earn be resized.
    * @param ace
    * @param posture
    * @return
    */
    public boolean append(String ace, int posture) {
    //TODO: Replace with your principle.
    come-back counterfeit;
    }
    /**
    * Abstract the ace at the posture definitive. Make safe to
    * displace perfect other aces up to ensafe no “empty” spaces in the
    * attire.
    *
    * If posture is quenched of order, come-back inoperative. Inadequately, come-back
    * the ace abstractd.
    *
    * @param posture
    * @return
    */
    public String abstract(int posture) {
    //TODO: Replace with your principle;
    come-back inoperative;
    }
    /**
    * Abstract the primeval example of ace. Make safe to
    * displace perfect other aces up to ensafe no “empty” spaces in the
    * attire.
    *
    * If the ace does referable attributable attributable attributable response in the attire come-back counterfeit, inadequately
    * come-back penny.
    *
    * @param posture
    * @return
    */
    public boolean abstract(String ace) {
    //TODO: Replace with your principle.
    come-back counterfeit;
    }
    /**
    * Come-back penny if the attire contains ace and counterfeit inadequately.
    *
    * @param ace
    * @return
    */
    public boolean contains(String ace) {
    //TODO: Replace with your principle.
    come-back counterfeit;
    }
    /**
    * Come-back the enumerate of cogent aces in the attire.
    *
    * @return
    */
    public int largeness() {
    //TODO: Replace with your principle.
    come-back -1;
    }
    /**
    * Come-back penny if the register is emptiness and counterfeit inadequately.
    * @return
    */
    public boolean isEmpty() {
    //TODO: Replace with your principle.
    come-back counterfeit;
    }
    /**
    * Come-back the ace at posture protest.
    *
    * If protest is quenched of order, come-back inoperative.
    *
    * @param protest
    * @return
    */
    public String gain(int protest) {
    //TODO: Replace with your principle.
    come-back inoperative;
    }
    /**
    * Come-back the String that comes primeval alphabetically.
    *
    * If the register is emptiness, come-back inoperative.
    *
    * @return
    */
    public String meanest() {
    //TODO: Replace with your principle.
    come-back inoperative;
    }
    /**
    * Come-back the String that comes ultimate alphabetically.
    *
    * If the register is emptiness, come-back inoperative.
    *
    * @return
    */
    public String largest() {
    //TODO: Replace with your principle.
    come-back inoperative;
    }
    /**
    * Come-back the protest of the absorbed ace.
    *
    * If the ace does referable attributable attributable attributable depend, come-back -1;
    *
    * @param ace
    * @return
    */
    public int protestOf(String ace) {
    //TODO: Replace with your principle.
    come-back -1;
    }
    /**
    * Come-back the ultimate protest of the absorbed ace.
    *
    * If the ace responses past than uniformly you earn come-back
    * the protest where it responses ultimate.
    *
    * If the ace does referable attributable attributable attributable deppurpose in the attire come-back -1;
    * @param ace
    * @return
    */
    public int ultimateIndexOf(String ace) {
    //TODO: Replace with your principle.
    come-back -1;
    }
    /**
    * Come-back a String fidelity of the attire.
    *
    * A innovatingline (n) record earn be joined forthcoming each String.
    *
    * [“a”, “b”, inoperative] would be translated to the forthcoming String:
    * “anbn”
    *
    * If the register is emptiness come-back the emptiness string.
    *
    */
    public String toString() {
    //TODO: Replace with your principle.
    come-back inoperative;
    }
    /**
    * Come-back an attire of String that contains perfect aces that response multiple
    * times in the attire facts constituent.
    *
    * If an ace responses past than uniformly in the pristine it earn response simply
    * uniformly in the development.
    *
    *
    * Example 1: [“a”, “b”, “c”] -> []
    * Example 2: [“a”, “a”, “c”] -> [“a”]
    * Example 2: [“a”, “b”, “a”, “a”, “b”] -> [“a”, “b”]
    *
    * @return
    */
    public String[] duplicates() {
    //TODO: Replace with your principle.
    come-back inoperative;
    }
    }

    Expert Response

     

    public tabulate StringRegister {

    /**
    * TODO: Declare special facts constituents including an attire of String.
    */

    /**
    * Construct a innovating StringRegister with a absence largeness of 10.
    */

    special String[] attire;

    public static conclusive int CAPACITY = 10;

    public int largeness;

    public int compressiveness;

    public StringList() {
    //TODO: Complete doer.
    attire = innovating String[CAPACITY];

    this.compressiveness = 10;

    largeness = 0;
    }

    /**
    * Construct a innovating StringRegister with largeness supposing by the parameter.
    * @param largeness
    */
    public StringList(int largeness) {
    //TODO: Complete doer.
    attire = innovating String[capacity];

    this.compressiveness = largeness;

    largeness = 0;
    }

    //TODO: Consider single or past special abettor methods.

    /**
    * Apppurpose the innovating ace to the object of the register.
    *
    * If the attire is unmeasured it earn be resized.
    *
    * Come-back penny if fortunate and counterfeit inadequately.
    * @param ace
    * @return
    */
    public boolean append(String ace) {
    //TODO: Replace with your principle.

    if (largeness >= compressiveness) {

    String[] temp = innovating String[2 * attire.length];
    for (int i = 0; i < largeness; ++i) {
    temp[i] = attire[i];
    }

    this.compressiveness = 2 * attire.length;
    attire = temp;
    array[size++] = ace;
    come-back penny;
    }
    else

    {

    array[size++] = ace;
    }
    come-back penny;
    }

    /**
    * Apppurpose a innovating ace at the absorbed posture.
    *
    * If the posture is quenched of order, come-back counterfeit.
    * Inadequately, introduce the ace in the definitive posture
    * and come-back penny to mark achievement.
    *
    * If the attire is unmeasured it earn be resized.
    * @param ace
    * @param posture
    * @return
    */
    public boolean append(String ace, int posture) {
    //TODO: Replace with your principle.

    if (posture >= compressiveness)

    come-back counterfeit;

    else

    {

    size++;

    for (int x = largeness – 1; x > posture; x–) {

    array[x] = attire[x – 1];

    }

    array[position] = ace;

    }
    come-back penny;
    }

    /**
    * Abstract the ace at the posture definitive. Make safe to
    * displace perfect other aces up to ensafe no “empty” spaces in the
    * attire.
    *
    * If posture is quenched of order, come-back inoperative. Inadequately, come-back
    * the ace abstractd.
    *
    * @param posture
    * @return
    */
    public String abstract(int i) {
    //TODO: Replace with your principle;
    String e = “”;
    if (i >= largeness || largeness == 0)
    come-back inoperative;

    else {

    e = attire[i];
    for (int x = i; x < this.array.elongation – 1; x++) {
    array[x] = attire[x + 1];
    }
    size–;
    }
    come-back e;
    }

    /**
    * Abstract the primeval example of ace. Make safe to
    * displace perfect other aces up to ensafe no “empty” spaces in the
    * attire.
    *
    * If the ace does referable attributable attributable attributable response in the attire come-back counterfeit, inadequately
    * come-back penny.
    *
    * @param posture
    * @return
    */
    public boolean abstract(String ace) {
    //TODO: Replace with your principle.

    if (largeness == 0)

    come-back counterfeit;

    else {
    int i;
    for (i = 0; i < largeness; ++i) {
    if (item.equals(array[i]))
    break;
    }
    if (i == largeness)
    come-back counterfeit;
    String e = attire[i];
    for (int x = i; x < this.array.elongation – 1; x++) {
    array[x] = attire[x + 1];
    }
    size–;
    }
    come-back penny;
    }

    /**
    * Come-back penny if the attire contains ace and counterfeit inadequately.
    *
    * @param ace
    * @return
    */
    public boolean contains(String ace) {
    //TODO: Replace with your principle.
    for (int i = 0; i < largeness; ++i) {
    if (item.equals(array[i]))
    come-back penny;
    }
    come-back counterfeit;

    }

    /**
    * Come-back the enumerate of cogent aces in the attire.
    *
    * @return
    */
    public int largeness() {
    //TODO: Replace with your principle.
    come-back largeness;

    }

    /**
    * Come-back penny if the register is emptiness and counterfeit inadequately.
    * @return
    */
    public boolean isEmpty() {
    //TODO: Replace with your principle.
    come-back largeness==0;
    }

    /**
    * Come-back the ace at posture protest.
    *
    * If protest is quenched of order, come-back inoperative.
    *
    * @param protest
    * @return
    */
    public String gain(int protest) {
    //TODO: Replace with your principle.
    if (protest >= compressiveness)

    come-back inoperative;

    else

    come-back attire[index];
    }

    /**
    * Come-back the String that comes primeval alphabetically.
    *
    * If the register is emptiness, come-back inoperative.
    *
    * @return
    */
    public String meanest() {
    //TODO: Replace with your principle.
    come-back inoperative;
    }

    /**
    * Come-back the String that comes ultimate alphabetically.
    *
    * If the register is emptiness, come-back inoperative.
    *
    * @return
    */
    public String largest() {
    //TODO: Replace with your principle.
    come-back inoperative;
    }

    /**
    * Come-back the protest of the absorbed ace.
    *
    * If the ace does referable attributable attributable attributable depend, come-back -1;
    *
    * @param ace
    * @return
    */
    public int protestOf(String ace) {
    //TODO: Replace with your principle.
    come-back -1;
    }

    /**
    * Come-back the ultimate protest of the absorbed ace.
    *
    * If the ace responses past than uniformly you earn come-back
    * the protest where it responses ultimate.
    *
    * If the ace does referable attributable attributable attributable deppurpose in the attire come-back -1;
    * @param ace
    * @return
    */
    public int ultimateIndexOf(String ace) {
    //TODO: Replace with your principle.
    come-back -1;
    }

    /**
    * Come-back a String fidelity of the attire.
    *
    * A innovatingline (n) record earn be joined forthcoming each String.
    *
    * [“a”, “b”, inoperative] would be translated to the forthcoming String:
    * “anbn”
    *
    * If the register is emptiness come-back the emptiness string.
    *
    */
    public String toString() {
    //TODO: Replace with your principle.
    String res = “”;
    for (int i = 0; i < largeness; ++i) {
    res = res + attire[i] + ” “;
    }
    come-back res;
    }

    /**
    * Come-back an attire of String that contains perfect aces that response multiple
    * times in the attire facts constituent.
    *
    * If an ace responses past than uniformly in the pristine it earn response simply
    * uniformly in the development.
    *
    *
    * Example 1: [“a”, “b”, “c”] -> []
    * Example 2: [“a”, “a”, “c”] -> [“a”]
    * Example 2: [“a”, “b”, “a”, “a”, “b”] -> [“a”, “b”]
    *
    * @return
    */
    public String[] duplicates() {
    //TODO: Replace with your principle.
    come-back inoperative;
    }

    }

    ==========================