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

Using Java Language Add the following methods to the SortedArrayCollection class

ID: 3847927 • Letter: U

Question

Using Java Language

Add the following methods to the SortedArrayCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the SortedArrayCollection, not by calling the previously defined 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.

T smallest() returns null if the collection is empty, otherwise returns the smallest element of the collection.

int greater(T element) returns a count of the number of elements e in the collection that are greater than element, that is such that e.compareTo (element) is > 0.

SortedArrayCollection<T> combine(SortedArrayCollection<T> other) creates and returns a new SortedArrayCollection object that is a combination of this object and the argument object.

Explanation / Answer

Given below is the completed SortedArrayCollection class. A test program is written to test the newly implemented methods - toString(), smallest(), greater(), combine(). Please don't forget to rate the answer if it helped. Thank you.

SortedArrayCollection.java


public class SortedArrayCollection<T> implements CollectionInterface<T>
{
protected final int DEFCAP = 100; // default capacity
protected int origCap; // original capacity
protected T[] elements; // array to hold collection 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,
// indicates add index if not found
public SortedArrayCollection()
{
elements = (T[]) new Object[DEFCAP];
origCap = DEFCAP;
}
public SortedArrayCollection(int capacity)
{
elements = (T[]) new Object[capacity];
this.origCap = capacity;
}
protected void enlarge()
// Increments the capacity of the collection by an amount
// equal to the original capacity.
{
// Create the larger array.
T[] larger = (T[]) new Object[elements.length + origCap];
// Copy the contents from the smaller array into the larger array.
for (int i = 0; i < numElements; i++)
{
larger[i] = elements[i];
}
// Reassign elements reference.
elements = larger;
}
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 and location to the
// array index where target should be inserted.
{
location = 0;
found = false;
if (!isEmpty())
recFind(target, 0, numElements - 1);
}
protected void recFind(T target, int first, int last)
// Used by find.
{
int result; // result of the comparison
if (first > last)
{
found = false;
result = ((Comparable)target).compareTo(elements[location]);
if (result > 0)
location++; // adjust location to indicate insert index
}
else
{
location = (first + last) / 2;
result = ((Comparable)target).compareTo(elements[location]);
if (result == 0) // found target
found = true;
else
if (result > 0) // target too high
recFind(target, location + 1, last);
else // target too low
recFind(target, first, location - 1);
}
}
public boolean add(T element)
// Precondition: element is Comparable to previously added objects
//
// Adds element to this collection.
{
if (numElements == elements.length)
enlarge();
find(element); // sets location to index where element belongs
for (int index = numElements; index > location; index--)
elements[index] = elements[index - 1];
elements[location] = 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)
{
for (int i = location; i <= numElements - 2; i++)
elements[i] = elements[i+1];
elements[numElements - 1] = null;
numElements--;
}
return found;
}
public int size()
// Returns the number of elements on this collection.
{
return numElements;
}
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 isEmpty()
// Returns true if this collection is empty; otherwise, returns false.
{
return (numElements == 0);
}
public boolean isFull()
// This collection is unbounded so always returns false.
{
return false;
}
public String toString()
{
if(isEmpty())
return "";
String str = elements[0].toString();
for(int i = 1; i < numElements; i++)
str += ", " + elements[i];
return str;
}
public T smallest()
{
if(isEmpty())
return null;
else
return elements[0];
}
public int greater(T elem)
{
if(isEmpty())
return 0;
find(elem);
int idx = location;
if(found)
{
while(elements[idx].equals(elem))
idx++;
}
return numElements - idx;
}
SortedArrayCollection<T> combine(SortedArrayCollection<T> other)
{
int idx1 = 0, idx2 = 0, idx3 = 0;
int sz1 = size(), sz2 = other.size();
SortedArrayCollection<T> col = new SortedArrayCollection<>(sz1 + sz2);
while(idx1 < sz1 && idx2 < sz2)
{
if(((Comparable)(elements[idx1])).compareTo(other.elements[idx2]) < 0)
col.add(elements[idx1++]);
else
col.add(other.elements[idx2++]);
}
while(idx1 < sz1)
col.add(elements[idx1++]);
while(idx2 < sz2)
col.add(other.elements[idx2++]);
return col;
}
}

TestSortedCollection.java


public class TestSortedCollection {
public static void main(String[] args) {
SortedArrayCollection<String> fruits = new SortedArrayCollection<String>();
fruits.add("orange");
fruits.add("grapes");
fruits.add("pineapple");
fruits.add("apple");
System.out.println("The fruits collection is " + fruits.toString());
System.out.println("The smallest is " + fruits.smallest());
System.out.println("greater than banana = " + fruits.greater("banana"));
System.out.println("greater than watermelon = " + fruits.greater("watermelon"));
System.out.println("greater than grapes = " + fruits.greater("grapes"));
SortedArrayCollection<String> veggies = new SortedArrayCollection<String>();
veggies.add("tomato");
veggies.add("chilly");
veggies.add("brinjal");
veggies.add("potato");
SortedArrayCollection<String> all = fruits.combine(veggies);
System.out.println("The fruits collection is " + fruits.toString());
System.out.println("The veggies collection is " + veggies.toString());
System.out.println("All combined is " + all.toString());
}
}

output

The fruits collection is apple, grapes, orange, pineapple
The smallest is apple
greater than banana = 3
greater than watermelon = 0
greater than grapes = 2
The fruits collection is apple, grapes, orange, pineapple
The veggies collection is brinjal, chilly, potato, tomato
All combined is apple, brinjal, chilly, grapes, orange, pineapple, potato, tomato

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