Please help me with this question. Im not sure how to implement the inform and r
ID: 3575532 • Letter: P
Question
Please help me with this question.
Im not sure how to implement the inform and returnDuplicate methods.
package studentCode;
public class BattleCow implements Battleable {
private String name;
private int strength;
private int levelUps;
private int victoriesSinceLevelUp;
/**
* Method that takes the outcome of a battle and increments the level
* if the number of victories has passed the threshold or sets the
* strength to 0 if the outcome was a loss. We define "passed the
* threshold" as meaning the victories equals or exceeds the threshold.
* Also, note that the drop in strength level is not true for all
* things that implement Battleable.
* @param outcome the outcome of the battle in which this BattleCow was
* involved
* @return true if the outcome conveyed caused a level-up
*/
public boolean inform(Universe.Outcomes outcome) {
throw new RuntimeException("You must implement this!");
}
/**
* Method to create an independent copy of the BattleCow.
* @return independent copy of the BattleCow
*/
public Battleable returnDuplicate() {
throw new RuntimeException("You must implement this!");
}
Within a seperate class, there are three outcomes: {WIN, LOSE, DRAW} and the threshold = 17.
public class Universe {
public static enum Outcomes { WIN, LOSE, DRAW }
public static int THRESHOLD = 17;
public static Battleable fight(Battleable player1, Battleable player2) {
int result =
player1.getLevel()*player1.getStrength()
-
player2.getLevel()*player2.getStrength();
if (result < 0) {
player1.inform(Outcomes.LOSE);
player2.inform(Outcomes.WIN);
return player2;
}
if (result > 0) {
player1.inform(Outcomes.WIN);
player2.inform(Outcomes.LOSE);
return player1;
}
player2.inform(Outcomes.DRAW);
player2.inform(Outcomes.DRAW);
return null;
}
}
Thank you.
Explanation / Answer
//u didn't specify the interface Battleable therefore my solution is containing some error becoz Batteable is not defined
//Define the interface Battleable and code will work perfectly
package studentCode;
public class BattleCow implements Battleable
{
private String name;
private int strength;
private int levelUps;
private int victoriesSinceLevelUp;
/**
* Method that takes the outcome of a battle and increments the level
* if the number of victories has passed the threshold or sets the
* strength to 0 if the outcome was a loss. We define "passed the
* threshold" as meaning the victories equals or exceeds the threshold.
* Also, note that the drop in strength level is not true for all
* things that implement Battleable.
* @param outcome the outcome of the battle in which this BattleCow was
* involved
* @return true if the outcome conveyed caused a level-up
*/
public boolean inform(Universe.Outcomes outcome) //Outcomes is static so we access it by class name
{
//throw new RuntimeException("You must implement this!");
if(victoriesSinceLevelUp>Universe. THRESHOLD)
{
levelUps++;
}
if(outcome==Universe.Outcomes.LOSE)
{
strength=0;
}
}
/**
* Method to create an independent copy of the BattleCow.
* @return independent copy of the BattleCow
*/
public Battleable returnDuplicate()
{
//throw new RuntimeException("You must implement this!");
BattleCow c=new BattleCow();
return c;
}
}
//Within a seperate class, there are three outcomes: {WIN, LOSE, DRAW} and the threshold = 17.
public class Universe
{
public static enum Outcomes { WIN, LOSE, DRAW }
public static int THRESHOLD = 17;
public static Battleable fight(Battleable player1, Battleable player2)
{
int result =
player1.getLevel()*player1.getStrength()
-
player2.getLevel()*player2.getStrength();
if (result < 0) {
player1.inform(Outcomes.LOSE);
player2.inform(Outcomes.WIN);
return player2;
}
if (result > 0) {
player1.inform(Outcomes.WIN);
player2.inform(Outcomes.LOSE);
return player1;
}
player2.inform(Outcomes.DRAW);
player2.inform(Outcomes.DRAW);
return null;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.