MyList.java public interface MyList { /** THE STUDENT MUST WRITE THE METHOD HEAD
ID: 3704249 • Letter: M
Question
MyList.java
public interface MyList { /** THE STUDENT MUST WRITE THE METHOD HEADERS. THE FIRST ONE IS DONE FOR YOU **/ /** * Appends the specified element to the end of this list * @param data * @return boolean */ public boolean add(E data); /** * Inserts the specified element at the specified position in this list. * Shifts the element currently at that position (if any) and any subsequent * elements by adding one to their indices. * @param index - index at which the specified element is to be inserted * @param data - element to be inserted * @return boolean * @throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()) */ add /** * Removes all of the elements from this list */ clear /** * Returns true if this list contains the specified element * @param data * @return boolean */ contains /** * Returns the element at the specified position in this list * @param index * @return E */ get /** * Returns the index of the first occurrence of the specified element in this list * Return, or -1 if this list does not * contain the element * @param data * @return int */ indexOf /** * Returns the index of the last matching of the element in this list * Return -1 if no match * @param data * @return int */ lastIndexOf /** * Returns true if this list contains no elements * @return boolean */ isEmpty /** * Removes the element at the specified position in this list. * Shifts any subsequent elements by subtracting one from their indices. * @param index - index of the element to be removed * @return E - the element that was removed from the list * IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()) */ remove /** * Trims the capacity of this ArrayList instance to be the list's current size. An application can use this * operation to minimize the storage of an ArrayList instance. */ trimToSize /** * Returns the number of elements in this list * @return int */ size }
Instructions for MyArrayList Hopefully you were able to code along with the video to implement your own ArrayList. We will begin with that code and complete the class. Most of the methods were written in the video. The code is also available on Blackboard/Week 4 in MyArrayList.zip Your MyArrayList class where you will implement MyList begins like this: public class MyArrayList extends MyAbstractList implements MyList After the functionality is complete and the bugs are worked out, the next step is to make MyArrayList a generic typed class by adding parameterized data type. Of course, MyAbstractList and MyList will also need the conversion applied to them. For grading, the class header will be: public class MyArrayList extends MyAbstractList implements MyList An example was given of converting Couple.java to a parameterized data type class. In general, the steps are: Add syntax to the class, and possibly some method headers (your IDE will help you know) Change Object to E in definitions of parameters, return types, arrays, and variables Take care when creating new arrays. Though the code below has replaced object with E, we cannot create an array this way Ell mylist - new E[10] // cannot do this nstead, we must create an array of object references and then cast the array to E Ell mylist(Ell) new Object [101: // can do thisExplanation / Answer
public abstract class MyAbstractList<E> implements MyList<E>{
}
//***********************************
public class MyArrayListTester {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyArrayList<Integer> myList = new MyArrayList<>();
myList.add(10);
myList.add(20);
myList.add(30);
System.out.println(myList.contains(30));
System.out.println(myList.get(1));
System.out.println(myList.size());
myList.add(1,15);
System.out.println(myList.get(1));
}
}
//********************************************
public interface MyList<E> {
/**
* THE STUDENT MUST WRITE THE METHOD HEADERS. THE FIRST ONE IS DONE FOR YOU
**/
/**
* * Appends the specified element to the end of this list * @param data
* * @return boolean
*/
public boolean add(E data);
/**
* * Inserts the specified element at the specified position in this list. *
* Shifts the element currently at that position (if any) and any subsequent
* * elements by adding one to their indices. * @param index - index at
* which the specified element is to be inserted * @param data - element to
* be inserted * @return boolean * @throws IndexOutOfBoundsException - if
* the index is out of range (index < 0 || index > size())
*/
public boolean add(int index,E data) throws IndexOutOfBoundsException;
/** * Removes all of the elements from this list */
public void clear();
/** * Returns true if this list contains the specified element * @param data * @return boolean */
public boolean contains(E data);
/** * Returns the element at the specified position in this list * @param index * @return E */
public E get(int index)throws IndexOutOfBoundsException;
/** * Returns the index of the first occurrence of the specified element in this list * Return, or -1 if this list does not * contain the element * @param data * @return int */
public int indexOf(E data);
/** * Returns the index of the last matching of the element in this list * Return -1 if no match
*
* @param data * @return int
*/
public int lastIndexOf(E data);
/** * Returns true if this list contains no elements * @return boolean */
public boolean isEmpty();
/** * Removes the element at the specified position in this list. * Shifts any subsequent elements by subtracting one from their indices. * @param index - index of the element to be removed * @return E - the element that was removed from the list * IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()) */
public E remove(int index)throws IndexOutOfBoundsException;
/** * Trims the capacity of this ArrayList instance to be the list's current size. An application can use this * operation to minimize the storage of an ArrayList instance. */
public void trimToSize();
/** * Returns the number of elements in this list * @return int */
public int size();
}
//************************************
public class MyArrayList<E> extends MyAbstractList<E>{
E[] array ;
int size;
int currentIndex;
@SuppressWarnings("unchecked")
public MyArrayList() {
// TODO Auto-generated constructor stub
array = (E[]) new Object[10];
size = 10;
}
public MyArrayList(int size) {
// TODO Auto-generated constructor stub
array = (E[]) new Object[10];
size = size;
}
@Override
public boolean add(E data) {
// TODO Auto-generated method stub
array[currentIndex] = data;
currentIndex = currentIndex + 1;
return true;
}
@Override
public void clear() {
// TODO Auto-generated method stub
array = (E[]) new Object[10];
currentIndex = 0;
}
@Override
public boolean contains(E data) {
// TODO Auto-generated method stub
for(E elem:array){
if(elem == data){
return true;
}
}
return false;
}
@Override
public E get(int index) throws IndexOutOfBoundsException{
// TODO Auto-generated method stub
if(index<0 || index>size())
throw new IndexOutOfBoundsException();
int count = 0;
for(E elem:array){
if(index == count){
return elem;
}else
count++;
}
return null;
}
@Override
public int indexOf(E data) {
// TODO Auto-generated method stub
int count = 0;
for(E elem:array){
if(elem == data){
return count;
}else
count++;
}
return -1;
}
@Override
public int lastIndexOf(E data) {
// TODO Auto-generated method stub
int count=0;
int match = -1;
for(E elem:array){
if(elem == data){
match = count;
}else
count++;
}
return match;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
if(size!=0)
return false;
else
return true;
}
@Override
public E remove(int index)throws IndexOutOfBoundsException {
// TODO Auto-generated method stub
if(index<0 || index>size())
throw new IndexOutOfBoundsException();
return null;
}
@Override
public void trimToSize() {
// TODO Auto-generated method stub
}
@Override
public int size() {
// TODO Auto-generated method stub
return currentIndex;
}
@Override
public boolean add(int index, E data) throws IndexOutOfBoundsException {
// TODO Auto-generated method stub
E[] newArray = (E[]) new Object[size];
int in = 0;
for(E elm:array){
if(in==index){
newArray[in] = data;
in++;
}
else{
newArray[in] = array[in];
in++;
}
}
array = newArray;
currentIndex = currentIndex+1;
return false;
}
public String getIdendificationString(){
return "Program6,";
}
public int getCapacity(){
if(array[0] instanceof Integer)
return Integer.SIZE * currentIndex;
else if(array[0] instanceof Double)
return Double.SIZE * currentIndex;
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.