DEV/DEV2.1/TP6:Polymorphisme/Moyenne.java

23 lines
367 B
Java
Raw Permalink Normal View History

2023-03-27 11:28:45 +02:00
public class Moyenne{
private double total=0;
private long cpt=0;
public void add(Number val){
total += val.doubleValue();
++cpt;
}
public double getAverage(){
if (cpt==0){
return 0;
}
return total/cpt;
}
public static void main(String[] args) {
Moyenne m = new Moyenne();
m.add(5.5);
m.add(10);
System.out.println(m.getAverage());
}
}