Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task Inside src / main / java / random , in Game.java there is an unimplemented function named battle. This function should return true if

Task
Inside src/main/java/random, in Game.java there is an unimplemented function named battle. This function should return true if the hero wins the battle and false if they lose. The chance of the hero winning is 0.5.
There are two constructors for the class; one for testing where the random attribute is seeded with the given value, and a default constructor which uses the current time as the seed. The default constructor is for real usage (for example, the main method we have provided).
When the Random object is constructed with a seed of 4, the following values are the results first 8 calls to .nextInt(100):
62523586751146
For -4:
391398543892023
For 0:
6048294715539161
Write at least 2 unit tests for battle inside GameTest.java using seeds.
Once you have done this, implement the function.
How would you write tests for Game with the default constructor that prove the battle function works as expected? Think about this and be prepared to answer during marking. Write your answer down in your blog postpackage random;
import java.util.Random;
/**
* A simple game, where a hero engages in battles. The hero has an equally
* likely chance of succeeding as of failing.
*
* @author Nick Patrikeos + @your name
*/
public class Game {
private Random random;
public Game(long seed){
random = new Random(seed);
}
public Game(){
this(System.currentTimeMillis());
}
public boolean battle(){
// TODO
int randomNumber = random.nextInt(100);
return randomNumber <50;
}
public static void main(String[] args){
Game g = new Game();
for (int i =0; i <100; i++){
if (g.battle()){
System.out.println("We won!! You are awesome!!");
} else {
System.out.println("Lost :(");
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions