In: Computer Science
Complete ArrayCollection.java with following methods (Please complete code in java language):
public ArrayCollection();
public ArrayCollection(int size);
public boolean isEmpty();
public boolean isFull();
public int size(); // Return number of elements in the Collection.
public String toString();
public boolean add(T element); // Add an element into the Collection, return true if successful
public boolean remove(T target); // Remove the target from Collection, return true if successful
public boolean removeAll(T target); // Remove all occurrences of Target, return if successful
public void removeDuplicate(); // Remove duplicated element(s)
public boolean equals(ArrayCollection that); // Return true if this Collection is same as that Collection
public int count(T target); // Return the number of a given target in the Collection
public void merge(ArrayCollection that); // Merge this Collection with that Collection
public void enlarge(int size); //Increase this Collection with additional size elements
public void clear(); // Clear entire Collection
public boolean contains(T target); // Return true if the target is in the Collection
public int findIndex(T target); // Return index of the target
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) {
// 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;
}
}
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(isFull())
enlarge(size());
elements[numberOfElements] =
element;
numberOfElements++;
return true;
}
public boolean remove(T target) {
int index =
findIndex(target);
if(index == -1)
return false;
//not found
//shift other elements
for(int i = index + 1; i <
numberOfElements; i++)
elements[i-1] =
elements[i];
return true;
}
public boolean removeAll(T target) {
if(contains(target)) {
while(contains(target))
remove(target);
return
true;
}
else
return
false;
}
public void removeDuplicate() {
T[] temp = (T[]) (new
Object[numberOfElements]);
int n = 0;
int i = 0, j = 0;
while(i < numberOfElements)
{
boolean dup =
false;
for(int k = 0; k
< i; k++) {
if(elements[k].equals(elements[i])) {
dup = true;
break;
}
}
if(!dup) {
temp[j++] = elements[i];
n++;
}
i++;
}
elements = temp;
numberOfElements = n;
}
public boolean equals(ArrayCollection that) {
// Return true if ArrayCollection are identical.
boolean result = true;
if(size() != that.size())
result =
false;
else {
for(int i = 0; i
< size(); i++) {
if(!elements[i].equals(that.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 < size(); 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.size();
i++) {
add(that.elements[i]);
}
}
public void enlarge(int size) {
// Enlarge elements[] with
additional size
T[] temp = (T[]) new
Object[elements.length + size];
for(int i = 0; i <
numberOfElements; i++)
temp[i] =
elements[i];
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 < size();
i++) {
if(elements[i].equals(target)) {
found = true;
break;
}
}
return found;
}
public int findIndex(T target) {
// Return index of target
int index = -1;
for(int i = 0; i < size();
i++) {
if(elements[i].equals(target)) {
index = i;
break;
}
}
return index;
}
}