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:

  1. Implement the required method in MirrorCollection.java.
  2. Run this program to verify the method was implemented properly.

********************************************************************************

COLLECTIONINTERFACE

public interface CollectionInterface{ void add(T ele); //Add an element to the collection void add(T ele, int index); //Add an element in the index location //if index>number of element of the collection, then append the ele to the collection T get(T ele); // Returns an element e from this collection such that e.equals(ele). // If no such element exists, returns null. T get(int index); // Returns the (index+1)th element in the collection. // If index<0 or index>= the number of elements in this collection, returns null. boolean contains(T ele); // Returns true if this collection contains an element e such that // e.equals(ele) returns true; otherwise returns false. int remove(T ele); // 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. boolean remove(int index); // Removes the element at index position, if index

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 implements CollectionInterface{

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; i0){ tmp = tmp.substring(0, tmp.length() -2); } System.out.println("["+tmp+"]"); }

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 col= new CollectionT(5); col.add(9); col.add(3); col.add(5); col.print(); CollectionInterface res = new MirrorCollection().mirrorCollection(col); res.print(); }

public CollectionInterface mirrorCollection(CollectionInterface col){ // *** Student task #1 ***

/* 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 col) method in this last class to have the mirror result required? It shows a NullPointerException when it reaches the codes:

CollectionInterface res = new MirrorCollection().mirrorCollection(col); res.print();

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!