Question

need help answering this with java Write and test a class that contains an array as...

need help answering this with java

Write and test a class that contains an array as an attribute.

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. Write a FlipStats class with the following methods:

Design (what are attributes and methods needed, constants, static attributes/methods), code, and test your FlipStats class. See the test program (with method names expected)

import java.util.Random;
public class FlipStatsTest {
  public static void main( String [] args ) {
          int [] flips= new int[100];
          Random rand = new Random();
          for (int i=0; i<flips.length; i++){
                  flips[i]=rand.nextInt(2);
                  System.out.print(flips[i]+" ");
          }
          System.out.println();
          FlipStats analyzer = new FlipStats(flips);
          System.out.println(analyzer.firstRun(0,3));
          System.out.println(analyzer.firstRun(1,5));
  }
}

Sample Output:
1 1 1 0 0 1 1 0 1 0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 0 1 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 0 0 0 1 1 1 0 0 1 0 0 0 0 1 1 0 1 1 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1 0 0 1 1 0 1 0 1 0 0 0
9
12

  • A non-default constructor that performs a deep copy from its single argument, an array of integers,
  • 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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: I am provided FlipStats.java program only because not modification done in the "FlipStatsTest .java" program.

Explanation : If you want to find the string with exact length with next flip is same or not , it returns that index

For example:

flip array = 1100001111001

firstRun(0,3) = 2

firstRun(1,3) = 7

Screenshot of the program:

Sample output:

0 1 0 0 1 1 1 1 1 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 0 0 0 1 0 0 0 1 0 0 1 1 1 0 0 1 1 0 1 1 0 0 1 1 1 1 0 0 1 0 1 0 1 0 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 1 1 0 0 1 0 1 1 0 1 0 1 0 0 0 1

14

4

Code to copy:


public class FlipStats
{
//0 for heads, 1 for tails.
int[] flips;
int size;
/*A non-default constructor */
public FlipStats(int[] flips)
{//deep copy of the flips
  this.flips = flips;
  size = flips.length;
}
/*"firstRun" method that takes 2 arguments,
* an integer denoting what side of the coin,
* and an integer denoting what exact length
*/
public int firstRun(int flip, int n)
{
  //check entire flips array to find flip with length n
  for(int i=0;i<size-n;i++)
  {
   boolean found = true;
   for(int j=i,k=0;k<n;j++,k++)
   {
    //check string of length is existing or not
    if(flip != flips[j])
    {     
     found = false;
     break;
    }
   }
   //if found is true than return index i
   if(found == true)
    return i;
  }
  //if not found string return false
  return -1;
}

}

Note: Provided answer as per the question above.But if you want to find the string with exact length than replace this below method "firstRun()" in the "FlipStats.java" program.

For example:

1) flip array = 11000011001

firstRun(0,3) = -1, //here heads of length 3 is not found than return -1,

2) flip array = 110000110001

firstRun(0,3) = 8,//here heads of length 3 is found at 8th location than return 8.

Screenshot of the Method:

Add a comment
Know the answer?
Add Answer to:
need help answering this with java Write and test a class that contains an array as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • In JAVA Thank You What is the exact output produced by running the method test? /**...

    In JAVA Thank You What is the exact output produced by running the method test? /** * TestSwap.java * Demonstrates parameter passing involving arrays and integers, * scope of variables, flow of control, and overloaded methods. */ public class TestSwap { public void swap (int x, int y) { int temp; temp = x; x = y; y = temp; System.out.println("Inside swap version 1:"); System.out.println("x = " + x); System.out.println("y = " + y); } public void swap (int[] a,...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • In class we wrote a method closestPairFast that on an input array of numbers, finds the...

    In class we wrote a method closestPairFast that on an input array of numbers, finds the distance of the closest pair by first sorting the input array and then finding the closest adjacent pair. (See the file ClosestPair1D.java in the Code folder on D2L.) In this problem, you are asked to modify the method so that it returns an integer array consisting of the indices of the closest pair in the original array. If there is a tie, just return...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT