Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java Language Add the following methods to the ArrayCollection class, and cre

ID: 3823251 • Letter: I

Question

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.

Explanation / Answer

import java.util.Arrays;

public class ArrayCollection<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 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;
}
  
public int count(T target)
{
   int count=0;
   if(numElements==0)
   {
       //arrays is empty
       return count;
   }
   else
   {
       for( location=0;location<numElements;location++)
       {
         
           if(elements[location].equals(target))
           {
               count++;
           }
       }
      
       return count;
      
   }
  
}
  
public void removeAll(T target)
{
  
   if(numElements==0)
   {
       //arrays is empty
   return;
   }
   else
   {
       for( location=0;location<numElements;location++)
       {
         
           if(elements[location].equals(target))
           {

           elements[location] = elements[numElements - 1];
           elements[numElements - 1] = null;
           numElements--;
           }
       }
      
      
   }
}
  
@SuppressWarnings("unchecked")
public ArrayCollection<T> combine(ArrayCollection<T> other)
{
   ArrayCollection<T> combination=new ArrayCollection<T>();
     
   for(location=0;location<other.numElements;location++)
   {
       combination.elements[location]= other.elements[location];
         
  
   }
   for(int i=0; i<this.numElements;i++)
   {
       combination.elements[location++]= this.elements[i];
         
  
   }
combination.numElements=combination.elements.length;     
     

   return combination;
}


  
public static void main(String args[])
{
   ArrayCollection<Integer> integers=new ArrayCollection<Integer>();
   ArrayCollection<Integer> numbers=new ArrayCollection<Integer>();

   integers.add(1);
   integers.add(1);
   integers.add(2);
  
   numbers.add(4);
   System.out.println(integers.count(1));
   integers.removeAll(1);
   System.out.println(Arrays.toString(integers.elements));
  
  
   System.out.println((Arrays.toString(integers.combine(numbers).elements)));

  
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote