Question: Build a class ExpenseAccount that extends your ArrayList. Also implement Cloneable interface. You should remove the limit on the number of bills that can be
Build a class ExpenseAccount that extends your ArrayList. Also implement Cloneable interface. You should remove the limit on the number of bills that can be placed in an account by making your ArrayList dynamically resize itself.
public class ArrayList { private Object[] array = new Object[1]; public void insert(char x, int i) { int counter = 0; if( i > this.size() ) { System.out.println("Index "+ i +" outside of list size;" + this.size()); System.exit(0); } try { Object[] other = new Object[this.array.length+1]; switch(i) { case 0: { other[0] = x; for(int j = 1; j < this.array.length; j++) other[j] = this.array[j-1]; this.array = other; break; } default: { for(int j = 0; j < i; j++) other[j] = this.array[j]; other[i] = x; for(int k = i+1; k < this.array.length; k++) other[k] = this.array[k-1]; this.array = other; break; } } } catch(ArrayIndexOutOfBoundsException exception) { copyArray(); if(++counter == 2){ System.out.println("ERROR"); System.exit(0); } } } void insert(Object object, int index){ if(index>this.size()){ System.out.println("Index "+index +" outside of list size; max: " +this.size()); System.exit(0); } int counter = 0; try { Object[] o = new Object[this.array.length + 1]; switch(index){ case 0:{ o[0] = object; for(int j = 1;j < this.array.length; j++) o[j] = this.array[j-1]; this.array = o; break; } default: { for(int j = 0; j < index; j++) o[j] = this.array[j]; o[index] = object; for(int k = index + 1 ; k < this.array.length; k++) o[k] = this.array[k]; this.array = o; break; } } } catch(ArrayIndexOutOfBoundsException exception) { copyArray(); if(++counter == 2) { System.out.println("ERROR"); System.exit(0); } } } Object remove(int index){ if(index > this.size()){ System.out.println("The index value of "+ index +" is outside of the array size; " + this.size()); System.exit(0); } Object temporary = this.array[index]; int counter = 0; try { Object[] other = new Object[this.array.length - 1]; switch(index) { case 0:{ other[0] = this.array[1]; for(int j = 1;j < this.size(); j++) other[j] = this.array[j+1]; this.array = other; break; } default: { for(int j = 0;j < index; j++) other[j] = this.array[j]; for(int k = index; k < this.size(); k++) other[k] = this.array[k+1]; this.array = other; break; } } } catch(ArrayIndexOutOfBoundsException exception) { copyArray(); if( ++counter == 2 ){ System.out.println("ERROR"); System.exit(0); } } return temporary; } public void copyArray(){ Object[] o = new Object[this.array.length * 2]; System.arraycopy(this.array, 0, o, 0, this.array.length); this.array = o; } public boolean isEmpty() { boolean b = true; for (Object element : this.array) { if(element!=null){ b = false; break; } else { b = true; break; } } return b; } public int size(){ int count = 0; for(int i = 0 ;i < this.array.length; i++) { if(this.array[i] == null) continue; else count++; } return count; } public String toString(){ String string = ""; for(int i = 0; i < this.size(); i++){ if(i < this.size() - 1) string += this.array[i] + ", "; else string += this.array[i]; } return string; } int indexOf(Object object){ int index = -1; for(int i=0;i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
