You are to write a program name ArrayList.java that creates an ArrayList data st
ID: 3668077 • Letter: Y
Question
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. (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 ArrayList();
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.
Remember --- The size function should be based upon the actual number of elements you have in the arraylist.
Explanation / Answer
testarray.java
public class testarray{
public static void main(String[] args) {
// Instantiate Empty ArrayList with a size of 52
ArrayList deck = new ArrayList(52);
// Build Deck
// adds 13 cards at a time, for each suit.
// clubs
for (int i = 0; i < 13; i++){
Card tempCard = new Card(i+2, "Clubs");
deck.add(tempCard);
}
// hearts
for (int i = 13; i < 26; i++){
Card tempCard = new Card(i-13+2, "Hearts");
deck.add(tempCard);
}
// diamonds
for (int i = 26; i < 39; i++){
Card tempCard = new Card(i-26+2, "Diamonds");
deck.add(tempCard);
}
// spades
for (int i = 39; i < 52; i++){
Card tempCard = new Card(i-39+2, "Spades");
deck.add(tempCard);
}
// PRINT CARDS PRE-SHUFFLE, IN ORDER
System.out.println("Cards in ArrayList, PRE-SHUFFLE: ");
for (int i = 0; i < 52; i++){
Card cardToPrint = (Card) deck.get(i);
if (cardToPrint.getValue() == 11){
System.out.println("Jack of " + cardToPrint.getSuit());
}
else if (cardToPrint.getValue() == 12){
System.out.println("Queen of " + cardToPrint.getSuit());
}
else if (cardToPrint.getValue() == 13){
System.out.println("King of " + cardToPrint.getSuit());
}
else if (cardToPrint.getValue() == 14){
System.out.println("Ace of " + cardToPrint.getSuit());
}
else{
System.out.println(cardToPrint.getValue() + " of " + cardToPrint.getSuit());
}
}
// apply ArrayList's shuffle method to shuffle the deck's objects.
deck.shuffleArray(deck.getObjectArray());
System.out.println();
System.out.println("Cards in ArrayList, POST-SHUFFLE: ");
// print out shuffled deck
for (int i = 0; i < 52; i++){
Card cardToPrint = (Card) deck.get(i);
if (cardToPrint.getValue() == 11){
System.out.println("Jack of " + cardToPrint.getSuit());
}
else if (cardToPrint.getValue() == 12){
System.out.println("Queen of " + cardToPrint.getSuit());
}
else if (cardToPrint.getValue() == 13){
System.out.println("King of " + cardToPrint.getSuit());
}
else if (cardToPrint.getValue() == 14){
System.out.println("Ace of " + cardToPrint.getSuit());
}
else{
System.out.println(cardToPrint.getValue() + " of " + cardToPrint.getSuit());
}
}
System.out.println();
// TEST: ArrayList() - default constructor
// expected values: length, 10; size, 0;
ArrayList testArrayList = new ArrayList();
System.out.println("TEST: ArrayList() Expected: Length = 10, Size = 0");
System.out.println("Results: Length = " + testArrayList.getObjectArrayLength() +
", Size = " + testArrayList.size());
if (testArrayList.getObjectArrayLength() == 10 && testArrayList.size() == 0){
System.out.println("PASS ");
}
else{
System.out.println("FAIL ");
}
// TEST: ArrayList(int x) - overloaded constructor
// expected values: length, 100; size, 0;
ArrayList testArrayList1 = new ArrayList(100);
System.out.println("TEST: ArrayList(int x) Expected: Length = 100, Size = 0");
System.out.println("Results: Length = " + testArrayList1.getObjectArrayLength() +
", Size = " + testArrayList1.size());
if (testArrayList.getObjectArrayLength() == 10 && testArrayList1.size() == 0){
System.out.println("PASS ");
}
else{
System.out.println("FAIL ");
}
// TEST: add(Object objectToAdd) void
// expected values: Length = 1, Size = 1
// adds at first empty spot in array
ArrayList testAddArrayList = new ArrayList(1); // arrayList with length of 1
Object testObject = new Integer(1); // object to add
testAddArrayList.add(testObject); // add testObject to testAddArrayList
System.out.println("TEST: add(Object objectToAdd) Expected: Length = 1, Size = 1");
System.out.println("Results: Length = " + testAddArrayList.getObjectArrayLength() +
", Size = " + testAddArrayList.size());
if (testAddArrayList.getObjectArrayLength()==1 && testAddArrayList.size() == 1){
System.out.println("PASS ");
}
else{
System.out.println("FAIL ");
}
// TEST: add(int index, Object objectToAdd) void
// Will add at specific index, instead of the next null element.
// expected values: Array with one object, at index = 2
ArrayList testAddWithIndexArray = new ArrayList(3);
testAddWithIndexArray.add(2, testObject); // add testObject at index 2
System.out.println("TEST: add(int index, Object objectToAdd)");
System.out.println("Expected: testObject at index 2");
System.out.println("Results: testObject at index: " + testAddWithIndexArray.find(testObject));
if (testAddWithIndexArray.find(testObject) == 2){
System.out.println("PASS ");;
}
else{
System.out.println("Fail ");
}
// TEST: get(int index) returns object
// Will return object at given index
// expected values: returned testObject
ArrayList testGetArray = new ArrayList(5);
testGetArray.add(testObject);
System.out.println("TEST: get(int index)");
System.out.println("Expected: return object at index 0 with integer value 1");
Object testGetObject = testGetArray.get(0);
System.out.println("Results: testObject at index = 0: " + testGetObject.toString());
if (testGetObject.toString().equals("1")){
System.out.println("PASS ");
}
else{
System.out.println("FAIL ");
}
// TEST: size() returns int
// adds 2 objects to testSizeArray, then test size()
// expected values: Size = 2; length doesn't matter.
ArrayList testSizeArray = new ArrayList();
// add two testObjects to testSizeArray
testSizeArray.add(testObject);
testSizeArray.add(testObject);
System.out.println("TEST: size();");
System.out.println("Expected: returns a size of 2.");
System.out.println("Results: testArray.size() = " + testSizeArray.size());
if (testSizeArray.size() == 2){
System.out.println("PASS ");
}
else{
System.out.println("FAIL ");
}
// TEST: isEmpty() returns boolean
// should return boolean value depending on contents of array
// expected values: ArrayList() should be empty. A basic ArrayList is empty.
ArrayList testIsEmptyArray = new ArrayList();
System.out.println("TEST: isEmpty()");
System.out.println("Expected: isEmpty() == true");
System.out.println("Results: testIsEmptyArray.isEmpty() == " + testIsEmptyArray.isEmpty());
if (testIsEmptyArray.isEmpty()==true){
System.out.println("PASS ");
}
else{
System.out.println("FAIL ");
}
// TEST: isIn(Object objectToFind) returns boolean
// after adding testObject, isIn(testObject) should return true
// expected values: testIsInArray.isIn(testObject) == true;
ArrayList testIsInArray = new ArrayList();
// add testObject to testIsInArray
testIsInArray.add(testObject);
System.out.println("TEST: isIn(Object objectToFind)");
System.out.println("Expected: isIn(); == true");
System.out.println("Results: testIsInArray.isIn(testObject) == " + testIsInArray.isIn(testObject));
if (testIsInArray.isIn(testObject)== true){
System.out.println("PASS ");
}
else{
System.out.println("FAIL ");
}
// TEST: find(Object objectToFind) returns int
// will add testObject to testFindArray at index 3
// find(testObject) should return 3
ArrayList testFindArray = new ArrayList();
testFindArray.add(3, testObject);
System.out.println("TEST: find(Object objectToFind)");
System.out.println("Expected: testFindArray.find(testObject) returns: 3");
System.out.println("Results: testFindArray.find(testObject returns: " + testFindArray.find(testObject));
if (testFindArray.find(testObject)==3){
System.out.println("PASS ");
}
else{
System.out.println("FAIL ");
}
// TEST: remove(Object objectToRemove) void
// will add integer object at index 0, 1, 2, and 3. will then remove()
// the first occurrence of testObject (at index 2). the size should decrement by one
// moving the objects back a space.
System.out.println("TEST: remove(Object objectToRemove)");
System.out.println("Expected: testRemoveArray.remove(testObject): size - 1, and objects shift to left");
ArrayList testRemoveArray = new ArrayList();
Object o0 = new Integer(0);
Object o1 = new Integer(1);
Object o2 = new Integer(2);
Object o3 = new Integer(3);
// add test objects to array
testRemoveArray.add(o0);
testRemoveArray.add(o1);
testRemoveArray.add(o2);
testRemoveArray.add(o3);
System.out.println("Array before removal: ");
for (int i = 0; i < testRemoveArray.size(); i++){
System.out.println(testRemoveArray.get(i).toString());
}
// remove object "o2" in the 2nd index
testRemoveArray.remove(o2);
// print contents after removing object "o2"
System.out.println("Array after removing object at index 2.");
for (int i = 0; i < testRemoveArray.size(); i++){
System.out.println(testRemoveArray.get(i).toString());
}
System.out.println("PASS ");
}
}
ArrayList.java
public class ArrayList {
private Object[] objectArray; // main Object array.
private Object[] tempArray; // object array used to help copy/manipulate
// data before inserting/copying back to the
// "main" array.
/*
* ArrayList() default constructor. Creates Object[] with a capacity/length of 10.*/
ArrayList() {
objectArray = new Object[10]; // basic empty array with a size of 10
}
/*
* ArrayList(int newArraySize) constructor that sets objectArray to specific
* size. method parameter: int newArraySize. Takes int from user input and
* passes as an argument in the constructor.
* Post-condition: creates an instance of an ArrayList with specified capacity */
ArrayList(int newArraySize) {
int arraySize = newArraySize; // places argument into arraySize
objectArray = new Object[arraySize]; // passes given arraySize to create
// new array with given integer
}
/*void add(Object objectToAdd) iterates through current objectArray and
* inserts objectToAdd into next null position. */
public void add(Object objectToAdd){
Object obj = objectToAdd;
if (this.isFull()){
this.expandObjectArray();
this.objectArray[this.size()] = obj;
}
else{
this.objectArray[this.size()] = obj;
}
}
/*public void add(int index, Object objectToAdd)
*adds object at specified index. If array is full, expand() is used */
public void add(int index, Object objectToAdd){
Object obj = objectToAdd;
if (this.indexIsOutOfBounds(index)){
this.expandObjectArrayToIndexOutOfBounds(index);
this.objectArray[index] = obj;
}
else if (this.isFull()){
this.expandObjectArray();
this.shiftRightFromIndex(index);
this.objectArray[index] = obj;
}
else{
this.shiftRightFromIndex(index);
this.objectArray[index] = obj;
}
}
/*
* REMOVE BY OBJECT
* public void remove(Object objectToRemove) */
public void remove(Object objectToRemove){
int indexOfObject = this.find(objectToRemove);
if (indexOfObject == -1){
System.out.println("Object Not Found in Array.");
}
else {
this.remove(indexOfObject);
}
}
/* REMOVE BY INDEX*/
public void remove(int index){
if (index > this.getObjectArrayLength()){
System.out.println("Index Specified is Out of Bounds. Does Not Exist in Array");
}
else{
this.objectArray[index] = null;
this.shiftLeftFromIndex(index);
}
}
/*public Object get(int index)*/
public Object get(int index){
return this.objectArray[index];
}
/*public int size() */
public int size(){
int arraySize = 0; // counter for objects in array
for (int i = 0; i < this.objectArray.length; i++){
if (this.objectArray[i] != null){ // if current array index is NOT null, arraySize++
arraySize++;
}
}
return arraySize; // returns int of current arrays size()
}
/* public boolean isEmpty()
* uses size() method to return an int that gives amount of elements in array. */
public boolean isEmpty(){
int arraySize = this.size(); // uses this.size() to get arrays element count
if (arraySize > 0){
return false; // returns false if arraySize > 0
}
else{
return true; // if no elements counted, only null indices, returns true
}
}
/*
* public boolean isFull()
* takes arrays length and subtracts it by arrays size.*/
public boolean isFull(){
if (this.objectArray.length - this.size() == 0){
return true;
}
else {
return false;
}
}
/* public boolean isIn(Object objectToFind)takes an Object as an argument*/
public boolean isIn(Object objectToFind){
Object objToFind = objectToFind;
for (int i = 0; i < this.objectArray.length; i++){
if (this.objectArray[i].equals(objToFind)){
return true;
}
}
return false;
}
/*
* public boolean indexIsOutOfBounds() checks to see if index that is adding at exists or not*/
public boolean indexIsOutOfBounds(int indexToCheck){
if (indexToCheck > this.getObjectArrayLength()){
return true;
}
else{
return false;
}
}
/*
* public int find(Object objectToFind)
* Takes an object, iterates over the objectArray comparing against the contents of array. */
public int find(Object objectToFind){
Object objToFind = objectToFind;
for (int i = 0; i < this.objectArray.length; i++){
if (objToFind.equals(this.objectArray[i])){
return i; // if object .equals, returns current index, i
}
}
return -1;
}
/*public int getObjectArrayLength()
* this returns the value of the length of the current objectArray.*/
public int getObjectArrayLength(){
return this.objectArray.length;
}
/*public int getTempArrayLength()
* this returns the value of the length of the current tempArray.*/
public int getTempArrayLength(){
return this.tempArray.length;
}
/* public void expandObjectArray() default constructor. takes current length of working array and doubles it.*/
public void expandObjectArray(){ // default expansion. doubles length based on current length.
int newArrayLength = this.objectArray.length*2; // takes size of current array and doubles by 2
this.tempArray = this.objectArray.clone();
this.objectArray = new Object[newArrayLength];
for (int i = 0; i < this.tempArray.length; i++){
this.objectArray[i] = this.tempArray[i];
}
this.tempArray = this.objectArray.clone(); // copies updated object array into temp
// for future use
}
/* public void expandObjectArrayToIndexOutOfBounds(int indexToExpandTo)
* when given an index that exists outside of current array capacity, */
public void expandObjectArrayToIndexOutOfBounds(int indexToExpandTo){
this.tempArray = this.objectArray.clone();
this.objectArray = new Object[indexToExpandTo+1];
for (int i = 0; i < this.tempArray.length; i++){
this.objectArray[i] = this.tempArray[i];
}
this.tempArray = this.objectArray.clone();
}
/*
* public void shiftRightFromIndex()
* this method takes contents of array from given index, and shifts
* all remaining objects from index one space to the right.*/
public void shiftRightFromIndex(int index){
this.expandObjectArray(); // expands Array and copys contents to a temp array
this.objectArray[index] = null; // sets index at shift point to null
for (int i = index+1; i < this.getTempArrayLength()-index; i++){
this.objectArray[i] = this.tempArray[i-1];
}
}
/*
* public void shiftLeftFromIndex(int index)
*shifts Objects in array from a given index to the left by one space
* implemented within the remove() method */
public void shiftLeftFromIndex(int index){
this.tempArray = this.objectArray.clone();
for (int i = index; i < this.getObjectArrayLength()-1; i++){
this.objectArray[i] = this.objectArray[i+1];
}
}
/*shuffles objects in array to a random order using the Fisher-Yates algorithm*/
public void shuffleArray(Object[] objectArray){
int objectArrayLength = objectArray.length;
for (int i = 0; i < objectArray.length; i++){
int random = i + (int) (Math.random() * (objectArrayLength-i));
Object randObject = objectArray[random];
objectArray[random] = objectArray[i];
objectArray[i] = randObject;
}
}
/*returns objectArray from working ArrayList*/
public Object[] getObjectArray(){
return this.objectArray;
}
}
Card.java
// the Card class is a basic playing card used in the ArrayList data structure
public class Card {
final private int value; // 2-10 and 11-14 for J(11), Q(12), K(13), A(14)
final private String suit; // Hearts, Diamonds, Spades, Clubs
Card(int value, String suit){
this.value = value;
this.suit = suit;
}
public void printCard(){
System.out.println(this.value + " of " + this.suit);
}
// getters (no setters, as only one of each card should exist)
public int getValue(){
return this.value;
}
public String getSuit(){
return this.suit;
}
}
Sample Output
Cards in ArrayList, PRE-SHUFFLE:
2 of Clubs
3 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.