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

How would I implement this? Any help would be appreciated. // I have a class cal

ID: 3575605 • Letter: H

Question

How would I implement this? Any help would be appreciated.

// I have a class called Battleable which is an interface which includes three getters, and one setter. They are getLevel(), getName(), getStrength(), setStrength(int new Strength), and toString().

Here is the method i need help with:

package studentCode;

import java.util.ArrayList;

public class BattleDeck<T extends Battleable> {
  

private ArrayList<Battleable> arr;
  
//You might end up adding more instance fields...

/**
   * Goes through each item in the BattleDeck and deducts the specified
   * loss value from every creature it contains.
   *
   * @param lossValue the value to deduct from the strength level
   */
   public void weakenCreatures(int lossValue) {
       throw new RuntimeException("You must implement this!");

/**

   * Removes any creature currently in the battle deck who have
   * no more strength left.
   */
   public void sweepDeck() {
      
       throw new RuntimeException("You must implement this!");

/**

   * Shuffles the contents of the deck in the way described here.
   * The deck will be divided into two "packets" (we will call them
   * the top half and the bottom half). The shuffled BattleDeck of
   * cards will consist of the first card from the top packet,
   * followed by the first card from the bottom packet, followed by
   * the second card from the top packet, followed by the second card
   * from the bottom packet, etc.
   *
   * Important: If there are an odd number of cards, the top packet
   * should have one more card than the bottom packet.
   *
   * Remember that the top of the deck is considered to be the front
   * of the ArrayList.
   *

public void shuffle() {
       throw new RuntimeException("You must implement this!");
   }

Thank you!

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.ArrayList;

public class BattleDeck<T extends Battleable> {

   private ArrayList<T> arr;

   /**

   * Standard constructor. It needs to initialize the ArrayList object

   * and do any other setup that you deem necessary for this class object.

   */

   public BattleDeck() {

       arr = new ArrayList<T>();

   }

   /**

   * Adds an item to the BattleDeck in a very special way.

   * The structure is double-ended; this means that the "side" to which

   * objects are added alternates with every other item added. If things

   * are added in the order 1,2,3,4,5 then the BattleDeck would grow as

   * the following:

   * 1

   * 1,2

   * 3,1,2

   * 3,1,2,4

   * 5,3,1,2,4

   * It is your job to determine a good way to ensure this alternation.

   *

   * @param newItem refers to a creature to be added to this BattleDeck

   */

   public void add(T newItem) {

       if(arr.size()%2 == 0){ // if size is even then add at begining

           arr.add(0, newItem);

       }else{ // add at end

           arr.add(newItem);

       }

   }

   /**

   * Goes through each item in the BattleDeck and deducts the specified

   * loss value from every creature it contains.

   *

   * @param lossValue the value to deduct from the strength level

   */

   public void weakenCreatures(int lossValue) {

       for(int i=0; i<arr.size(); i++){

           T item = arr.get(i);

           int oldStrength = item.getStrength();

           item.setStrength(oldStrength - lossValue);

           arr.set(i, item);

       }

   }

   /**

   * Removes any creature currently in the battle deck who have

   * no more strength left.

   */

   public void sweepDeck() {

       ArrayList<T> newList = new ArrayList<T>();

       for(int i=0; i<arr.size(); i++){

           if(arr.get(i).getStrength() > 0){

               newList.add(arr.get(i));

           }

       }

      

       arr = newList;

   }

  

   public void shuffle() {

int length = arr.size();

  

int middle = 0;

if(length%2 == 0) // if even number of elements

   middle = length/2-1;

else // odd number of elements

   middle = length/2;

// index starting for left half and right half

int leftStart = 0;

int rightStart = length-1;

  

ArrayList<T> newList = new ArrayList<T>();

  

       while(leftStart <= middle && rightStart >= (middle-1)){

          

           newList.add(arr.get(leftStart));

           newList.add(arr.get(rightStart));

           leftStart++;

           rightStart--;

       }

      

       if(leftStart < middle) // if odd number of elements

           newList.add(arr.get(middle));

      

       arr = newList;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote