Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

----ArrayList code below---- import java.util.Arrays; public class ArrayList { //class variables private int numElements = 0; private int currCap = 100; private Object[] list =

image text in transcribed

----ArrayList code below----

import java.util.Arrays;

public class ArrayList {

//class variables

private int numElements = 0;

private int currCap = 100;

private Object[] list = new Object[100];

public void insert(Object obj, int index){

//extends the ArrayList if current capacity is reached

if(numElements == list.length){

currCap*=2;

list = Arrays.copyOf(list, currCap);

}

//bumps all current elements up 1 index to make room for new object

for(int i = numElements; i > index; i--){

list[i] = list[i-1];

}

//adds object

list[index] = obj;

numElements++;//tracks number of elements

}

public void add(Object obj){

//expands the ArrayList if the current capacity is reached

if(numElements == list.length){

currCap*=2;

list = Arrays.copyOf(list, currCap);

}

//adds the object to the farthest index

list[numElements] = obj;

numElements++;

}

public Object remove(int index){

Object item = list[index];

for(int i = index; i

list[i] = list[i+1];

}

numElements--;

return item;

}

public int size(){

return numElements;

}

public boolean isEmpty(){

boolean check = true;

for(int i = 0; i

if(list[i] != null){

check = false;

}

}

return check;

}

public int indexOf(Object obj){

int index = -1;

for(int i = 0; i

if(obj.equals(list[i])){

index = i;

break;

}

}

return index;

}

public String toString(){

String arrayList = "";

for(int i = 0; i

//adds all objects to string followed by a new line

arrayList += list[i].toString() + " ";

}

return arrayList;

}

public boolean equals(Object obj){

ArrayList that = (ArrayList) obj; //type cast

boolean check = true;

if(this.numElements != that.size()){//if ArrayLists have different sizes

check = false;

}

for(int i = 0; i

if(this.get(i) != that.get(i)){ //if elements don't equal

check = false;

}

}

return check;

}

public Object get(int index){

return list[index];

4. 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago