Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ * * * Purpose: Simulates an ArrayList for Strings. MyStringArrayList * is an abstract data type ( ADT ) . * / public class

/**
* Purpose: Simulates an ArrayList for Strings. MyStringArrayList
* is an abstract data type (ADT).
*/
public class MyStringArrayList {
/**
* Memory reference to the array that we use to store the data.
*/
private String[] array;
/**
* The maximum amount the current array can hold. The default capacity is ten.
*/
private static final int CAPACITY =10;
/**
* Holds the current number of data in the array
*/
private int size;
/**
* Sets the container capacity to be 10 and size to 0.
*/
public MyStringArrayList(){
array = new String[CAPACITY];
size =0;
}
/**
* requires: e != null
* ensures : e added to the end of this array list.
* Add a new element at the end of this list
*
* @param e - element to be added
*/
public void addLast(String e){
if(isFull()){
increaseSize();
}
array[size]= e; //add element e to last empty location.
size++; // increment the size of this array list
}
/**
* This method is only used by this class.
* @returns if this array list is full then it returns true, otherwise returns false.
*/
private boolean isFull(){
return size == array.length;
}
/**
* This method is only used by this class, and is thus detonated by private.
* This method increases the size of the attribute 'array' by the CAPACITY.
* Note: Java uses a garbage collector that will come through and remove the old reference we are overwriting in this method.
*/
private void increaseSize(){
String[] temparray = new String[array.length + CAPACITY];
System.arraycopy(array,0, temparray, 0, array.length);
array = temparray; // make temparray the container
}
/**
* returns the current size (# of elements) of this list
* requires: none
* ensures: self.size == #self.size
*
* @return current size of this array list
*/
public int getSize(){
return size;
}
/**
* Adds a String to the front of this Data Structure.
* @param s any valid string
*/
public void addFront(String s){
//TODO use add last as reference
}
/**
* Uses Linear search to see if param s is contained within the ArrayList.
* @param s any valid string to search
* @return boolean that tells the client if the param s was found.
*/
public boolean contains(String s){
// TODO
return false;
}
/**
* Gets the element at the index i in the ArrayList. Index starts at 0.
* @param i integer specifies the index in the ArrayList that should be returned.
* @return Returns null if out of bound or the String if it is found.
*/
public String getElement(int i){
// This should be implemented first
// TODO
return "";
}
/**
* Removes the element at the index i.
*
* @param i Integer that specifies the index to be removed starting from 0.
* @return Returns the string being removed or null if it is out of bounds.
*/
public String removeElementAt(int i){
// TODO
return "";
}
/**
* This method converts the ArrayList to a string.
*
* @return Passes a String representation of the list starting with < ending with > and each string separated by a comma.
*/
public String toString(){
// TODO
return "<>";
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions