In: Computer Science
Using Java Languse, Complete ArraySet.java down below by using (Array collection) :
package Homework3;
public class ArraySet extends ArrayCollection {
public ArraySet() {
}
public ArraySet(int size) {
super(size);
}
public boolean add(T element) {
// Complete your code here
return true;
}
}
ArrayCollection.java:
package Homework3;
public class ArrayCollection {
protected static final int DEFAULT_CAPACITY = 100;
protected T[] elements;
protected int numberOfElements;
public ArrayCollection() {
this(DEFAULT_CAPACITY);
}
public ArrayCollection(int size) {
elements = (T[]) new Object[size];
numberOfElements = 0;
}
public boolean isEmpty() {
return numberOfElements == 0;
}
public boolean isFull() {
return numberOfElements == elements.length;
}
public int size() {
return numberOfElements;
}
public String toString() {
String collection = "";
for (int i = 0; i < numberOfElements; i++)
collection += elements[i] + "\n";
return collection;
}
public boolean add(T element) {
// Complete your code here
return true;
}
public boolean remove(T target) {
// Complete your code here
return true;
}
public boolean removeAll(T target) {
// Complete your code here
return true;
}
public void removeDuplicate() {
// Remove any duplicated elements
}
public boolean equals(ArrayCollection that) {
// Return true if ArrayCollection are identical.
boolean result = true;
// Complete your code here.
return result && this.size() == that.size();
}
public int count(T target) {
// Return count of target occurrences
int c = 0;
// Complete your code here
return c;
}
public void merge(ArrayCollection that) {
// Merge that ArrayCollection into this ArrayCollection
// Complete your code here
}
public void enlarge(int size) {
// Enlarge elements[] with additional size
// Complete your code here
}
public void clear() {
// Remove all elements in the collection
}
//Note: Different from textbook, this implementation has no 'found'
and
'location' attributes.
// There is no find() method.
// There is a new methods findIndex().
public boolean contains(T target) {
// Return true if target is found
boolean found = false;
// Complete your code here
return found;
}
public int findIndex(T target) {
// Return index of target
int index = 0;
// Complete your code here
return index;
}
}
Homework3 class:
public class Homework3 {
public static void main(String[] args) {
ArrayCollection ac1 = new ArrayCollection(); // Calling
Default
Constructor
ArrayCollection ac2 = new ArrayCollection(2); // Calling
overloaded
constructor
ArraySet as1 = new ArraySet();
ac2.add("Apple");
ac2.add("Orange");
ac2.add("Lemon"); // This can't be added into ac2 as collection is
full
System.out.println(ac2.remove("Apple")); // This should return
true
System.out.println(ac2);
ac2.enlarge(10);
ac2.add("Watermelon");
System.out.println("Equals: " + ac1.equals(ac2));
as1.add("Avocado");
as1.add("Avocado"); // This will not be added, since the
collection is "set"
}
}
// ArrayCollection.java
package Homework3;
public class ArrayCollection<T> {
protected static final int DEFAULT_CAPACITY =
100;
protected T[] elements;
protected int numberOfElements;
public ArrayCollection() {
this(DEFAULT_CAPACITY);
}
public ArrayCollection(int size) {
elements = (T[]) new
Object[size];
numberOfElements = 0;
}
public boolean isEmpty() {
return numberOfElements == 0;
}
public boolean isFull() {
return numberOfElements ==
elements.length;
}
public int size() {
return numberOfElements;
}
public String toString() {
String collection = "";
for (int i = 0; i <
numberOfElements; i++)
collection +=
elements[i] + "\n";
return collection;
}
public boolean add(T element) {
// if array is full , enlarge the
array by 1 element
if(numberOfElements ==
elements.length)
{
enlarge(1);
}
// add the element at the end
elements[numberOfElements] =
element;
numberOfElements++;
return true;
}
public boolean remove(T target) {
// loop over the array
for(int
i=0;i<numberOfElements;i++)
{
// if element is
found
if(elements[i].equals(target))
{
// shift the elements left
for(int j=i;j<numberOfElements-1;j++)
{
elements[j] =
elements[j+1];
}
numberOfElements--; // decrement the size
return true;
}
}
return false;
}
public boolean removeAll(T target) {
boolean found = false;
found = contains(target);
// loop that continues till the
collection contains the target
while(contains(target))
{
remove(target);
}
return found;
}
public void removeDuplicate() {
// loop over the elements
for(int
i=0;i<numberOfElements;i++)
{
// loop over the
elements starting next to i
for(int
j=i+1;j<numberOfElements;)
{
// if jth element = ith element, remove jth
element by shifting the elements left
if(elements[i].equals(elements[j]))
{
for(int
k=j;k<numberOfElements-1;k++)
{
elements[k] = elements[k+1];
}
numberOfElements--; //
decrement the size
}else // if elements are not equal increment j
to go to the next element
j++;
}
}
}
public boolean equals(ArrayCollection<T> that)
{
// Return true if ArrayCollection
are identical.
boolean result = true;
// Complete your code here.
// loop to check if count of all
elements in both collection is same
for(int
i=0;i<numberOfElements;i++)
{
// if not same
they are not equal
if(count(elements[i]) != that.count(elements[i]))
{
result = false;
break;
}
}
return result &&
this.size() == that.size();
}
public int count(T target) {
// Return count of target
occurrences
int c = 0;
for(int
i=0;i<numberOfElements;i++)
if(elements[i].equals(target))
c++;
return c;
}
public void merge(ArrayCollection<T> that)
{
// Merge that ArrayCollection into
this ArrayCollection
for(int
i=0;i<that.numberOfElements;i++)
{
this.add(that.elements[i]);
}
}
public void enlarge(int size) {
// Enlarge elements[] with
additional size
T temp[] = (T[])new
Object[numberOfElements + size];
for(int
i=0;i<numberOfElements;i++)
temp[i] =
elements[i];
elements = (T[])new
Object[temp.length];
elements = temp;
}
public void clear() {
// Remove all elements in the
collection
numberOfElements = 0;
}
//Note: Different from textbook, this implementation
has no 'found' and 'location' attributes.
// There is no find() method.
// There is a new methods findIndex().
public boolean contains(T target) {
// Return true if target is
found
boolean found = false;
for(int
i=0;i<numberOfElements;i++)
{
if(elements[i].equals(target))
{
found = true;
break;
}
}
return found;
}
public int findIndex(T target) {
// Return index of target
int index = 0;
// Complete your code here
for(index=0;index <
numberOfElements;index++)
{
if(elements[index].equals(target))
return index;
}
return -1; // element not
found
}
}
//end of ArrayCollection.java
//ArraySet.java
package Homework3;
public class ArraySet<T> extends ArrayCollection<T>
{
public ArraySet() {
}
public ArraySet(int size) {
super(size);
}
public boolean add(T element) {
// if element is not present in the
set, add to the set
if(!contains(element))
{
super.add(element);
return
true;
}
return false;
}
}
//end of ArraySet.java
//Homework3 .java
public class Homework3 {
public static void main(String[] args) {
ArrayCollection<String> ac1 =
new ArrayCollection<String>(); // Calling Default
Constructor
ArrayCollection<String> ac2 =
new ArrayCollection<String>(2); // Calling
overloaded constructor
ArraySet<String> as1 = new
ArraySet<String>();
ac2.add("Apple");
ac2.add("Orange");
ac2.add("Lemon"); // This can't be
added into ac2 as collection is full
System.out.println(ac2.remove("Apple")); // This should return
true
System.out.println(ac2);
ac2.enlarge(10);
ac2.add("Watermelon");
System.out.println("Equals: " +
ac1.equals(ac2));
as1.add("Avocado");
as1.add("Avocado"); // This will
not be added, since the collection is "set"
System.out.println(as1);
}
}
//end of Homework3 .java
Output: