Add a previous () method to the MyContainer class. Type the method in the text b
ID: 3607335 • Letter: A
Question
Add a previous() method to the MyContainer class. Type the method in the text box.
Add a hasPrevious() method to the MyContainer class. Type your response in the text box.
MyContainer.java:
Iterator.java:
Explanation / Answer
import java.util.Iterator; public class MyContainer { private E[] items; private int size = 0; public MyContainer(){ this(5); } public MyContainer(int capacity){ items = (E[]) new Object[capacity]; } /** * Returns the item at position idx. * @param idx the index to search in. * @throws ArrayIndexOutOfBoundsException if index is out of range. */ public E get(int idx) { if( idx < 0 || idx >= size ) throw new ArrayIndexOutOfBoundsException( "Index " + idx + "; size " + size ); return items[idx]; } public boolean hasPrevious(){ return items.length > 0; } public E previous(){ if(hasPrevious()){ return items[size-1]; } return null; } /** * Adds an item to this collection, at the end. * @param x any object. * @return true. */ public boolean add(E x) { if(items.length == size) { E[] old = items; items = (E[]) new Object[items.length * 2]; for(int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.