I need help implementing the following methods in bold. Any help would be apprec
ID: 3575526 • Letter: I
Question
I need help implementing the following methods in bold. Any help would be appreciated!
package studentCode;
import java.util.ArrayList;
public class BattleDeck<T extends Battleable> {
private ArrayList<Battleable> arr;
//You might end up adding more instance fields...
/**
* The method will return a ragged 2D structure using the Java array.
* It will have references to copies of the creatures currently stored
* in the battle deck.
*
* The 2D structure will have one row for each valid skill level
* (0 through 9). Within each row the order will be based on the
* "front to back" order of them in the deck's single-dimensional
* structure.
*
* NOTE: To build the ragged 2D structure, you'll need to read
* through the list of creatures once to determine how big each
* row will need to be and then another time to populate the
* ragged structure with the references to the copies of the
* creatures.
*
* @return reference to a ragged 2D structure using the java array
*/
public Battleable[][] export2Darray() {
//returns a two-dimensional ragged array where each row
// contains a deep copy of all of the Battleable objects
// in the BattleStack with the corresponding Level value
Battleable[][] retVal = new Battleable[10][];
return retVal;
}
/**
* The method will return a ragged 2D structure using the ArrayList
* data type - it will have references to copies of the creatures
* currently stored in the battle deck.
*
* The 2D structure will have one row for each valid skill level
* (0 through 9). Within each row the order will be based on the
* "front to back" order of them in the deck's single-dimensional
* structure.
*
* NOTE: To build this ragged 2D structure, you should only need
* to go through the deck once!
*
* @return reference to a ragged 2D structure using ArrayLists
*/
public ArrayList<ArrayList<Battleable>> export2Darraylist() {
ArrayList<ArrayList<Battleable>> retVal =
new ArrayList<ArrayList<Battleable>>();
//YOUR CODE GOES HERE
return retVal;
}
This class implements an interface called Battleable:
public interface Battleable {
/**
* Getter for the skill level of the creature.
* @return skill level of the creature
*/
public int getLevel();
/**
* Getter for the name of the creature.
* @return name of the creature
*/
public String getName();
/**
* Getter for the strength of the creature.
* @return strength of the item
*/
public int getStrength();
/**
* Setter for the strength of the creature.
* @param newStrength the new strength value for the creature
*/
public void setStrength(int newStrength);
/**
* Method to create an independent copy of the creature.
* @return independent copy of the creature
*/
public Battleable returnDuplicate();
/**
* Method that takes the outcome of a battle and increments the level
* if the number of victories has passed the threshold.
* @param outcome the outcome of the battle in which this creature was
* involved
* @return true if the outcome conveyed caused a level-up
*/
public boolean inform(Universe.Outcomes outcome);
/**
* String generator for the creature.
* @return String representing the creature
*/
public String toString();
}
Thank you!
Explanation / Answer
package studentCode;
import java.util.ArrayList;
public class BattleDeck<T extends Battleable> {
private ArrayList<Battleable> arr;
// You might end up adding more instance fields...
/**
* The method will return a ragged 2D structure using the Java array.
* It will have references to copies of the creatures currently stored
* in the battle deck.
*
* The 2D structure will have one row for each valid skill level
* (0 through 9). Within each row the order will be based on the
* "front to back" order of them in the deck's single-dimensional
* structure.
*
* NOTE: To build the ragged 2D structure, you'll need to read
* through the list of creatures once to determine how big each
* row will need to be and then another time to populate the
* ragged structure with the references to the copies of the
* creatures.
*
* @return reference to a ragged 2D structure using the java array
*/
public Battleable[][] export2Darray() {
// returns a two-dimensional ragged array where each row
// contains a deep copy of all of the Battleable objects
// in the BattleStack with the corresponding Level value
Battleable[][] retVal = new Battleable[10][];
int countCreaturesPerLevel[] = new int[10];
for (Battleable creature : arr) {
countCreaturesPerLevel[creature.getLevel()]++;
}
for (int index=0; index < 10; index++) {
retVal[index] = new Battleable[countCreaturesPerLevel[index]];
}
int filledUpCount[] = new int[10];
for (Battleable creature : arr) {
int level = creature.getLevel();
retVal[level][filledUpCount[level]++] = creature.returnDuplicate();
}
return retVal;
}
/**
* The method will return a ragged 2D structure using the ArrayList
* data type - it will have references to copies of the creatures
* currently stored in the battle deck.
*
* The 2D structure will have one row for each valid skill level
* (0 through 9). Within each row the order will be based on the
* "front to back" order of them in the deck's single-dimensional
* structure.
*
* NOTE: To build this ragged 2D structure, you should only need
* to go through the deck once!
*
* @return reference to a ragged 2D structure using ArrayLists
*/
public ArrayList<ArrayList<Battleable>> export2Darraylist() {
ArrayList<ArrayList<Battleable>> retVal = new ArrayList<ArrayList<Battleable>>();
for (Battleable creature : arr) {
ArrayList<Battleable> creaturesList = retVal.get(creature.getLevel());
if (creaturesList == null) {
creaturesList = new ArrayList<Battleable>();
retVal.add(creature.getLevel(), creaturesList);
}
creaturesList.add(creature.returnDuplicate());
}
return retVal;
}
}
public interface Battleable {
/**
* Getter for the skill level of the creature.
* @return skill level of the creature
*/
public int getLevel();
/**
* Getter for the name of the creature.
* @return name of the creature
*/
public String getName();
/**
* Getter for the strength of the creature.
* @return strength of the item
*/
public int getStrength();
/**
* Setter for the strength of the creature.
* @param newStrength the new strength value for the creature
*/
public void setStrength(int newStrength);
/**
* Method to create an independent copy of the creature.
* @return independent copy of the creature
*/
public Battleable returnDuplicate();
/**
* Method that takes the outcome of a battle and increments the level
* if the number of victories has passed the threshold.
* @param outcome the outcome of the battle in which this creature was
* involved
* @return true if the outcome conveyed caused a level-up
*/
public boolean inform(Universe.Outcomes outcome);
/**
* String generator for the creature.
* @return String representing the creature
*/
public String toString();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.