[英]how can I find the lowest value of each row and exclude them to find a new average from array
[英]how can i find the average of the observation?
我真的很努力地與此類生成一個隨機數並更新觀察者的類。 我如何找到所有這些隨機數的平均值? (observations_avg)並將其傳遞給觀察者
A級
public abstract class A implements Observer {
public abstract String disp();
public abstract void update(Observable subject, Object o);
}
B級
public class B extends A {
public double limit;
public B(double limit) {
this.limit = limit;
}
public void update(Observable o, Object arg) {
if (observations_avg >= limit) {
System.out.println(disp());
}
}
public String disp() {
// observations_avg = sum of all observation, and divide it by the quantity of them
String d = "WARNING avg exceeded limit: " + observations_avg;
return d;
}
}
C級
public class C extends A {
Avg avg = new Avg(10);
public void update(Observable subject, Object o) {
System.out.println(disp());
}
public String disp() {
String d = "avg is " observations_avg+ ; // i want to display the average of all the random numbers
return d;
}
}
生成隨機數並更新觀察者類Avg
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();
}
}
主要的
public class Main {
public static void main(String[] args) {
Avg avg = new Avg(10);
// create observers and add them
B B_obj = new B(8);
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();
}
}
}
先感謝您。
您需要在Avg對象中創建類似getAverage()的方法,然后在觀察avg的同時檢索值:
public void update(Observable o, Object arg) {
Avg avg = (Avg) o;
double observations_avg = avg.getAverage();
if (observations_avg >= limit) {
System.out.println(disp());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.