Question

1. Implement the arraylist ADT as MyArrayList class implements Cloneable) with the following public methods: a. append(Object

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 < maxsize)
       obj[initialsize++] = element;
   else
       System.out.println("Full");
}

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

public void contains(Object element){
   int flag = 1;
   for(int i=0;i<initialsize;i++){
       if(obj[i] == element){
       System.out.println("Found");
       flag = 1;
       break;
       }
   }
   if(flag == 0)
       System.out.println("Not Found");
}

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

public int indexOf(Object element){
   for(int i=0;i<initialsize;i++)
   {
       if(obj[i] == element)
           return i;
   }
   return -1;
}

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<initialsize-1;i++){
       obj[i] = obj[i+1];
   }
}

public void remove(Object element){
   for(int i=0;i<initialsize;i++){
       if(obj[i] == element){
           removedAt(i);
           break;
       }
   }
}

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<toIndex;i++){
       removedAt(i);
   }
}

public String toString(){
   String res = " ";
   for(int i=0;i<initialsize;i++){
       res = res + str(obj[i]);
   }
   return res;
}

public void reverse(){
   int last = initialsize -1;
   for(int i=0;i<(initialsize-1)/2;i++){
       Object temp = obj[i];
       obj[i] = obj[last];
       obj[last] = temp;      
       last--;
   }
}

public void merge(MyArrayList arraylist2){
   for(int i=0;i<arraylist2.size();i++){
       obj[initialsize++] = arraylist2[i];
   }
}
public void print(){
   for(int i=0;i<initialsize;i++)
       System.out.print(obj[i] + " ");
}

}

This above will be the Abstract data type.

Now for testing,


public Class Lab2{
   public static void test(){
       MyArrayList list = new MyArrayList();
       list.append(0);
       list.append(1);
       int one = 0,two =1;
       for(int i=0;i<23;i++){
           int third = one + two;
           list.append(third);
           one = two;
           two = third;
       }
       list.print();
       list.reverse();
       list.print();
       MyArrayList list2 = (MyArrayList)list.clone();
       list.remove(3);
       list.print();
       list2.reverse();
       list2.print();
       list.merge(list2);
       list.print();
   }
}

Let this one helps Thank you:)

Add a comment
Know the answer?
Add Answer to:
iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. Create a program named TestArrayList, and declare an ArrayList with an initial capacity of 10....

    3. Create a program named TestArrayList, and declare an ArrayList with an initial capacity of 10. Insert one copy of the string "Python", five copies of the string "Java" followed by four copies of the string "C++" so that your vector is now filled to сараcity. Add a class method named printArrayList to print out all the elements stored within an ArrayList on separate lines. Test your method to be sure it works a) Add a class method named delete...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • Add the following methods to the ArrayList class that we wrote during lecture. You may call...

    Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...

  • Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation...

    Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation for adding and removing items. You should reference the SortedArrayCollection class provided for how these algorithms should be implemented. What needs to change here? Is it a lot of code or not much? Include a toString method that creates and returns a string that correctly represents the current collection. Include a test driver application that demonstrates your class correctly. //--------------------------------------------------------------------------- // LinkedCollection.java // //...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following...

    Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • This is for a java lab, in class instruction we are not allowed to use "this"....

    This is for a java lab, in class instruction we are not allowed to use "this". if anyone can help, and explain the importance of "this" and as to why we should or should not use it. Thank you in advanced. Intarraybag.java: public class IntArrayBag implements Cloneable { // instance variables private int[ ] data; private int manyItems; // constructor : behavior #1 public IntArrayBag( ) { final int INITIAL_CAPACITY = 10; manyItems = 0; data = new int[INITIAL_CAPACITY]; }...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT