(Solved) : Include Methods Size Clear Isempty List Interface Return Size List Int Size Clear List Voi Q43992245 . . .
Include the methods size(), clear(), and isEmpty() in your Listinterface:
/** @return the size of this List */
int size();
/** Clear this List */
void clear();
/** @return true iff this List is empty */
boolean isEmpty();
Create a new package in your dsa project, listDriver, and copyHomeworkArrayListDriver to this package.
Implement these methods in your ArrayList class, and test yoursolution using HomeworkArrayListDriver.
>>
>>
>>
CODE TO BE MODIFIED BELOW:
>>>>>>
LIST INTERFACE
package list;
public interface List <E>
{
/**
*
* @param 0 <= ndx <= size
* @return the value at the given index in thislist
*/
E get (int ndx);
/**
* Set the value of the given index to the givenvalue
*
* @param 0 <= ndx <= size
* @return the old value
*/
E set (int ndx, E value);
/**
* Add the given value at the end of this list
*
*/
void add (E value);
/**
* Insert the given value at the given index
* @param 0 <= ndx <= size
*/
void add(int ndx, E value);
/**
* Remove the value of the given index from thislist
* @param 0 <= ndx < size
*/
E remove (int ndx);
}
>>>>>>
>>>>>>>
ARRAYLIST CLASS
package list;
public class ArrayList <E> implements List <E>
{
private int size = 0;
private E[] values;
public ArrayList()
{
this (10);
}
public ArrayList(int cap)
{
values = (E[]) newObject[cap];
}
public E get (int ndx)
{
return values[ndx];
}
public E set(int ndx, E value)
{
E result = values[ndx];
values[ndx] = value;
return result;
}
public void add(E value)
{
add(size,value);
}
public void add(int ndx, E value)
{
if (values.length == size)
alloc();
for(int i = size; i > ndx;i–)
values[i] =values[i-1];
values[ndx] = value;
size++;
}
private void alloc()
{
E[] tempArray =
(E[]) new Object[2*values.length];
for(int i = 0; i < size;i++)
tempArray[i] =values [i];
values = tempArray;
}
public E remove(int ndx)
{
E result = values[ndx];
for(int i = ndx; i < size-1;i++)
values[i] =values [i + 1];
size–;
return result;
}
}
>>>>
>>>>
>>>>
HOMEWORKARRAYLIST DRIVER:
package listDriver;
import list.*;
public class HomeworkArrayListDriver
{
public static void main()
{
List <String> names;
System.out.println (“Testing ArrayLists”);
names = new ArrayList <String> ();
names.add (“jim”);
names.add (“mary”);
names.add (“joe”);
names.add (“sue”);
System.out.println (names.get(2)); // Should be joe
names.set (2, “Joe”);
System.out.println (names.get(2)); // Should be Joe
System.out.println (names.size()); // Should be 4
names.remove (0);
System.out.println (names.size()); // Should be 3
names.add(0,”joe”);
System.out.println (names.size()); // Should be 4
System.out.println (names.get(3)); // Should be sue
if (names.isEmpty())
System.err.println (“Incorrect, error in isEmpty”);
names.clear();
if (!names.isEmpty())
System.err.println (“Incorrect, error in isEmpty or clear”);
names.add(“sue”);
for (int i=0; i<20; i++)
names.add (“sue” + i); // Add 20 more strings
if (! names.get(20).equals(“sue19”))
System.err.println (“Error, possibly in alloc”);
}
}
Expert Answer
Answer to Include the methods size(), clear(), and isEmpty() in your List interface: /** @return the size of this List */ int size…
OR