In: Computer Science
The purpose of this assignment is to develop your ability to code and understand ArrayLists. We will do this by taking a do-it-yourself (DIY) approach by building our own ArrayList-Like data structure called "ArrayBox". You must:
Create a generic class called ArrayBox that uses an ARRAY to store its objects. Assume the initial size of the array is two(2). Your ArrayBox must automatically double the size of your elements array when it becomes full as described in class (see slides). Your ArrayBox declaration should look similar to this:
public class ArrayBox<E>
{
private E[] elements = (E[])(new Object[2]);
private int end_idx = 0;
}
within this class implement the following public methods declared EXACTLY AS:
public int size()
public boolean add(E e)
public E get(int index)
public E set(int index, E element)
public E remove(int index)
You may add additional methods as you wish to modularize the code better but the above methods must be in your class.
Create a Driver class called ArrayBoxDriver (this will contain your main method). Have this class create an instance of ArrayBox of data type <String> and call a mainMenu() method:
then produces a "menu" that looks similar to this:
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>>
Testing Script (Total 10 marks)
This output script will test:
public boolean add( E item )
Dynamic Array resizing
List the contents of the ArrayBox (option 4)
OUTPUT:
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 1
Enter an String you want to add to the ArrayBox >>1st
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 1
Enter an String you want to add to the ArrayBox >>2nd
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 1
Enter an String you want to add to the ArrayBox >>3rd
(ATTN: array capacity has been resized to hold a maximum of
4) [6 MARKS]
Note: for the full 6 marks this message must be displayed saying
what the size of the new array is (i.e: 4). If it only states
"array has been resized" without specifying the new array size then
only 5 marks given.
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 4 [ 4 MARKS ]
INDEX VALUE
0 1st
1 2nd
2 3rd
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 2
Enter the INDEX of the item you want removed from the box
>>0
The element 1st was removed from the box. [ 2
MARKS]
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 4
INDEX VALUE
0 2nd [1
MARK]
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
For Marking (Total 2.5 marks)
This script tests error handling. Make sure you throw and catch exceptions.
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 2
Enter the INDEX of the item you want removed from the box
>>1000
ERROR! The box is currently empty.
[0.5 marks]
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 1
Enter an String you want to add to the ArrayBox
>>test
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 2
Enter the INDEX of the item you want removed from the box
>>1000
ERROR! Index must be between 0 and 0 [ 0.5
marks]
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 2
Enter the INDEX of the item you want removed from the box
>>-1
ERROR! Index must be between 0 and 0 [0.5
marks]
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 3
Enter the INDEX of the item you want changed from the box
>>1000
Enter the String for the item >>test
ERROR! Index is out-of-bounds [0.5
marks]
=[==[==[=[=================================]=]==]==]=
=[==[==[=[ Welcome to ArrayBox ]=]==]==]=
=[==[==[=[=================================]=]==]==]=
Choose...
1. Add an element(String) to our box.
2. Remove an element(String) from our box.
3. Replace(set) an element(String) from our box.
4. List the contents of the box.
5. Exit program
>> 3
Enter the INDEX of the item you want changed from the box
>>-1
Enter the String for the item >>test
ERROR! Index is out-of-bounds [0.5
marks]
public class ArrayBox<E> {
private E[] elements = (E[])(new
Object[2]);
private int end_idx = 0;
// method to get current size of the list
public int size(){
return end_idx;
}
// method to add an element to the list
public boolean add(E e){
if(end_idx<elements.length){
elements[end_idx++]=e;
return true;
}
E[] newElements =
(E[])(new Object[2*elements.length]);
for(int i=0;
i<elements.length; i++){
newElements[i]=elements[i];
}
elements =
newElements;
elements[end_idx++]=e;
System.out.println("Array has been resized:
"+elements.length);
return true;
}
// method to return an element in the given
index
public E get(int index){
if(size()==0){
System.out.println("ERROR! List is empty ");
return null;
}
if(index>=size()){
System.out.println("ERROR! Index must be between 0 and
"+size());
return null;
}
try{
return elements[index];
}catch(IndexOutOfBoundsException e){
System.out.println("ERROR! Index must be between 0 and
"+size());
return null;
}
}
// method to replace the element in the given
index by given element
public E set(int index, E element){
if(get(index)==null)
return null;
try{
elements[index] = element;
return elements[index];
}catch(IndexOutOfBoundsException e){
System.out.println("ERROR! Index must be between 0 and
"+size());
return null;
}
}
// method to remove element in the
index
public E remove(int index){
E el = get(index);
if(el==null){
return el;
}
// all the elements from
index shifted to left
for(int i=index;
i<end_idx-1; i++){
elements[i] = elements[i+1];
}
end_idx--;
return el;
}
}
-----------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class ArrayBoxDriver {
public static void main(String[] args) {
Scanner in = new
Scanner(System.in);
ArrayBox<String>
list = new ArrayBox<>();
while(true){
mainMenu();
System.out.print(">> ");
String choice = in.nextLine();
if(choice.equals("5"))
break;
String input;
switch(choice){
case "1":
System.out.print("Enter an String you want to add to the ArrayBox
>> ");
input = in.nextLine();
list.add(input);
break;
case "2":
System.out.print("Enter the INDEX of the item you want removed from
the box >> ");
int index = Integer.parseInt(in.nextLine());
list.remove(index);
break;
case "3":
System.out.print("Enter the INDEX of the item you want replaced
from the box >> ");
index = Integer.parseInt(in.nextLine());
System.out.print("Enter an String you want to add to the ArrayBox
>> ");
input = in.nextLine();
list.set(index, input);
break;
case "4":
System.out.println("INDEX\tVALUE");
for(int i=0; i<list.size(); i++){
System.out.println(i+"\t"+list.get(i));
}
}
}
}
private static void mainMenu() {
System.out.println("\n=[==[==[=[=================================]=]==]==]=");
System.out.println("=[==[==[=[
Welcome to ArrayBox ]=]==]==]=");
System.out.println("=[==[==[=[=================================]=]==]==]=");
System.out.println("\nChoose");
System.out.println("1.
Add an element(String) to our box.");
System.out.println("2.
Remove an element(String) from our box.");
System.out.println("3.
Replace(set) an element(String) from our box.");
System.out.println("4.
List the contents of the box.");
System.out.println("5.
Exit program");
}
}
Sample output