Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribedimage text in transcribed

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=2 for coin tosses or 0 to exit: " ); int num=sc.nextInt(); //Continue until enter 0 while(num>=2) { //Object ceation CoinToss ct=new CoinToss(); //valu passing ct.setToss(num); ct.setArray(num); //Print System.out.println(ct.toString()); //Repeat user prompt System.out.println("Enter Integer Number(>=2 for coin tosses or 0 to exit: " ); num=sc.nextInt(); } //Enter 0 exit System.out.println("Bye !!!"); System.exit(0); }

}

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 that

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions

Question

10. Are you a. a leader? b. a follower? _______

Answered: 1 week ago