How would I implement this? I would appreciate any help on this. public class Ba
ID: 3575530 • Letter: H
Question
How would I implement this? I would appreciate any help on this.
public class BattleDeck {
private ArrayList 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() {
throw new RuntimeException("You must implement this!");
}
/**
* 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) {
throw new RuntimeException("You must implement this!");
}
/**
* 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!");
}
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.