Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

package utils; /***************************************************************** * This class ArrayList implements a contiguous block of data * using an...

3 answer below »



package
utils;






/*****************************************************************



* This class ArrayList implements a contiguous block of data



* using an array.



*



*

TODO : Please Complete the Following




*

1. Comment all steps in your methods used for its




*

implementation. Be clear and precise.




*

2. Your comments must clearly explain the step to




*

the satisfaction of the instructor (in brief).




*

3. Use JavaDoc Comments above each method.




*****************************************************************/






public class
ArrayList E>{

//constants




public static final int

DEFAULT_CAPACITY
=
10
;








// these are the data fields listed in the lab documentation




E[]
data
;

// reference for an array of type E




int

size
;

// stores the number of occupied places in the array.








public

ArrayList(){

this(DEFAULT_CAPACITY);



}

@SuppressWarnings("unchecked")

public

ArrayList(int
capacity){

data
= (E[])
new
Object[capacity];




size
=
0
;



}

public boolean

add(E
item){

// creating a placeholder for the current size




int
oldSize =
size
;








// allows the size of array to grow if size > length



ensureCapacity(size
+
1);








// appends to back of list = first vacant spot




data[size] = item;








// increments size = updated number of occupied spots




size++;








//returns true if size updated by 1




return

size
== (oldSize +
1);



}

private void

checkIndex(int
index){

if(index
0
|| index >=
size){

throw new
IndexOutOfBoundsException("Invalid Index: "
+ index);



}
}

public void

clear(){

size
=
0
;



}

public boolean

contains(String value){

for
(int
i =
0
;
i
size
;
i++) {

if
(value !=
data[i]) {

continue;



}

return true;



}

return false;

//NOT FOUND



}

@SuppressWarnings("unchecked")

public void

ensureCapacity(int
targetCapacity){

// checks if needed capacity > current array length




if(targetCapacity >
data.length){

// select the larger of the two (double length or target size)




int
newCapacity = Math.max(targetCapacity,

2
*
data.length
+
1);








//make a new array with the larger size




E[] newArray = (E[])
new
Object[newCapacity];








//copy the values in to the new array




for(int
i =
0
;
i
data.length
;
i++){
newArray[i] =
data[i];



}

// re-assign underlying array reference to new array




data
= newArray;



}
}

public int

get(int
index){

return
(int)
data[index];



}

public int

indexOf(int
value){

for
(int
i =
0
;
i
size
;
i++) {

if(data[i] == value){

return
i;



}
}

return
-1
;

//NOT FOUND



}

// returns true if list has no occupied items




public boolean

isEmpty(){

return

size
==
0
;



}

public short

remove(int
index){

for
(int
i = index;
i
size
-
1
;
i++) {

data[i] =
data[i +
1];



}

size--;



return

0
;



}

public short

set(int
index,
String value){
checkIndex(index);




data[index] = value;



return

0
;



}

public int

size(){

return

size
;



}

//prints out the values in sequence from 0 to length - 1




public
String
toString(){

if(isEmpty()){

return

"[]"
;







}else
{
StringBuilder result =
new
StringBuilder(" ["
+
data[0]);







for
(int
i =
1
;
i
size
;
i++) {
result.append(", ").append(data[i]);



}

return
result +
"]"
;



}
}
}


Answered 8 days After Mar 10, 2023

Solution

Sumit Kumar answered on Mar 19 2023
38 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here