Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java Language Add the following methods to the ArrayCollection class, and create a test driver for each to show that they work correctly. In

In Java Language

Add the following methods to the ArrayCollection class, and create a test driver for each to show that they work correctly. In order to practice your array coding skills, code each of these methods by accessing the internal variables of the ArrayCollection, not by calling the previously defined public methods of the class.

String toString() creates and returns a string that correctly represents the current collection. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stored element already provides its own reasonable toString method.

int count(T target) returns a count of the number of elements e in the collection such that e.equals(target) is true.

void removeAll(T target) removes all elements e from the collection such that e.equals(target) is true.

ArrayCollection combine(ArrayCollection other) creates and returns a new ArrayCollection object that is a combination of this object and the argument object.

//--------------------------------------------------------------------------- // CollectionInterface.java by Dale/Joyce/Weems Chapter 5 // // Interface for a class that implements a collection of T. // A collection allows addition, removal, and access of elements. // Null elements are not allowed. Duplicate elements are allowed. //--------------------------------------------------------------------------- package ch05.collections; public interface CollectionInterface { boolean add(T element); // Attempts to add element to this collection. // Returns true if successful, false otherwise. T get(T target); // Returns an element e from this collection such that e.equals(target). // If no such element exists, returns null. boolean contains(T target); // Returns true if this collection contains an element e such that // e.equals(target); otherwise returns false. boolean remove (T target); // Removes an element e from this collection such that e.equals(target) // and returns true. If no such element exists, returns false. boolean isFull(); // Returns true if this collection is full; otherwise, returns false. boolean isEmpty(); // Returns true if this collection is empty; otherwise, returns false. int size(); // Returns the number of elements in this collection. } 
 
 
 
 
 
 
//--------------------------------------------------------------------------- // ArrayCollection.java by Dale/Joyce/Weems Chapter 5 // // Implements the CollectionInterface using an array. // // Null elements are not allowed. Duplicate elements are allowed. // // Two constructors are provided: one that creates a collection of a default // capacity, and one that allows the calling program to specify the capacity. //--------------------------------------------------------------------------- package ch05.collections; public class ArrayCollection implements CollectionInterface { protected final int DEFCAP = 100; // default capacity protected T[] elements; // array to hold collection's elements protected int numElements = 0; // number of elements in this collection // set by find method protected boolean found; // true if target found, otherwise false protected int location; // indicates location of target if found public ArrayCollection() { elements = (T[]) new Object[DEFCAP]; } public ArrayCollection(int capacity) { elements = (T[]) new Object[capacity]; } protected void find(T target) // Searches elements for an occurrence of an element e such that // e.equals(target). If successful, sets instance variables found to true // and location to the index of e. If not successful, sets found to false. { location = 0; found = false; while (location < numElements) { if (elements[location].equals(target)) { found = true; return; } else location++; } } public boolean add(T element) // Attempts to add element to this collection. // Returns true if successful, false otherwise. { if (isFull()) return false; else { elements[numElements] = element; numElements++; return true; } } public boolean remove (T target) // Removes an element e from this collection such that e.equals(target) // and returns true; if no such element exists, returns false. { find(target); if (found) { elements[location] = elements[numElements - 1]; elements[numElements - 1] = null; numElements--; } return found; } public boolean contains (T target) // Returns true if this collection contains an element e such that // e.equals(target); otherwise, returns false. { find(target); return found; } public T get(T target) // Returns an element e from this collection such that e.equals(target); // if no such element exists, returns null. { find(target); if (found) return elements[location]; else return null; } public boolean isFull() // Returns true if this collection is full; otherwise, returns false. { return (numElements == elements.length); } public boolean isEmpty() // Returns true if this collection is empty; otherwise, returns false. { return (numElements == 0); } public int size() // Returns the number of elements in this collection. { return numElements; } } 

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

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

More Books

Students also viewed these Databases questions