Question: Notes: Assume that the classes listed in the Java Quick Reference have been imported where appropriate. Unless otherwise noted in the question, assume that parameters

Notes:

  • Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
  • Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.
  • In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.

4. This question involves analyzing integer values that are obtained using the getInt method in the following TargetIntegers class. You will write one method in the class.

public class TargetIntegers

{

/** Returns an int from simulated user input */

public static int getInt()

{ /* implementation not shown */ }

/** Returns true if x is considered a target number; returns false otherwise

* Some target numbers are positive and some are negative.

*/

public static boolean isTarget(int x)

{ /* implementation not shown */ }

/** Analyzes sampleSize values obtained using the getInt method, as described

* in part (a)

* Precondition: sampleSize is a positive even integer.

*/

public static boolean runSimulation(int sampleSize)

{ /* to be implemented in part (a) */ }

// There may be variables and methods that are not shown.

}

(a) Some integer values are considered target numbers. A helper method, isTarget, has been provided. Method isTarget returns true if its parameter is a target number and returns false otherwise. Some target numbers are positive, and some are negative.

Method runSimulation obtains sampleSize values using the getInt method. The method returns true if there are more positive target values (target values that are greater than 0) in the first half of the obtained values than in the second half of the obtained values.

Complete method runSimulation. You must use getInt and isTarget appropriately in order to receive full credit.

/** Analyzes sampleSize values obtained using the getInt method, as described in

* part (a)

* Precondition: sampleSize is a positive even integer.

*/

public static boolean runSimulation(int sampleSize)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!