Question: Build a class ExpenseAccount that extends your ArrayList. You should remove the limit on the number of bills that can be placed in an account
Build a class ExpenseAccount that extends your ArrayList. You should remove the limit on the number of bills that can be placed in an account by making your ArrayList dynamically resize itself.
supported code arrayList attacted below
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class ArrayList {
private Object[] list;
private int numElement;
public ArrayList() {
list = new Object[1000];
numElement = 0;
}
public void insert(Object newObj, int index) { // insert an object in certain index of list
if (index > numElement) { // throwing an exception if the argument's index is greater than the number of element
throw new IndexOutOfBoundsException();
}
if (list[index] != null) {
for (int i = numElement; i > index; i--) { // from the inserted index, move one object to the right
list[i] = list[i - 1];
}
}
list[index] = newObj; // insert object to assigned index
numElement++;
}
public Object remove(int index) { // remove an object at assigned index
if (index >= numElement) {
throw new IndexOutOfBoundsException();
}
Object obj = list[index];
for (int i = index; i <= numElement; i++) { // object move to left from the assigned index
list[i] = list[i + 1];
}
numElement--;
return obj;
}
public boolean isEmpty() { // if there is no object in list
if (numElement == 0) {
return true;
} else {
return false;
}
}
public int size() { // the size is the number of object
return numElement;
}
public String toString() { // report the list of object
String str = "[ ";
for (int i = 0; i < numElement; i++) {
str = str + "\t" + list[i];
}
return str + " ]";
}
public int indexOf(Object obj) { // found the index of assigned object
for (int i = 0; i < numElement; i++) {
if (list[i].equals(obj)) {
return i;
}
}
return -1; // index of assigned object not found
}
public boolean equals(Object obj) { // compare sizes and elements in the data structure
ArrayList objectList = (ArrayList) obj; // type casting object
if (numElement != objectList.size()) {
return false;
}
for (int i = 0; i < numElement; i++) {
if (!list[i].equals(objectList.list[i])) {
return false;
}
}
return true;
}
public Object get(int index) { // get an object at specified index
Object obj = null;
if (index >= numElement) {
throw new IndexOutOfBoundsException();
}
obj = list[index];
return obj;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
