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

Please answer these problem and thank you! PROBLEM 1 - Arrays (Note: There are t

ID: 3709994 • Letter: P

Question

Please answer these problem and thank you!

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 . · » » »

Explanation / Answer

Screenshot

--------------------------------------------------------------------------------------

Program

//Packages
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<toss;i++) {
           double t=0+(double) (Math.random()*(1-0));
           if(t<0.5) {
               tossArray[i]='h';
               fractionH+=1;
               fractionHCount+=1;
           }
           else {
               tossArray[i]='t';
               fractionT+=1;
               fractionTCount+=1;
           }
       }
       System.out.println("Farction of Heads: "+(fractionH/fractionHCount));
       System.out.println("Farction of Tails: "+(fractionT/fractionTCount));
       return tossArray;
   }
   //Longest run finding method
public int longestRun() {
  
   for(int i=0;i<toss;i++) {
           if (tossArray[i]=='h'&& tossArray[i+1]=='h') {
               countH+=1;
               if (maxH<countH) {
                   maxH=countH;
               }
           }
           else if(tossArray[i]=='t'&& tossArray[i+1]=='t'){
              
               countT+=1;
               if (maxT<countT) {
                   maxT=countT;
               }
           }
       }
       if(maxT<maxH) {
           return maxH;
       }
       else {
           return maxT;
       }
   }
//Tosrting method to print value
public String toString() {
   if(longestRun()==maxT) {
       h="Head";
   }
   else {
       h="Tail";
   }
   return "Number of coins tosses: "+cointossed()+" Longest run is "+longestRun()+" "+h;
}
}
//Main class
public class TestCoinToss {
//Main method
   public static void main(String[] args) {
       //Read input
       Scanner sc=new Scanner(System.in);
       //User prompt
       System.out.println("Enter Integer Number(>=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);
      
      
   }

}