标题:关于计算总分与平均分的问题!
只看楼主
huangjian325
该用户已被删除
已结贴  问题点数:20 回复次数:4 
关于计算总分与平均分的问题!
提示: 作者被禁止或删除 内容自动屏蔽
搜索更多相关主题的帖子: 平均分 总分 
2010-05-04 10:23
HeiN
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:53
专家分:193
注 册:2010-3-30
得分:0 
如果牵涉到表用sum函数和avg函数呀,如果不是 ,就直接求呀
2010-05-04 11:38
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
得分:5 
程序代码:
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;
    }
}
代码没有判断是否溢出,楼主自己修改
2010-05-04 14:42
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
得分:5 
以下是引用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 编辑 ]

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2010-05-04 15:05
baifenghan
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:258
专家分:952
注 册:2006-3-17
得分:10 
楼上的方法可以,但是看了下,有两个缺陷:
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);
    }

}


2010-05-04 18:14



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-305290-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038125 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved