linkedlistcaculator.java

来自「PSP实践第一个练习」· Java 代码 · 共 90 行

JAVA
90
字号
package psp0_1;

import java.util.LinkedList;

/**
 * concrete caculator,using LinkedList to hold the data
 * @author qian
 * 2006 9/12
 */
public class LinkedListCaculator implements SimpleCaculator{

	public LinkedListCaculator() {
		super();
		// TODO Auto-generated constructor stub
		list = new LinkedList();
	}
	
	
	
	public void addNum(double d)
	{
		list.add(new Double(d));
	}

	
	public void clearFigure()
	{
		list.clear();
	}
	
	
	public double getMean()
	{
		double sum = 0;
		int length = 0;
		double result = 0;
		
		for(int i = 0; i < list.size();i++)
		{
			Double d =(Double)list.get(i);
			sum = sum + d.doubleValue();
			length ++;
		}
		
		if(length == 0)
		{
			result = 0;
		}else
		{
			result = sum/length;
		}
		
		return result;
	}
	
	
	/**
	 * no data will return 0 and 1 data will return 0
	 */
	public double getDeviation()
	{
		int length = list.size();
		double sum = 0;
		double mean = this.getMean();
		double result = 0;
		
		for(int i = 0; i < list.size() ; i++)
		{
			Double d =(Double)list.get(i);
			sum = sum + (d.doubleValue()-mean)*(d.doubleValue()-mean);
		}
		
		
		if(length <= 1)
		{
			result = 0;
		}else
		{
			result = Math.sqrt(sum/(length - 1));
		}
		
		
		return result;
	}
	
	
	private LinkedList list;//the list to hold the data

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?