create a CustomArrayList class, which performs as: * The CustomArrayList class h
ID: 3786481 • Letter: C
Question
create a CustomArrayList class, which performs as: * The CustomArrayList class has these instance variables: * arr --- * a private array, always of size 100, which stores the list's data items, and each data item is of type Object. * NOTE: property initializations are usually done in the constructors, so do that for this assignment too. * numitems --- a private int property, which stores the number of data items currently in the list * EXTRA NOTE: your array is always size 100, but numitems tracks the actual number of data items in your list. For example, 3 data items. (This is the same trick that the ArrayList class uses!) * The CustomArrayList class has this constructor: * a default constructor * The CustomArrayList class has these instance methods, which should perform the same way as in the ArrayList class: (but also there are some extra functionalities described here) public boolean add(Object element) //add to back of list --- * adds data item 'element' to the back of the data items. * But, if the array is full of data and can't fit 'element', then the add routine should only output "Array full, ignoring" and should not add the data item 'element'. * If the add was successful, the function should return true, but if the array was full, the function should return false. public void add(int index, Object element) //insert into list --- * inserts data item 'element' into position 'index', according to the same rules discussed for the ArrayList class. * But, if the array is full of data and can't fit 'element', then the add routine should only output "Array full, ignoring" and should not add the data item 'element'. * If the insertion location 'index' is less than the index of the first data item, or if it is greater than the index that is right AFTER the last data item (since it is LEGAL to insert right after the last data item, since this is "add to back"), then the add routine should only output "Illegal index, ignoring" and should not insert the data item 'element'. * Like ArrayList, your CustomArrayList should ALLOW insertion at the position which is at the back of the data, which is like the single-parameter add ("add to back"). public Object get(int index) --- * returns the data item at position 'index'. * But, if position 'index' is out of range for the current data items, then 'get' should output "Illegal index" and should return null. public Object set(int index, Object element) --- * sets the data item at position 'index', to 'element', and also returns the data item that was there before the set. * But, if position 'index' is out of range for the current data items, then 'set' should output "Illegal index" and should return null. public Object remove(int index) --- * acts just like in the ArrayList class. * But, if position 'index' is out of range for the current data items, then 'remove' should output "Illegal index" and should return null. public int size() public boolean isEmpty() public String toString() --- * these act just like in the ArrayList class. * HINT: the toString should act like ArrayList's, which means the data items should output like: [Hi, Hello, Welcome] There are different ways to accomplish this task.
Explanation / Answer
package com.java2novice.algos;
import java.util.Arrays;
public class CustomArrayList {
private Object[] arr; //creating an object
private int numitems = 0; // number of items at any time in arraylist
public CustomArrayList (){
arr = new Object[100]; //an array of size 100
}
public boolean add(Object element){
if(numitems < 100){
arr[numitems ++] = element; //it can add element at the back if array is not full
return true;
}
else
{
return false; //otherwise it returns false
}
}
public void add(int index, Object element){
if(numitems > 100){
system.out.print("Array full, ignoring"); //adding at particular index
return;
}
else if((index < 0) or (index > 99)){
system.out.print("Illegal index, ignoring"); //if the index size is out of range
return;
}
else
{
arr[index] = element; //index in range and array not full add the element
numitems++;
}
}
public Object get(int index){
if(index < 0 ) or (index >100 ){
system.out.print("Illegal index"); //since index will only lie in range 0-99
return null;
} else {
return arr[index];
}
}
public Object set(int index, Object element)
{
int tmp; //create temp variable to return
if(index < 0 ) or (index >100 ){
system.out.print("Illegal index"); // illegal index
return null;
}
else {
tmp = arr[index]; //storing present value in temp variable
arr[index] = element; //assigning new value
numitems++; //incrementing number of items
return tmp; //return old value
}
}
public Object remove(int index){
if(index < 99 && index > -1){
Object element = arr[index];
arr[index] = null; //set value at index to null
int tmp = index;
while(tmp < numitems ){
arr[tmp] = arr[tmp+1]; //swap the values
arr[tmp+1] = null;
tmp++;
}
numitems --; //decrease numitems
return element;
} else {
system.out.print("Illegal index");
return null;
}
}
public int size(){
return numitems ; //returns number of items
}
public boolean isEmpty(){
if (numitems==0){
return true
}
else
{
return false
}
}
public String toString() {
int i=0;
String result = "[";
while(i<numitems){
result + = get(i) + "," //keep appending to string
}
if(i==numitems){
result = get(i) + "]"; //for the last element exclude extra comma
}
return result;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.