In: Computer Science
Show the values contained in the instance variables elements and numElements of the sample collection after the following sequence of operations:
ArrayCollection<String> sample = new ArrayCollection<String>; sample.add("A"); sample.add("B"); sample.add("C"); sample.add("D"); sample.remove("B");
//---------------------------------------------------------------------------
// 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<T> implements
CollectionInterface<T>
{
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 array 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;
}
}
ArrayCollection.java
public class ArrayCollection<T> implements
CollectionInterface<T> {
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 array 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(element==null) {
return
false;
}
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;
}
public void display() {
System.out.print("[");
for(int i=0;i<numElements;i++)
{
System.out.print(elements[i]+" ");
}
System.out.print("]");
System.out.println();
}
}
Main.java
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method
stub
System.out.println("Constructing
Array with Default Constructer");
ArrayCollection<String>
sample = new ArrayCollection<String>();
System.out.println("Displaying
Collection");
sample.display();
System.out.println("Adding A,B,C,D
to Collection");
sample.add("A");
sample.add("B");
sample.add("C");
sample.add("D");
sample.display();
System.out.println("Removing B from
Collection");
sample.remove("B");
sample.display();
System.out.println("Constructing
Array with Parameterised Constructer");
ArrayCollection<String>
capacitysample = new ArrayCollection<String>(5);
System.out.println("Displaying
Collection");
capacitysample.display();
System.out.println("Adding A,B to
Collection");
capacitysample.add("A");
capacitysample.add("B");
capacitysample.display();
System.out.println("Removing A from
Collection");
capacitysample.remove("A");
capacitysample.display();
}
}
Output
Constructing Array with Default Constructer
Displaying Collection
[]
Adding A,B,C,D to Collection
[A B C D ]
Removing B from Collection
[A D C ]
Constructing Array with Parameterised Constructer
Displaying Collection
[]
Adding A,B to Collection
[A B ]
Removing A from Collection
[B ]
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it
helpful