Please write a driver for this class: import java.util.Arrays; class ArrayList { protected Object[] list; protected int

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Please write a driver for this class: import java.util.Arrays; class ArrayList { protected Object[] list; protected int

Post by answerhappygod »

Please write a driver for this class:
import java.util.Arrays; class ArrayList { protected Object[]
list; protected 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 = list; } } 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 =
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; } return str +
" ]"; } public int indexOf(Object obj) { // found the index of
assigned object for (int i = 0; i < numElement; i++) { if
(list.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.equals(objectList.list)) {
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; } } class ExpenseAccount extends ArrayList
{ // constructor public ExpenseAccount() { // call super class
constructor super(); } // override insert method public void
insert(Object newObj, int index) { // check if ArrayList is full
if(list.length <= index) { // resize it and copy all existing
Object to new list list = Arrays.copyOf(list, list.length + 100); }
// call super class method super.insert(newObj, index); } }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply