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

JAVA PLZ OVERVIEW This program primarily focuses on the implementation of a Arra

ID: 3842239 • Letter: J

Question

JAVA PLZ

OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the necessary methods to implement the ArrayList. It also includes polymorphism and class comparison. INSTRUCTIONS Your deliverable will be to turn in three files. The files will be named Card.java, PremiumCard.java and the last file will be called CardArrayList.java. You will need support files CardSorter.java from the course web site. For this assignment, any use of a data control structure other than a simple Arrary or String will result in no credit. I am aware that this assignment could be done quite simply by the implementation of an ArrayList but the point of the assignment is to do it without the standard implementation to get a feel for how they work "under the hood" COLLECTABLE CARD While the primary goal of this assignment will be the implementation and use of a custom ArrayList. The particular implementation we will use will focus on the idea of a simple collectable card game. For our card game, we will assume that every card has two values expressed as integers. Each card will have power and a toughness rating. These numbers need to be a minimum of 1 and a maximum of 1000. Cards then have a calculated cost attribute that comes from the formula below. cost IV1.5 power t 0.9 toughness Rounded up to the nearest integer.

Explanation / Answer

Below is the code for all 3 class with proper comments. I modified shuffle method in CardsArrayList.java class since it is better way to do it. please rate my answer

Card.java

package cards;

import java.util.Random;

/**
* Card class
*
*/
public class Card {

   private int power;
   private int toughness;

   /**
   * Empty Constructor
   */
   public Card() {
       power = new Random().nextInt(1000);
       toughness = new Random().nextInt(1000);
   }

   /**
   * Constructor takes in value and create new card
   *
   * @param x
   * @throws Exception
   */
   public Card(int x) throws Exception {
       if (x <= 1 && x >= 1000) {
           throw new Exception("power/toughness is out of bounds 1 and 1000");
       } else {
           power = x;
           toughness = x;
       }
   }

   /**
   * Constructor takes in value for power and toughness and create new card
   *
   * @param x
   * @throws Exception
   */
   public Card(int p, int t) throws Exception {
       if ((p <= 1 && p >= 1000) || (t <= 1 && t >= 1000)) {
           throw new Exception("power/toughness is out of bounds 1 and 1000");
       } else {
           power = p;
           toughness = t;
       }
   }

   /**
   * method returns power of card
   *
   * @return power
   */
   public int getPower() {
       return power;
   }

   /**
   * method returns toughness of card
   *
   * @return toughness
   */
   public int getToughness() {
       return toughness;
   }

   /**
   * method returns cost of card
   *
   * @return cost
   */
   public int getCost() {
       return (int) Math.sqrt((1.5 * power) + (0.9 * toughness));
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   public String toString() {
       return "[" + power + "/" + toughness + "]";
   }

   /**
   * method reduces power and toughness of card by ten percent
   */
   public void weaken() {
       power = (int) (power - power * 0.1);
       toughness = (int) (toughness - toughness * 0.1);

   }

   /**
   * method increases power and toughness of card by ten percent
   */
   public void boost() {
       power = (int) (power + power * 0.1);
       toughness = (int) (toughness + toughness * 0.1);
   }

}

PremiumCard.java

package cards;

import java.util.Random;
/**
*
* Premium card class
* @author
*
*/
public class PremiumCard extends Card {

   private int power;
   private int toughness;

   /**
   * Empty Constructor
   */
   public PremiumCard() {
       super();
       power = new Random().nextInt(1000);
       toughness = new Random().nextInt(1000);
   }

   /**
   * Constructor takes in value and create new card
   *
   * @param x
   * @throws Exception
   */
   public PremiumCard(int x) throws Exception {
       super(x);
       if (x <= 1 && x >= 1000) {
           throw new Exception("power/toughness is out of bounds 1 and 1000");
       } else {
           power = x;
           toughness = x;
       }
   }

   /**
   * Constructor takes in value for power and toughness and create new card
   *
   * @param x
   * @throws Exception
   */
   public PremiumCard(int p, int t) throws Exception {
       super(p, t);
       if ((p <= 1 && p >= 1000) || (t <= 1 && t >= 1000)) {
           throw new Exception("power/toughness is out of bounds 1 and 1000");
       } else {
           power = p;
           toughness = t;
       }
   }

   /**
   * method returns power of card
   *
   * @return power
   */
   public int getPower() {
       return power;
   }

   /**
   * method returns toughness of card
   *
   * @return toughness
   */
   public int getToughness() {
       return toughness;
   }

   /**
   * method returns cost of card
   *
   * @return cost
   */
   public int getCost() {
       return (int) Math.sqrt((1.5 * power) + (0.9 * toughness));
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   public String toString() {
       return "{{" + power + "/" + toughness + "}}";
   }

   /**
   * method reduces power and toughness of card by ten percent
   */
   public void weaken() {
       power = (int) (power - power * 0.1);
       toughness = (int) (toughness - toughness * 0.1);

   }

   /**
   * method increases power and toughness of card by ten percent
   */
   public void boost() {
       power = (int) (power + power * 0.1);
       toughness = (int) (toughness + toughness * 0.1);
   }
}

CardsArrayList.java

package cards;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Cards ArrayList class
*
* @author
*
*/
public class CardArrayList {

   private int sizeCounter;
   private Card[] cards;

   /**
   * Default constructor
   */
   public CardArrayList() {
       cards = new Card[10];
       sizeCounter = 0;
   }

   /**
   * Constructor that takes in a value and create a new cards array of same
   * size
   *
   * @param x
   */
   public CardArrayList(int x) {
       if (x < 0) {
           System.out.println("Error");
       } else {
           cards = new Card[x];
           sizeCounter = 0;
       }
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   *
   * method returns string
   */
   public String toString() {
       StringBuilder builder = new StringBuilder();
       builder.append("[0");
       int length = cards.length;
       for (int i = 0; i < length; i++) {
           if (cards[i] != null) {
               builder.append(cards[i].toString());
               builder.append(",");
           }
       }
       builder.deleteCharAt(builder.length() - 1);
       builder.append(":" + length);

       return builder.toString();
   }

   /**
   * return current number of elements of arraylist being stored
   *
   * @return
   */
   public int size() {
       int length = cards.length;
       int filled = 0;
       for (int i = 0; i < length; i++) {
           if (cards[i] != null) {
               filled = filled + 1;
           }
       }

       return filled;
   }

   /**
   * Adds card provided to array list in last empty location and increments
   * size counter
   *
   * @param x
   */
   public void add(Card x) {
       int length = cards.length;
       for (int i = 0; i < length; i++) {
           if (cards[i] == null) {
               cards[i] = x;
               break;
           }
       }
       sizeCounter++;
   }

   /**
   * Returns last element of array list
   *
   * @return
   */
   public Card remove() {
       int temp = sizeCounter;
       sizeCounter = sizeCounter - 1;
       return cards[temp - 1];
   }

   /**
   * Returns card located at location x in the array
   *
   * @param x
   * @return
   */
   public Card get(int x) {
       if (x > cards.length - 1) {
           System.out.println("Invalid Location");
           return null;
       } else {
           return cards[x];
       }
   }

   /**
   * Returns index of given card in the array
   *
   * @param x
   * @return
   */
   public int indexOf(Card x) {
       for (int i = 0; i < cards.length; i++) {
           if (x.toString() == cards[i].toString()) {
               return i;
           }
       }

       return -1;
   }

   /**
   * Adds card to specified location in the array,shifts all cards to right by
   * one location if given location is in middle
   *
   * @param l
   * @param x
   */
   public void add(int l, Card x) {
       if (isRoom() == false) {
           expand();
       }
       for (int i = 0; i < cards.length; i++) {
           if (i == l) {
               for (int j = i; j < cards.length; j++) {
                   cards[j + 1] = cards[j];
               }

               cards[i] = x;
               sizeCounter++;
           }
       }
   }

   /**
   * Removes card to specified location in the array,shifts all cards to left
   * by one location if given location is in middle
   *
   * @param l
   * @param x
   */
   public Card remove(int j) {
       Card card = null;
       for (int i = 0; i < cards.length; i++) {
           if (j == i) {
               card = cards[i];

               for (int k = i; k < cards.length; k++) {
                   cards[k] = cards[k + 1];
                   sizeCounter--;
               }
           }
       }

       return card;
   }

   /**
   * Sorts array elements in descending order
   */
   public void sort() {
       Arrays.sort(cards, Collections.reverseOrder());
   }

   /**
   * Shuffles elements in cards array
   */
   public void shuffle() {
       List<Card> list = Arrays.asList(cards);
       Collections.shuffle(list);
   }

   /**
   * Checks if there is space in array ,returns false if not
   *
   * @return
   */
   private boolean isRoom() {
       if (sizeCounter == cards.length) {
           return false;
       }
       return true;
   }

   /**
   * doubles the size of array
   */
   private void expand() {
       cards = Arrays.copyOf(cards, cards.length * 2);
   }

   /**
   * Swaps two elements of array
   *
   * @param a
   * @param b
   */
   private void swap(int a, int b) {
       Card temp = null;
       temp = cards[a];
       cards[a] = cards[b];
       cards[b] = temp;
   }

   public void clear() {
       cards = new Card[10];
   }

}