Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Main.java import java.util.*; public class Main { public static void main( String [] args ) { Scanner in = new Scanner(System.in); int seed = in.nextInt();

image text in transcribed

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribedMain.java

import java.util.*; public class Main { public static void main( String [] args ) { Scanner in = new Scanner(System.in); int seed = in.nextInt(); int [] flips= new int[30]; Random rand = new Random(seed); for (int i=0; i

System.out.println(analyzer.longestRun(0)); System.out.println(analyzer.longestRun(1));

System.out.println(analyzer.averageRun(0)); System.out.println(analyzer.averageRun(1)); } }

FlipStats.java

public class FlipStats { private int [] data; private int [] runTrack;

public FlipStats(int [] d) { data = new int[d.length]; runTrack = new int[d.length]; for (int i=0;i

private void calculateRunTrack(){ // head(0) run is negative, tail(1) run is positive int k=0, side=data[0]; int currentRun=1; for (int i=1;i0) { if (side==0) runTrack[k]=-1*currentRun; else runTrack[k]=currentRun; k++; } currentRun=1; side=data[i]; } } if (side==0) runTrack[k]=-1*currentRun; else runTrack[k]=currentRun; int [] runTrackNew = new int[k+1]; for (int i=0;i

return 0; } public int longestRun(int side) { // using runTrack // head(0) run is negative, tail(1) run is positive

return 0; } public double averageRun(int side) { // using runTrack // head(0) run is negative, tail(1) run is positive

return 0; } }

Question 4 - Arrays - FlipStats Enhancement (10 points) We want to analyze a series of random coin flips represented by an array of integers, 0 for heads, 1 for tails. Specifically, we want to identify a "run", which is a consecutive sequence of the same result for the coin flip. Someone wrote a FlipStats class with the following methods: - A non-default constructor that performs a deep copy from its single argument, an array of integers. You can assume the argument is correct. - A "firstRun" method that takes 2 arguments, an integer denoting what side we are looking for, and an integer denoting what exact length run of that side we are looking for. The method should return the index position that the first run of that exact length starts or return 1 if not found. Sample Testing: What if we want to expand the functionality of our FlipStats class to answer the questions like the following? - What is the longest run? - What is the longest run of heads? Or tails? - What is the average run length? - What is the average run length of heads? Or tails? We could write methods for each of these individually. Or notice that we can process the original data once in the constructor to identify and store all the runs, and then just process that "runTrack" array, which is shorter than the original array, to answer questions about runs more easily. For example, if our input array is this 010001101011000 We could process it once and create this runTrack array which denotes head runs ( 0 s) with a negative number for the length of the run. And denotes tail runs (1s) with a positive number for the length of the run. 11-3 2 - 11 - 123 Then process that runTrack array to answer these questions: - Index location of first run of heads of length 2? -1, DOES NOT EXIST - Index location of first run of tails of length 2? 5 - What is the longest run? LENGTH 3 - What is the longest run of heads? LENGTH 3 Or tails? LENGTH 2 - What is the average run length? (1+1+3+2+1+1+1+2+3)/9=15/9=1.66 - What is the average run length of heads? (1+3+1+1+3)/5=9/5=1.8 Or tails? (1+2+1+2)/4=6/4=1.5 I updated the attributes and constructor and wrote the private method "calculateRunTrack" which walks the "data" array, keeping track of runs, and loads the length of each run into the "runTrack" private array attribute as discussed above. See FlipStats.java. You need to write these methods: - Rewrite the "public int firstRun(int side, int length)" method that processes the "runTrack" array instead of the "data" array. - Write a new method "public int longestRun(int side)" that return the length of the longest run of that side. - Write a new method "public double averageRun(int side)" that return the average length of the runs of that side. If no runs of that side, return 0 . Here is sample input/output from Main.java: 4 7 5 5 2.0 2.5714285714285716 123456789 110000101000000000010110010110 41111011221121 1 1 10 2 2. 857142857142857 1.4285714285714286 12121212 000100000100010011110000001101 315131246211 0 1 6 4 3.33333333333333351.666666666666667

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

Step: 3

blur-text-image

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

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions

Question

6. Suppose X is G(1,). Find g such that g(X)g() is AN(0, c).

Answered: 1 week ago

Question

is particularly relevant to these questions.)

Answered: 1 week ago