Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write and implement the ArrayList Data Structure You are to write a program name

ID: 3668936 • Letter: W

Question

write and implement the ArrayList Data Structure

You are to write a program name ArrayList.java that creates an ArrayList data structure that is similar to the ArrayList data Structure that exist in the java library. The class must be written to accept any type of Objects. The following must be implemented i.e. YOU must write the code (do not import them from the Java Library): 1. One default constructor that will create an ArrayList object with a default size of 10 --public ArrayListO; 2. Another constructor that accepts a parameter of type int and sets the size to this parameter public Array List(int n); 3. A method that allows you to place a value at the end of the ArrayList public void add(Object x); 4. A method that allows you to place a value at a given location public void add(int index, Object x); 5. A method that allows you to retrieve a value from a given location public Object get(int index); 6. A method that allows you to return the number of elements that is currently in the the ArrayList public int sizeO; 7. A method would test to see if he ArrayList is empty public boolean isEmptyO; 8. A method that see if a particular object exist in the ArrayList -- public boolean isIn(Object ob); 9. A method that will return the location of first occurrence of an Object starting from location 0 -public int find (Object n); 10. A method that will remove the first occurrence of an Object starting from location 0 -public void remove (Object n); Now, write a driver program (the class with the public static void main(String[] args) method) name testarray.java to test the ArrayList data structure you just created. Fill the ArrayList with Card Objects (i.e. you are creating a Deck of Cards - 52) and print these to the screen. Now. write an additional function that would take this ArrayList of Cards, and shuffle it. Write a random shuffling routine (do not invoke the shuffling routine from the library) and now display the shuffled ArrayList (Deck) of Cards. Also test all nine of the above methods. For those that are returning a value, print out the returned value to the screen and for those that are not returning a value, print a message that indicate if it successfully completed its task.

Explanation / Answer

Answer:

import java.util.*;
class Card
{
private String value_of_suit;
private int value_of_suit;
  

public Card( String suit, int value )
{
value_of_suit = suit;
value_of_suit = value;
}

public String name()
{
String[] namesOfCard =
{
"Deuce", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Ace"
};

return namesOfCard[ value_of_suit - 2 ] + " of " + value_of_suit;
}
}

class Testarray
{
   Testarray(){
   //System.out.println("Hello");
   }
public static void printallcards_given_names( ArrayList<Card> a )
{
int i;
System.out.println( "Size is " + a.size() );
for ( i = 0 ; i < a.size() ; i++ )
{
Card c = a.get( i );
System.out.println( "#" + (i+1) + ": " + c.name() );
}
}

public static ArrayList<Card> methodDeck()
{
   int count=0;
  
  
ArrayList<Card> cards_given_names = new ArrayList<Card>();
  
System.out.println(cards_given_names);
String[] namesOfCard =
{
"Ace" , "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Jack", "Queen", "King", "Deuce"
};
String[] givensuits = { "spades", "hearts", "diamonds", "clubs" };

for (int a=0; a < givensuits.length; a++)
{
for (int b=0; b< 13 ; b++)
{
       count++;
cards_given_names.add(new Card(givensuits[a],b));
  
  
  
}
  
}
  
   System.out.println("Number of Card Deck Filled count :"+ count);
  
return cards_given_names;
}

public static void main( String[] args )
{
   Testarray obj=new Testarray();
System.out.println(methodDeck());
   ArrayList al=new ArrayList();
   al.add(1);
   al.add(1,3);
   al.add(2);
   al.add(3);
   al.add(4);
   al.get(1);
   al.size();
   al.isEmpty();
   al.remove(0);
   al.remove();
   System.out.println(al.get(1));
   System.out.println(al.size());
   System.out.println(al.isEmpty());
  
   System.out.println(al.remove(0));
   System.out.println(al);
}

}