This question takes advantage object oriented and interface concepts to generali
ID: 3722934 • Letter: T
Question
This question takes advantage object oriented and interface concepts to generalize the tasks of assignment 2. The UML diagram below presents the organization of the classes and the interface Statistics «Experiment oneRun: int Experimenter exp: Experiment stats: Statistics int numberOfRuns runExperiments: void values intl t, min, max int teStatistics(int value): void TwolnARow BirthDayParadox int int +oneRun: int +oneRun: int The Java source code below shows the intended use of these objects TwolnARowt Experimenter e; tnew TwolnARow (10) enew Experimenter ( 100); e. runExperiments ); The execution of the above program should produce the following output on the console We have run 100 experiments: the minimum was 3 the maximum was 41 the mean was 10.62 the standard deviation was 7.86 A. Implement the interface Experiment. The interface declares a method oneRun with no parameter, the return value of the method is of tvpe int.Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
// Experiment.java
public interface Experiment {
/**
* method prototype for oneRun()
*/
public int oneRun();
}
// TwoInARow.java
import java.util.Random;
public class TwoInARow implements Experiment {
/* maximum range of values */
private int range;
/* a Random object to generate random values */
Random random;
/***
* constructor with one argument
*
* @param range
* - maximum range of values
*/
public TwoInARow(int range) {
/**
* Initializing values
*/
this.range = range;
random = new Random();
}
@Override
public int oneRun() {
/**
* Generating first two run values
*/
int prev = random.nextInt(range);
int current = random.nextInt(range);
int numRuns = 2;
/**
* if the first two values are equal, it will not enter the following
* loop, or else, it will loop until current and previous values are
* same
*/
while (prev != current) {
/**
* Setting current value as previous
*/
prev = current;
/**
* generating a new value as current
*/
current = random.nextInt(range);
/**
* incrementing the number of runs needed
*/
numRuns++;
}
return numRuns;
}
}
// Experimenter.java
public class Experimenter {
/**
* Required attributes as per the UML diagram
*/
private Experiment exp;
private int numberOfRuns;
private Statistics stats;
public Experimenter(Experiment exp, int numberOfRuns) {
/**
* Initializing all fields, including Statistics object
*/
this.exp = exp;
this.numberOfRuns = numberOfRuns;
stats=new Statistics(numberOfRuns);
}
/**
* method to run experiments for specific number of times, and display the results
*/
public void runExperiments(){
/**
* running experiment for specific number of times
*/
for(int i=0;i<numberOfRuns;i++){
int value=exp.oneRun();
/**
* Updating the stats
*/
stats.updateStatistics(value);
}
/**
* displaying the results
*/
System.out.println("We have run "+numberOfRuns+" experiments");
System.out.println(" the minimum was "+stats.getMin());
System.out.println(" the maximum was "+stats.getMax());
System.out.printf(" the mean was %.2f ",stats.average());
System.out.printf(" the standard deviation was %.2f ",stats.standardDeviation());
}
}
// Statistics.java
/**
* The provided Statistics class. I haven't made any changes as this is
* sufficient enough to carry out the experiments. if you need this to be
* changed, please specify how it should be changed
*/
public class Statistics {
private int[] values;
private int count, min, max;
public Statistics(int numRuns) {
values = new int[numRuns];
count = 0;
}
public void updateStatistics(int value) {
if (count == 0) {
min = max = value;
}
min = Math.min(min, value);
max = Math.max(max, value);
values[count] = value;
count++;
}
public int getMin() {
return min;
}
public int getMax() {
return max;
}
public double average() {
double result = 0.0;
for (int i = 0; i < count; i++) {
result += values[i];
}
return result / count;
}
public double standardDeviation() {
double mean = average();
double squareSum = 0;
for (int i = 0; i < count; i++) {
squareSum += Math.pow(values[i] - mean, 2);
}
return Math.sqrt((squareSum) / count);
}
}
// Test.java
public class Test {
public static void main(String[] args) {
/**
* Defining TwoInARow object and an Experimenter object, running and
* displaying the experiments
*/
TwoInARow t = new TwoInARow(10);
Experimenter e = new Experimenter(t, 100);
e.runExperiments();
}
}
/*OUTPUT*/
We have run 100 experiments
the minimum was 2
the maximum was 66
the mean was 12.18
the standard deviation was 11.73
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.