当前位置: 代码迷 >> Java相关 >> 关于计算总分与平均分的问题!
  详细解决方案

关于计算总分与平均分的问题!

热度:491   发布时间:2010-05-04 10:23:53.0
关于计算总分与平均分的问题!
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-04 02:23:53
HeiN

等 级:论坛游侠
帖 子:53
专家分:193
注 册:2010-3-30
  得分:0 
如果牵涉到表用sum函数和avg函数呀,如果不是 ,就直接求呀
----------------解决方案--------------------------------------------------------
程序代码:
public class SumAndAvgImpl extends SumAndAvg{

    public static void main(String[] args) {
        Student s1=new Student(1,"1",90);
        Student s2=new Student(1,"1",91);
        Student s3=new Student(1,"1",92);
        Student s4=new Student(1,"1",93);
        Student s5=new Student(1,"1",94);
        Student s6=new Student(1,"1",95);
        Student s7=new Student(1,"1",96);
        SumAndAvg saa = new SumAndAvgImpl();
        saa.addStus(s1);
        saa.addStus(s2);
        saa.addStus(s3);
        saa.addStus(s4);
        saa.addStus(s5);
        saa.addStus(s6);
        saa.addStus(s7);
        System.out.println(saa.getAvg());
        System.out.println(saa.getSum());
        System.out.println(saa.getSum(2,4));
        System.out.println(saa.getAvg(2,4));
    }

    @Override
    public float getAvg() {
        return getSum()/getStus().size();
    }

    @Override
    public float getAvg(int startN, int endN) {
        return getSum(startN, endN)/(endN-startN+1);
    }

    @Override
    public float getSum() {
        float sum=0;
        for(Student stu:getStus()){
            sum+=stu.getScore();
        }
        return sum;
    }

    @Override
    public float getSum(int startN, int endN) {
        float sum=0;
        for(int i = startN;i<=endN;i++){
            sum = sum+getStus().get(i-1).getScore();
        }
        return sum;
    }

}



public class Student {
    private float num;
    private String name;
    private float score;
   
    public Student(){
      
    }
   
    public Student(int num, String name, int score){
        this.num=num;
        this.name=name;
        this.score=score;
    }
   
    public float getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
   
   
}





import java.util.ArrayList;

public abstract class SumAndAvg {
    private static ArrayList<Student> stus=new ArrayList<Student>();;
    public abstract float getSum();
    public abstract float getAvg();
    public abstract float getSum(int startN, int endN);
    public abstract float getAvg(int startN, int endN);
   
    public SumAndAvg(){
      
    }
   
    public void addStus(Student stu){
        stus.add(stu);
    }
   
    public ArrayList<Student> getStus(){
        return stus;
    }
}
代码没有判断是否溢出,楼主自己修改
----------------解决方案--------------------------------------------------------
以下是引用linjx0123在2010-5-4 14:42:26的发言:

public class SumAndAvgImpl extends SumAndAvg{

    public static void main(String[] args) {
        Student s1=new Student(1,"1",90);
        Student s2=new Student(1,"1",91);
        Student s3=new Student(1,"1",92);
        Student s4=new Student(1,"1",93);
        Student s5=new Student(1,"1",94);
        Student s6=new Student(1,"1",95);
        Student s7=new Student(1,"1",96);
        SumAndAvg saa = new SumAndAvgImpl();
        saa.addStus(s1);
        saa.addStus(s2);
        saa.addStus(s3);
        saa.addStus(s4);
        saa.addStus(s5);
        saa.addStus(s6);
        saa.addStus(s7);
        System.out.println(saa.getAvg());
        System.out.println(saa.getSum());
        System.out.println(saa.getSum(2,4));
        System.out.println(saa.getAvg(2,4));
    }

    @Override
    public float getAvg() {
        return getSum()/getStus().size();
    }

    @Override
    public float getAvg(int startN, int endN) {
        return getSum(startN, endN)/(endN-startN+1);
    }

    @Override
    public float getSum() {
        float sum=0;
        for(Student stu:getStus()){  //ArrayList不能用foreach迭代
            sum+=stu.getScore();
        }
        return sum;
    }

    @Override
    public float getSum(int startN, int endN) {
        float sum=0;
        for(int i = startN;i<=endN;i++){
            sum = sum+getStus().get(i-1).getScore();
        }
        return sum;
    }

}



public class Student {
    private float num;
    private String name;
    private float score;
   
    public Student(){
        
    }
   
    public Student(int num, String name, int score){
        this.num=num;
        this.name=name;
        this.score=score;
    }
   
    public float getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
   
   
}





import java.util.ArrayList;

public abstract class SumAndAvg {
    private static ArrayList stus=new ArrayList();;
    public abstract float getSum();
    public abstract float getAvg();
    public abstract float getSum(int startN, int endN);
    public abstract float getAvg(int startN, int endN);
   
    public SumAndAvg(){
        
    }
   
    public void addStus(Student stu){
        stus.add(stu);
    }
   
    public ArrayList getStus(){
        return stus;
    }
}代码没有判断是否溢出,楼主自己修改
子类SumAndAvgImpl有点错误

[ 本帖最后由 lampeter123 于 2010-5-4 15:52 编辑 ]
----------------解决方案--------------------------------------------------------
楼上的方法可以,但是看了下,有两个缺陷:
1.无法计算任意几个学生的总分或者平均分,比如不连续的几个学生。
2.不是很面向对象。

我写了个,献丑了:
package student;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

/**
* 学生登记册
* @author baifenghan
*
*/
public class Register implements Caculate{
   
    //用于记录所有注册的学生信息
    private Map<Integer, Student> stuRegister = new HashMap<Integer, Student>();
   
    public Register() {
        initStuRegister();
    }
   
    /**
     * 将已经注册的学生信息加入登记册
     */
    private void initStuRegister()
    {
        int[] scores = {75, 85, 68, 84, 96};
        Student stu = null;
        for (int i = 0; i < scores.length; i++) {
            stu = new Student(i, scores[i]);
            addStudent(stu);
        }
    }
   
    public void addStudent(Student stu)
    {
        stuRegister.put(stu.getId(), stu);
    }
   
    public void removeStudent(int id)
    {
        stuRegister.remove(id);
    }
   
    public Student getStudentById(int id)
    {
        return stuRegister.get(id);
    }
   
    /**
     * 根据一组学生Id返回相应的学生信息
     * @param ids
     * @return
     */
    public List<Student> getSomeStudentByIds(int[] ids)
    {
        List<Student> ret = new ArrayList<Student>();
        for (int i = 0; i < ids.length; i++) {
            ret.add(getStudentById(ids[i]));
        }
        
        return ret;
    }

    @Override
    public float getAvg(int[] ids) {
        
        if (ids == null || ids.length == 0)
        {
            JOptionPane.showMessageDialog(null, "所选择的学生数量为零,无法计算平均成绩。");
            return -1;
        }
        else
        {
            return getTotal(ids) / ids.length;
        }
    }

    @Override
    public float getTotal(int[] ids) {
        List<Student> stus = getSomeStudentByIds(ids);
        float ret = 0;
        for (Student student : stus) {
            ret += student.getScore();
        }
        return ret;
    }

}

package student;

/**
* 标示一个学生信息
* @author baifenghan
*
*/
public class Student {

    // 唯一标示学生的学号
    private int id;
    // 该学生的分数
    private float score;

    public Student() {
    }

    public Student(int id, float score) {
        this.id = id;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }

}


package student;

public interface Caculate {
   
    /**
     * 获得指定的一组学生的总分
     * @param ids
     * @return
     */
    float getTotal(int[] ids);
    /**
     * 获得指定的一组学生的平均分
     * @param ids
     * @return
     */
    float getAvg (int[] ids);
   

}


package student;

import junit.framework.Assert;

import org.junit.Test;

/**
* 单元测试用类
* @author baifenghan
*
*/
public class TestRegister {
   
    private Caculate register = new Register();
   
    @Test public void testGetTotal()
    {
        int[] input = {0, 1, 4};
        Assert.assertEquals(register.getTotal(input), 256.0, 0.0001);
    }
   
    @Test public void testGetAvg()
    {
        int[] input = {0, 1, 4};
        Assert.assertEquals(register.getAvg(input), 256 / 3.0, 0.0003);
    }

}



----------------解决方案--------------------------------------------------------
  相关解决方案