Question
Need help with adjusting to match the output in java coding. Thank you! import java.io.*; import java.util.*; //Class cointoss class CoinToss{ //Variable for calculation int
Need help with adjusting to match the output in java coding. Thank you!
import java.io.*; import java.util.*; //Class cointoss class CoinToss{ //Variable for calculation int fractionH=0,fractionT=0,fractionHCount=0,fractionTCount=0; int maxH=0,maxT=0,countH=1,countT=1; int toss; String h=""; char tossArray[]; //Default constructor CoinToss(){ } //Parameterised constructor CoinToss(int toss){ this.toss=toss; } //Mutators public void setToss(int toss) { this.toss=toss; } public void setArray(int toss) { this.tossArray=new char[toss];; } //accessor public int getToss() { return this.toss; } public char[] getArray() { return this.tossArray; } //Get the toss count public int cointossed() { return toss; } //Array setting public char[] tossArrayCreation(){ for(int i=0;i
}
PROBLEM 1 - Arrays (Note: There are two separate problems in this HW.) Write a Java application to simulate coin tosses and compute the fraction of heads and fraction of tails. As a challenge, determine the longest run of one outcome; that is, the most consecutive heads or consecutive tails (which ever is longer) To simulate the toss of a coin, use the random method in the Math utility class, that generates a number greater than or equal to 0 and less than 1 . O. The notation for this range is [0 , 1.0), where the square bracket means to include the 0 in the range and the rounded parenthesis means to exclude the 1.0 from the range. Since there are two outcomes of equal probability in the toss of a "fair" coin, divide the range of numbers between [0 ,1.0) into two roughly equal size sub-ranges. Assume any number less than 0.5 will be considered as a tail while any number greater than or equal to 0.5 as a head Program Organization Requirements: CoinToss.iava A parameterized constructor that accepts an integer parameter for the number of tosses. This value will be used to set the size of an array that will hold the coin toss results. A loop is used to fill an array with the characters 'h' (head) and t' (tail). As described above, use a random number to determine if each "coin toss" is a head or a tail. This array initialization is accomplished in the constructor (HINT you can keep a count of the number of heads and tails as attributes as you fill the array) Create methods that return the number of heads and the number of tails in the coin toss array (these are accessors) Create a method that returns the total number of coin tosses. Create a method that computes the longest (consecutive) number of heads or tails in the coin tosses array. This method stores this information in two attributes: one for the length of the longest run, and one for the outcome of thatStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started