日期:2014-05-20  浏览次数:20793 次

哪位好心人可以给一个完整的代码?
完整的题目如下:
组合的使用
1、创建一个Student类,有name、computerScore、mathScore三个成员变量及相应的getXXX方法。
2、创建一个BanJi类,它有一个学生数组作为成员变量,并有添加学生和删除学生、求班级所有学生总成绩和平均成绩的方法.
3、创建一个Test类,它有一个main函数,在该类中创建一些学生并将这些学生添加到班级类中,求出整个班级所有学生的平均成绩和总成绩。

------解决方案--------------------
哎,LZ太懒了。这种自己写写才能提高啊。给你段代码参考吧。
Java code

package com.student;

public class Student {

    private String name;
    private double computerScore;
    private double mathScore;

    public String getName() {
        return name;
    }

    public double getComputerScore() {
        return computerScore;
    }

    public double getMathScore() {
        return mathScore;
    }

    public Student(String name, double computerScore, double mathScore) {
        super();
        this.name = name;
        this.computerScore = computerScore;
        this.mathScore = mathScore;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        long temp;
        temp = Double.doubleToLongBits(computerScore);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(mathScore);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Student other = (Student) obj;
        if (Double.doubleToLongBits(computerScore) != Double
                .doubleToLongBits(other.computerScore))
            return false;
        if (Double.doubleToLongBits(mathScore) != Double
                .doubleToLongBits(other.mathScore))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}
package com.student;

import java.util.ArrayList;
import java.util.List;

public class BanJi {

    private List<Student> students;

    public BanJi() {
        students = new ArrayList<Student>();
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    public void delStudent(Student student) {
        students.remove(student);
    }

    public double getComputerAllScore() {
        double score = 0;
        for (Student student : students) {
            score += student.getComputerScore();
        }
        return score;
    }

    public double getMathAllScore() {
        double score = 0;
        for (Student student : students) {
            score += student.getMathScore();
        }
        return score;
    }

    public double getAvgMathScore() {
        if (students.size() == 0) {
            return 0;
        }
        return getMathAllScore() / students.size();
    }

    public double getAvgComputerScore() {
        if (students.size() == 0) {
            return 0;
        }
        return getComputerAllScore() / students.size();
    }
}

package com.student;

public class Test {

    public static void main(String[] args) {
        BanJi banji = new BanJi();
        banji.addStudent(new Student("张1",80,90));
        banji.addStudent(new Student("张2",81,91));
        banji.addStudent(new Student("张3",82,92));
        banji.addStudent(new Student("张4",83,93));
        System.out.println(banji.getComputerAllScore());
        System.out.println(banji.getMathAllScore());
        System.out.println(banji.getAvgComputerScore());
        System.out.println(banji.getAvgMathScore());
    }
}

------解决方案--------------------
Sutdent.java
Java code

package com.self ;

class Student{

    private String name;
    private double computerScore ;
    private double mathScore ;
    
    public Student(){
    }
    
    public Student(String name, double computerScore, double mathScore){
        this.name = name ;
        this.computerScore = computerScore ;
        this.mathScore = mathScore ;    
    }
    public String getName(){
        return this.name ;
    }
    public void setName(String name){
        this.name = name ;
    }
    
    public double getComputerScore(){
        return this.computerScore ;
    }
    public void setComputerScore(double computerScore){
        this.computerScore = computerScore ;
    }
    
    public double getMathScore(){
        return this.mathScore ;
    }
    public void setMathScore(double mathScore){
        this.mathScore = mathScore ;
    }
}