?? analysis.java
字號(hào):
/**
do analysis
*/
public class Analysis implements Observer, DisplayShare
{
/*easy to understand why we have such variables*/
private double charge=0;
private double maxCharge=0;
private double minCharge=0;
private double sumCharge=0;
private double avgCharge=0;
private AbsSubject myShare;
private int doTime=0;
/*instruct function*/
public Analysis(AbsSubject myShare)
{
/*register to the distributor*/
this.myShare=myShare;
myShare.registerObserver(this);
}
/*inherit the function from Observer*/
public void update(double charge)
{
doAnalysis(charge);
}
/*the key code of this class */
public void doAnalysis(double charge)
{
/*how many times the object has been visited */
if(doTime==0)
{
maxCharge=charge;
minCharge=charge;
sumCharge=charge;
this.charge=charge;
}
else
{
/*change the max and the min*/
if(charge>maxCharge)
{
maxCharge=charge;
}
if(charge<minCharge)
{
minCharge=charge;
}
/*assignment*/
this.charge=charge;
/*cal the sum of the charge*/
sumCharge+=charge;
}
/*one more time visited*/
doTime++;
/*avg you know*/
avgCharge=sumCharge/doTime;
/*display*/
display();
}
/*I am sure you can understand*/
public void display()
{
System.out.println("Work has been done for "+doTime+" time(s).");
System.out.println(" max min avg ");
System.out.println(maxCharge+" "+minCharge+" "+avgCharge);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -