Please help me with this Java question with Generics Briefly
Please help me with this Java question with Generics.
Briefly describe which part of code change is needed so that your implementations in Part B can accommodate any type of objects. Meaning it can store any type of objects. Provide at least two lines of code that shows the container gets instandtiated and how an object can be stored inside your container.
myStack2 java JI *myQueuel java JI myQueue2 java JI *Queue java J Stacks,iava agicCont X \"1 5 JI 2 public class MyMagicContainer t private final int capacityIncrement 10 private int internalContainer private int datasize; public My Magic Container internalcontainer new int[capacityIncrement]; datasize 0; 10 11 12 133 public void Add (int newNum 14 if(this datasize internalContainer.length) 15 16 int[] old Arry new int[datasize]; 17 System array copy internalContainer,0 old Arry 0, datasize); 18 internalcontainer. new int capacity Increment datasize]; 19 System arraycopy (oldArry 0, internalContainer,0, datasize); 20 21 internalContainer datasize] new Num 22 datasize++ 23 24 25 26e public int getInt (int pos) return internalContainer [pos]; 28 29 30@ public int etArra eturn internalContainer 31 32 public void setDataSize (int i)t 33 this datasize 34 35 public int Getsize() 36a 37 return datasize; 38 39 40 41e public int Getcontainersize() 42 return internalContainer.length 43 44, 45 46 47 48Solution
For generics, you need to make your Container implementation as follows,
MyMagicContainer<E>
{
wherever your item is used, the data type will become E.
}
and MyStack would be
public class MyStack extends MyMagicContainer<Integer>{
}
Instantiation:
MyMagicContainer<Integer> container = new MyMagicContainer<Integer>();
or
MyMagicContainer<Integer> container = new MyStack();
