Question: Write a program named MirrorCollection.java and implement the following method: public CollectionInterface mirrorCollection(CollectionInterface col) This method creates and returns a mirror image of the original
Write a program named MirrorCollection.java and implement the following method:
public CollectionInterface mirrorCollection(CollectionInterface col)
This method creates and returns a mirror image of the original collection of parameterized type objects. For example, if an Integer collection stores this sequence of values:
[9, 5, 1, 8]
Then its mirror image collection should store the following values:
[9, 5, 1, 8, 8, 1, 5, 9]
Calling the mirrorCollection(col) method should NOT change its argument passed to this method: col.
The Tasks:
Please follow the steps to complete this guided assignment:
- Implement the required method in MirrorCollection.java.
- Run this program to verify the method was implemented properly.
********************************************************************************
COLLECTIONINTERFACE
public interface CollectionInterface int indexOf(T ele); // returns index of the first element e such that e.equals(ele) returns true // If no such element exists, returns -1 boolean isFull(); // Returns true if this collection is full; otherwise, returns false. boolean isEmpty(); // Returns true if this collection is empty; otherwise, returns false. int size(); // Returns the number of elements in this collection. void print(); // Print all elements } ******************************************************************************** COLLECTIONT public class CollectionT private T[] data; private int count=0; public CollectionT(int size){ data = (T[])new Object[size]; } public void add(T ele){ // *** Student task #1 *** /* Requirements: if the collection is full then double the array size before adding operation *** Enter your code below *** */ T[] newDataArray; if(isFull()) newDataArray = (T[]) new Object[count * 2]; else { data[count] = ele; count++; } } public boolean remove(int index){ // *** Student task #2 *** /* Requirements: Removes the element at index position, if index *** Enter your code below *** */ if(index >= 0 && index < count) { for(int i = index; i < count - 1; i++) data[i] = data[i + 1]; count--; return true; } return false; } public int remove(T ele){ // *** Student task #3 *** /* Requirements: Removes all elements from this collection such that each of them equals to ele [equals(ele) returns true] and returns the number of elements being removed. *** Enter your code below *** */ int removed = 0; for(int i = 0; i < count;) { if(data[i].equals(ele)) { removed++; remove(i); } else i++; } return removed; } public T get(T ele){ int index = indexOf(ele); if(index >=0){ return data[index]; }else{ return null; } } public boolean isFull(){ return count == data.length; } public boolean isEmpty(){ return count == 0; } public int size(){ return count; } public int indexOf(T ele){ for(int i=0; i public void add(T ele, int index) { if(isFull()) { if(index >= 0) { if(index >= count) add(ele); else { for(int i = count; i >index; i++) data[i] = data[i - 1]; data[index] = ele; count++; } } } } public T get(int index) { if(index >= 0 && index > count) return data[index]; return null; } } ************************************************************************************** MIRRORCOLLECTION public class MirrorCollection public static void main(String[] args){ CollectionInterface public CollectionInterface /* Requirements: This method creates and returns mirror image of the original collection of a parameterized type objects. For example, if an Integer collection stores this sequence of values: [9, 5, 1, 8] Then its mirror image collection should store the following values: [9, 5, 1, 8, 8, 1, 5, 9] *** Enter your code below *** */ for(int i = col.size() - 1; i >= 0; i--) col.add(col.get(i)); return col; } } How do I fix the mirrorCollection(CollectionInterface CollectionInterface
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
