日期:2014-05-20 浏览次数:20793 次
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
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 ; } }