JAVA Eclipse You are to write a program name ArrayListjava t
JAVA Eclipse
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.
2.Another constructor that accepts a parameter of type int and sets the size to this parameter ---- public ArrayList(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 size();
7.A method would test to see if the is empty ---- public boolean isEmpty();
8.A method that see if a particular object exist in the --- 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.
Solution
testArray.java
public class testArray
{
public static void main(String a[]){
ArrayList mal = new ArrayList();
mal.add(new Integer(2));
mal.add(new Integer(5));
mal.add(new Integer(1));
mal.add(new Integer(23));
mal.add(new Integer(14));
for(int i=0;i<mal.size();i++){
System.out.print(mal.get(i)+\" \");
}
mal.add(new Integer(29));
System.out.println(\"Element at Index 5:\"+mal.get(5));
System.out.println(\"List size: \"+mal.size());
System.out.println(\"Removing element at index 2: \"+mal.remove(2));
for(int i=0;i<mal.size();i++){
System.out.print(mal.get(i)+\" \");
}
}
}
=======================================================
ArrayList.java
import java.util.Arrays;
public class ArrayList {
private Object[] myStore;
private int actSize = 0;
public ArrayList(){
myStore = new Object[10];
}
public Object get(int index){
if(index < actSize){
return myStore[index];
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
public boolean isEmpty()
{
if(actSize==0)
return true;
return false;
}
public boolean isIn(Object ob)
{
for (int i=0;i<actSize ;i++ ) {
if(myStore[i].equals(ob))
return true;
}
return false;
}
public int find (Object n)
{int i;
for ( i=0;i<actSize ;i++ ) {
if(myStore[i].equals(n))
break;
}
return i;
}
public void add(Object obj){
if(myStore.length-actSize <= 5){
increaseListSize();
}
myStore[actSize++] = obj;
}
public Object remove(int index){
if(index < actSize){
Object obj = myStore[index];
myStore[index] = null;
int tmp = index;
while(tmp < actSize){
myStore[tmp] = myStore[tmp+1];
myStore[tmp+1] = null;
tmp++;
}
actSize--;
return obj;
} else {
throw new ArrayIndexOutOfBoundsException();
}
}
public int size(){
return actSize;
}
private void increaseListSize(){
myStore = Arrays.copyOf(myStore, myStore.length*2);
System.out.println(\"\ New length: \"+myStore.length);
}
}
======================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ javac ArrayList.java
akshay@akshay-Inspiron-3537:~/Chegg$ java ArrayList
2 5 1 23 14
New length: 20
Element at Index 5:29
List size: 6
Removing element at index 2: 1
2 5 23 14 29
============================================
I have done with integer class and if you want to do with deck class then create deck object with your class


