Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Include the methods size(), clear(), and isEmpty() in your List interface: /** @return the size of this List */ int size(); /** Clear this List

Include the methods size(), clear(), and isEmpty() in your List interface:

/** @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 copy HomeworkArrayListDriver to this package.

Implement these methods in your ArrayList class, and test your solution using HomeworkArrayListDriver. >> >> >> CODE TO BE MODIFIED BELOW: >>>>>> LIST INTERFACE

package list;

public interface List { /** * * @param 0 <= ndx <= size * @return the value at the given index in this list */ E get (int ndx); /** * Set the value of the given index to the given value * * @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 this list * @param 0 <= ndx < size */ E remove (int ndx); }

>>>>>> >>>>>>> ARRAYLIST CLASS

package list;

public class ArrayList implements List { private int size = 0; private E[] values; public ArrayList() { this (10); } public ArrayList(int cap) { values = (E[]) new Object[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 names;

System.out.println ("Testing ArrayLists");

names = new ArrayList ();

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");

}

}

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

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899