You are given the main program and the output produced by it. You must write the
ID: 3862194 • Letter: Y
Question
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.
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.
FY18 Allocations Co FY18 Allocations Co FY18 Allocations. Con X C people.cs.pitt.edu/~hoffmant/S17-401/ CS 101 17 publio statio void tting aeg 8ysters print systees. printin ooin2.Elip Ngual ohanou of head. systers printin Equal printin Noti the tester progra rdcoded seed of 7 and the hich do ay get diff grading Desktop lab-ne-Ca CoinTeste lipping Coin 20 times TH THH lipping Coin2 10 times Ask me anything ectively. As off man) C chegg study IGuide x x Y GO ng a coin class fo x uld give the if yo utput every pt grad tests the wrote it 79% ing very old of the rectly yo 3:29 P 3/15/2017Explanation / Answer
import java.util.Random;
/**
*
* @author Namburi Ramesh
*/
public class Coin {
private int headcount,tailcount; //variables to store heads and tails count
private Random r; //Random class to generate uniformly distributed integers between 0 and 1
public Coin(int seed) {
r=new Random(seed); //initialise Random object with the seed passed in the constructor
//initialise headcount and tailcount to zero
headcount=0;
tailcount=0;
}
public char flip(){
int a=r.nextInt(2);//generate one random int between 0 and 1
//if generated random number is 0 then return Heads else return Tails
if(a==0){
headcount++;
return 'H';
}else if(a==1){
tailcount++;
return 'T';
}
return 0;
}
//this method returns heads count
public int getNumHeads(){
return headcount;
}
//this method returns tails count
public int getNumTails(){
return tailcount;
}
//This method resets counters to zero
public void reset(){
headcount=0;
tailcount=0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.