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

I\'m trying to find the average of the random number in java. at this moment i\'

ID: 3736628 • Letter: I

Question

I'm trying to find the average of the random number in java. at this moment i'm printing the random number, what i want is to sum all of the observations, and divide by the quantity of them. and then print them. and if the random number exceeds 8, print warning. the problem in my output is that it doesn't print the average. and doesn't detect the exceeded number correctly as you can see in the last figure. Thank you.

1)

2)

3)

4)

5)

30 import java.util.0bservable; 6 public abstract class A implements Observer 7 8 public 8 public abstract String dispO; A10 public abstract void update(Observable subject, 0bject o); 12 13

Explanation / Answer

A.java

import java.util.*;

public abstract class A implements Observer{

public double sum;

public double count;

public abstract String disp();

public abstract void update(Observable subject, Object o);

}

B.java

import java.util.*;

public class B extends A {

private double limit;

Avg avg = new Avg(10);

public B(double limit) {

this.limit = limit;

}

public void update(Observable o, Object arg) {

super.sum+=avg.getRandom();

super.count++;

if ((super.sum/super.count) >= limit) {

System.out.println(disp());

}

}

public String disp() {

String d = "WARNING avg exceeded limit: " + (super.sum/super.count);

return d;

}

}

C.java

import java.util.*;

public class C extends A {

Avg avg = new Avg(10);

private List<Double> observers = new ArrayList<>();

public double observ_avg;

public void update(Observable o, Object arg) {

super.sum+=avg.getRandom();

super.count++;

System.out.println(disp());

}

public String disp() {

String d = "avg is " + (super.sum/super.count);

return d;

}

}

Avg.java

import java.util.*;

public class Avg extends Observable {

private int seed;

Random random = new Random();

public Avg(int seed) {

this.seed = seed;

random.setSeed(this.seed);

}

public double getRandom() {

double r = random.nextDouble() * 10;

return r;

}

public void read() {

setChanged();

notifyObservers();

}

}

Main.java

import java.util.*;

public class Main{

public static void main(String args[]){

Avg avg = new Avg(10);

B B_obj = new B(5);

C C_obj = new C();

avg.addObserver(B_obj);

avg.addObserver(C_obj);

try{

while(true){

avg.read();

Thread.sleep(1000);

}

} catch(InterruptedException e){

e.printStackTrace();

}

}

}