Suppose we want to add a flip method to the Chance class Thi

Suppose we want to add a flip() method to the Chance class. This method should return, at random, either a 0 or a 1, where 0 stands for tails and 1 stands for heads. In the box provided below, code this method. Be sure to take advantage of the fact that Chance extends Random. Hint: what Random class method call will produce values of either 0 or 1? import java.util.Random; public class Chance extends Random { public int flip() {

Solution

import java.util.Random

{

public class Chance extends Random

{

public int flip()

{

Random rand=new Random();

double fl=2*rand.nextDouble();

return (int)fl;

}

}

Suppose we want to add a flip() method to the Chance class. This method should return, at random, either a 0 or a 1, where 0 stands for tails and 1 stands for h

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site