In: Computer Science
CSC202-001 Clark
Problem 1:
For this class Java’s ArrayList is used to store a collection of different elements. Please design and implement your own array-based version of MyArrayList that stores items of type
String and supports the following operations:
+MyArrayList()
//a constructor that creates an empty array list
+emptyHere() : Boolean
// Determines whether the list is empty or not
+sizeHere() : integer
// Returns the # of items stored in the array list
+add(in item : String) : void
// Appends the specified elements to the list
+removeMe(in item : String) : void
// Removes the first occurrence of the specified element from the list (if it is present)
+get(in index : integer) : String
// Returns the element at the specified index from the list. The list will remain unmodified.
+removeAll() : void
// Removes all the items in the list here
2. Your implementation should contain two class attributes. The first, called firstArray, is an
array of String. The second, called size, is of type int. The firstArray class attribute
should be created on the call to the constructor and initially have a size of two (2). The
constructor should also initialize the size attribute to zero (0).
3. On each call to add, check to see if firstArray is full. If adding the new element would
cause the array to overflow, then do the following:
a. please create a new bigger array that is twice the size of original array;
b. copy the whole contents of the original array to new array;
c. add the new element to the new array; and
d. reassign firstArray to reference the new array correctly
4. On each call to remove, check to see if firsttArray is less than or equal to a quarter full. If
removing the specified element would cause the array to be less than 25% full, then
a. remove the selected element from original array;
b. create a new smaller array that is half the size of the original array;
c. copy the whole contents of the original array to the new array; and
d. reassign firstArray to reference this new array.
5. Your implementation should reset the size of firstArray back to size 2 if all the elements
are removed (i.e. removeAll() is called).
6. To make sure that your implementation is working correctly, you will create an
MyArrayListException class. This exception needs to be thrown if any attempt is
made to access an illegal index within the lstArray attribute. Create a file called
MyArrayListException.java containing the following code:
public class MyArrayListException extends RuntimeException {
public MyArrayListException(String s) {
super(s);
}
}
Your submission should NOT contain a main method, the code implementing MyArrayListException, or any extra testing code
You may write any private helper methods, as needed.
You may use notes and help from internet.
Please up vote ,comment if any query . Thanks for Question . Be safe.
Note : check attached image for output ,code compiled and tested in java netbeans ide.
Attached a main class to test program , do not attach it for submission as your question stats.
Program Plan :
Program : *****************************MyArrayList.java**************************************
import java.util.Arrays;
//class MyArrayList
public class MyArrayList {
private String []firstArray; //Array of string
size not assigned
private int size; //size
public MyArrayList() {
this.firstArray=new String[2];
//initial size of array is 2
this.size=0; //size
(number of elements is zero )
}
//if size ==0 return true else false
public boolean emptyHere()
{
return
(this.size==0);
}
public int sizeHere()
{
return
this.size;//return number of item in array
}
public void add(String item) //add item to
array
{
if(this.firstArray.length==size) //if size equal to array
length
{
String []newArray; //new array
//copy old value in new array and size=oldsize*2
newArray =
Arrays.copyOf(this.firstArray,(this.firstArray.length*2));
newArray[size]=item; //add item
this.firstArray=Arrays.copyOf(newArray,newArray.length); //reassign
to old array
++size; //increment size
}
else //if size not equal
to array length
{
this.firstArray[size]=item; //add item and increment size
++size;
}
}
public void removeMe(String item) //remove
{
int itemIndex=-1;
//index of item is -1
for(int
i=0;i<this.size;i++)
{
if(this.firstArray[i]==item) //if item found
{
itemIndex=i;//run a loop from found item to size
for(int j=itemIndex;j<size-1;j++)
{
this.firstArray[j]=this.firstArray[j+1];
}
--this.size; //decrement size
break; //break from loop
}
}
if(itemIndex!=-1) //if
item found we have index in itemIndex
{
int newSize=(size)*100/this.firstArray.length; //get percentage of
size
if(newSize<=25) //if less than 25 or equal
{
String []newArray; //new array
newArray =
Arrays.copyOf(this.firstArray,(this.firstArray.length/2)); //new
array of half size
this.firstArray=Arrays.copyOf(newArray,newArray.length); //copy
values to old array
}
}
}
public String get(int index)
{
if(index>=size)
//index > = size throw exception
throw new MyArrayListException("Invalid Index.");
else
return this.firstArray[index]; //return item
}
public void removeAll() //size=0
{
this.size=0;
this.firstArray=Arrays.copyOf(firstArray, 2); //length is 2
}
}
Program : ************************MyArrayListException.java*********************************
//MyArrayListException class
public class MyArrayListException extends RuntimeException {
public MyArrayListException(String s)
//passed string
{
super(s);
}
}
Program : ***************************Main.java***********************************
public class Main {
public static void main(String[] args) {
MyArrayList obj=new
MyArrayList(); //object creating
System.out.println(obj.emptyHere()); //true if empty
System.out.println(obj.sizeHere()); //get size
obj.add("tinku"); //add
item
System.out.println(obj.emptyHere()); //now false
System.out.println(obj.sizeHere()); //size=1
obj.add("jangid"); //add
jangid
System.out.println(obj.emptyHere()); //false
System.out.println(obj.sizeHere()); //2
obj.add("jaipur");
//add
System.out.println(obj.emptyHere()); //false
System.out.println(obj.sizeHere()); //3
obj.removeMe("tinku");
//remove
System.out.println(obj.emptyHere()); //false
System.out.println(obj.sizeHere()); //2
obj.removeMe("jangid");
//remove
System.out.println(obj.emptyHere()); //false
System.out.println(obj.sizeHere()); //size 1
obj.removeMe("jaipur");
//remove
System.out.println(obj.emptyHere()); //true
System.out.println(obj.sizeHere()); //0
obj.removeAll();
//remove all
System.out.println(obj.emptyHere()); //true
System.out.println(obj.sizeHere()); //0
}
}
Output :
Please up vote ,comment if any query . Be Safe.