Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

These are the directions iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of the arraylist vii.remove all the

These are the directions

image text in transcribed

iii.print out the arraylist iv.reverse all the elements v.print out this arraylist

vi.make a clone of the arraylist

vii.remove all the elements at any odd index of the original arraylist (not the cloned one)

viii.print out the original arraylist

ix.reverse the cloned arraylist x.print out the cloned arraylist (this arraylist should still contain the original sequence of elements in order)

xi.merge the cloned arraylist to the original arraylist (please think about what happens and draw a diagram for yourself to visualize it.)

xii.print out the original arraylist

6.Under the main package, in the Main class, remove the contents of the main method we have added in the last lab (or project) and do the following: Lab2.test();

7.Compile and test your implementation.

This is the code I have written that does not work when I try and follow the directions.

First class.

public Class Lab2{

public static void test(String[] cmd){ MyArrayList list = new MyArrayList(); list.append(0); list.append(1); int one = 0,two =1; for(int i=0;i

Second Java class.

package collection;

public class MyArrayList implements Cloneable{

public Object[] obj;

public int maxsize = 100,initialsize = 0;

public MyArrayList(){ obj = new Object[maxsize]; }

public void append(Object element){ if(initialsize

public void clear(){ obj = new Object[maxsize]; }

public void contains(Object element){ int flag = 1; for(int i=0;i

public Object elementAt(int index){ return obj[index]; }

public int indexOf(Object element){ for(int i=0;i

public void insertAt(int index,Object element){ for(int i=initialsize;i>index;i--){ obj[i] = obj[i-1]; } obj[index] = element; }

public boolean isEmpty(){ if(initialsize == 0) return true; return false;

}

public void removedAt(int index){ for(int i=index;i

public void remove(Object element){ for(int i=0;i

public void replace(int index,Object element){ obj[index] = element; }

public int size(){ return initialsize; }

public boolean ensureCapacity(int minCapacity){ if(initialsize >= minCapacity) return true; return false; }

public Object clone() throws CloneNotSupportedException{ return (MyArrayList) super.clone(); }

public void removeRange(int fromIndex,int toIndex){ for(int i=fromIndex;i

public String toString(){ String res = " "; for(int i=0;i

public void reverse(){ int last = initialsize -1; for(int i=0;i

public void merge(MyArrayList arraylist2){ for(int i=0;i

}

1. Implement the arraylist ADT as MyArrayList class implements Cloneable) with the following public methods: a. append(Object element) - appending the element at the end of the arraylist b. clear() - make the arraylist collection empty C. contains (Object element) - check whether the arraylist contains the element d. elementat(int index) - access the element at the given index e. indexOf(Object element) - find the index of the element f. insert.At(int index, Object element) - insert the element at the given index g.isEmptyo - check whether the array list is empty h. removeAt(int index) - remove the element at the given index i. remove(Object element) - remove the element from the arraylist j. replace(int index, Object element) - replace the element at the given index with the given element k.size() - get the number of elements in the current arraylist 1. ensureCapacity (int minCapacity) - make sure the arraylist gets at least the given capacity m. clone) - returns a cloned copy of this arraylist n. removeRange(int fromIndex, int toIndex) - removes from this arraylist all of the elements whose index is between from Index, inclusive and toIndex, exclusive o.toString(- returns a string representation of this arraylist, containing the String representation of each element p. reverse() - reverse the elements in this arraylist 4. merge(MyArrayList arraylist2) - add all the elements in arraylist2 to the end of this arraylist 2. The MyArrayList class should be created in the collection package. 3. Your implementation should be a self-expanding arraylist, and behave like what we discussed in class. 4. After implementing the MyArrayList class you will test your MyArrayList class. To test the MyArrayList class you will create a Lab2 class under the lab package. 5. In the Lab2 class you will do the following: a. Search to find out what is the Fibonacci number sequence b. create a static method named test and do the following in this method i. create an instance of MyArrayList ii. add the first 25 Fibonacci numbers (starts with 0. 1. 1. 2. 3,5. 8. ...) into the arraylist based on the Fibonacci function, (not manually one by one, but by using a loop)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

2. Are my sources up to date?

Answered: 1 week ago