class ArrayList{ Object[] list; int size; // Constructor public ArrayList(int n)
ID: 3668938 • Letter: C
Question
class ArrayList{
Object[] list;
int size;
// Constructor
public ArrayList(int n){
list = new Object[n];
size = 0;
}
// Add at the end of the ArrayList
public void add(Object x){
list[size] = x;
size += 1;
}
// Add at specific location in the ArrayList
public void add(int index,Object x){
list[index] = x;
}
// return the Object at an index
public Object get(int index){
return list[index];
}
// return the size of the list
public int size(){
return size;
}
// check if the list is empty or not
public boolean isEmpty(){
if (size == 0) return true;
return false;
}
// find if the object is in list or not
public boolean isIn(Object ob){
if (find(ob) == -1) return false;
return true;
}
// return the first occurance of the object
public int find (Object n){
for (int i = 0; i < size; i++){
if (list[i] == n)
return i;
}
return -1;
}
// remove the first occurence of the Object
public void remove (Object n){
int index = find(n);
if (index == -1)
System.out.println("NO SUCH ITEM IS THERE");
for (int i = index; i < size-1; i++)
list[i] = list[i+1];
}
}
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.
Remember --- The size function should be based upon the actual number of elements you have in the arraylist.
Explanation / Answer
import java.util.*;
class ArrayList{
Object[] list;
int size;
// Constructor
public ArrayList(int n){
list = new Object[n];
size = 0;
}
// Add at the end of the ArrayList
public void add(Object x){
list[size] = x;
size += 1;
}
// Add at specific location in the ArrayList
public void add(int index,Object x){
list[index] = x;
}
// return the Object at an index
public Object get(int index){
return list[index];
}
// return the size of the list
public int size(){
return size;
}
// check if the list is empty or not
public boolean isEmpty(){
if (size == 0) return true;
return false;
}
// find if the object is in list or not
public boolean isIn(Object ob){
if (find(ob) == -1) return false;
return true;
}
// return the first occurance of the object
public int find (Object n){
for (int i = 0; i < size; i++){
if (list[i] == n)
return i;
}
return -1;
}
// remove the first occurence of the Object
public void remove (Object n){
int index = find(n);
if (index == -1)
System.out.println("NO SUCH ITEM IS THERE");
else
{
for (int i = index; i < size-1; i++)
list[i] = list[i+1];
size--;
System.out.println(" Card Removed");
}
}
// shuffle
public void shuffle()
{
Random r = new Random();
Card t;
int j,k;
for(int i=0;i<size/2; i++)
{
j=r.nextInt(size-1);
k=r.nextInt(size-1);
t=(Card)list[j];
list[j]=list[k];
list[k]=t;
}
}
public void showDeck()
{
Card c;
System.out.println(" Displaying Deck ");
for (int i = 0; i < size; i++){
c=(Card)list[i];
System.out.println(c.name+" "+c.number);
}
}
}
class Card
{
int number;
String name;
Card(String name, int number)
{
this.name=name;
this.number=number;
}
void display()
{
System.out.println(this.name+" "+this.number);
}
}
public class DeckOfCards
{
public static void main(String arg[])
{
//System.out.println("Hello");
ArrayList deck=new ArrayList(10);
Card d;
Card c=new Card("Spade",2);
// Here you can add as many cards as you want
deck.add(c);
c=new Card("Spade",5);
deck.add(c);
deck.add(new Card("Spade",10));
deck.add(new Card("Cheery",10));
deck.add(new Card("Diamond",12));
deck.add(new Card("Spade",8));
deck.add(new Card("Cherry",1));
deck.add(new Card("Spade",9));
deck.add(new Card("Diamond",7));
deck.showDeck();
deck.shuffle();
System.out.println(" Shuffle Complete ");
deck.showDeck();
// Get Card from deck and display it
d=(Card)deck.get(3);
d.display();
// Find card in deck
System.out.println(" Find Result: "+deck.find(d));
// remove card from deck
deck.remove(d);
deck.showDeck();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.