Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are given the main program and the output produced by it. You must write the

ID: 3655507 • Letter: Y

Question

You are given the main program and the output produced by it. You must write the Coin class definition 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. Declare your Random variable as a member of the class, not inside any method. Initialize and seed your random variable in your Constructor Write your Coin.java one method at a time and test each method independently. CoinTester.java starter file. /* CoinTester.java - tests the Coin class by constructing variables and calling it's methods */ public class CoinTester { public static void main( String args[] ) { Coin coin1 = new Coin(); Coin coin2 = new Coin(); // FLIP COIN1, PRINT RESULTS System.out.println(" Flipping Coin1 20 times."); for (int i=0 ; i<20 ; ++i) System.out.print( coin1.flip() + " " ); // Equal chance of head or tail System.out.println(); System.out.println("heads=" + coin1.getNumHeads() + ", tails=" + coin1.getNumTails() ); coin1.reset(); // sets numHeads and numTails back to zero; // FLIP COIN2, PRINT RESULTS System.out.println(" Flipping Coin2 10 times."); for (int i=0 ; i<10 ; ++i) System.out.print( coin2.flip() + " " ); // Equal chance of head or tail System.out.println(); System.out.println("heads=" + coin2.getNumHeads() + ", tails=" + coin2.getNumTails() ); coin2.reset(); // sets numHeads and numTails back to zero; // FLIP COIN2 AGAIN, PRINT RESULTS System.out.println(" Flipping Coin1 again 35 times."); for (int i=0 ; i<35 ; ++i) System.out.print( coin1.flip() + " " ); // Equal chance of head or tail System.out.println(); System.out.println("heads=" + coin1.getNumHeads() + ", tails=" + coin1.getNumTails() ); coin1.reset(); // sets numHeads and numTails back to zero; }// END main OUTPUT Here is the output of a sample run of CoinTester.java *YOUR OUTPUT MUST MATCH EXACTLY* Flipping Coin1 20 times. T T T T H H T T H T H T T H T H T T H T heads=7, tails=13 Flipping Coin2 10 times. T T T T H H T T H T heads=3, tails=7 Flipping Coin1 again 35 times. H H T H T T T H H H T H T H T H T T H H H H T T T T T T T T H H H H H heads=18, tails=17

Explanation / Answer

package com.cramster.nov5; import java.util.Random; public class Coin { private int heads; private int tails; private Random random; public Coin() { heads = 0; tails = 0; //Seeding Random with 50 random = new Random(50); } public void reset() { heads = 0; tails = 0; } public int getNumHeads() { return heads; } public int getNumTails() { return tails; } public String flip() { int chance = random.nextInt(2); //The above statement sees to it that chance has only 2 values 0 and 1 //If chance is zero then it's heads and if it's 1 it's tails. //This gives 50-50 probability if(chance == 0) { heads++; return "H"; } else { tails++; return "T"; } } } package com.cramster.nov5; public class CoinTester { public static void main( String args[] ) { Coin coin1 = new Coin(); Coin coin2 = new Coin(); // FLIP COIN1, PRINT RESULTS System.out.println(" Flipping Coin1 20 times."); for (int i=0 ; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote