This is the CoinTester.java code which does not need modified. This is the start
ID: 3862522 • Letter: T
Question
This is the CoinTester.java code which does not need modified. This is the starting file Coin.java that we need to write the code for in order to produce the exact output as above! Hope this helps and thanks for helping! Reverse Engineering a Class You are given the main program and the output produced by it. You must write the Coin class, a file named Coin java which will satisfy the class definition required by CoinTester java. Be sure to write your coin.java in the same directory with CoinTester.java Your leader will explain the task and begin suggesting what you do each step of the way by looking at each call made by the tester program and seeing exactly what that call returns. Your leader will then ask you to write that constructor or method in the Coin java file. Your leader will walk around and give as much help as needed for you to get it working. Just do it one step at a time. Write ONE method at a time then compile Coin java and test with the Tester. Look at the main and see how the Coin class is constructed and what methods are called. From those observations, you can deduce the private data members and public methods with their exact naming and data type. Note that when a coin object is created, it's internal head and tail counts are initialized to zero. Each time a coin is.flip()'d that flip increments that coin's heads or tails counts with a 50% each probability. Thus, you must use a Random variable inside your coin class to get a 50/50 chance of getting a head or tail. Once the flip method decided that a head or tail was rolled, the flip method increments the appropriate counter and then returns "H" or "T". The reset() method starts that coin's counters back at 0. CoinTester java & Coin.javaExplanation / Answer
import java.util.Random;
public class Coin {
static int heads;
static int tails;
int seed=0;
public Coin()
{
heads=0;
tails=0;
}
public Coin(int seed)
{
heads=0;
tails=0;
this.seed=seed;
}
public void resert()
{
heads=0;
tails=0;
}
public String flip()
{
Random ran=new Random(seed);
String s=null;
int num=1 + ran.nextInt()%2;
if(num==1)
{
s="H";
heads++;
}
else
{
s="T";
tails++;
}
return s;
}
public int getNumHeads()
{
return heads;
}
public int getNumTails()
{
return tails;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.