小李是一名大二学生,在班级里担任学习委员一职。现班主任需要小李协助统计班级三十名同学(包含小李)的基本信息(姓名、年龄、是否担任班级职务),且对每位同学在生物化学、物理化学、电子与电工技术三门课中取得的成绩进行录入,并算出每位学生三门课的总分和算术平均分。随后根据每位学生的算术平均分大小进行从高到低排序,获取排名前十的学生作为本年度校内奖学金候选人。除此之外,基于三十名学生的成绩,算出班级在这三门课中每门课整体的平均分、标准差以及中位数。请结合本学期学习的Java语言编程,解决下面提到的问题。
问题:
1.给出存储班级三十名学生基本信息的Java代码,基本信息的具体赋值由考生自拟,要求最后一名学生的信息为考生本人信息。可适当在代码中加入注释。
2. 给出存储班级三十名学生三门课程成绩的Java代码,课程分数的具体赋值由考生自拟。
3. 给出计算并存储每位学生三门课的总分和算术平均分功能的Java代码。
4. 给出对每位学生的算术平均分大小进行从高到低排序功能的Java代码,并通过代码输出排名前十位学生的名字。
5. 给出计算班级在这三门课中每门课整体的平均分、标准差以及中位数功能的Java代码。
定义student类,再定义相关方法就行了
//学生类
public class Student {
private String name;
private int age;
private boolean isHoldClassPosition;//是否担任班级职务
private CourseScore courseScore;//课程分数
int totalScore;//总分
int meanScore;//平均分
public Student() {
}
public Student(String name) {
this.name = name;
}
public Student(String name, int age, boolean isHoldClassPosition) {
this.name = name;
this.age = age;
this.isHoldClassPosition = isHoldClassPosition;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isHoldClassPosition() {
return isHoldClassPosition;
}
public void setHoldClassPosition(boolean holdClassPosition) {
isHoldClassPosition = holdClassPosition;
}
public void setCourseScore(CourseScore courseScore) {
this.courseScore = courseScore;
}
public CourseScore getCourseScore() {
return courseScore;
}
public double getTotalScore() {
return totalScore;
}
public void setTotalScore(int totalScore) {
this.totalScore = totalScore;
}
public double getMeanScore() {
return meanScore;
}
public void setMeanScore(int meanScore) {
this.meanScore = meanScore;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public String toString() {
return "学生{" +
"姓名='" + name + '\'' +
", 年龄=" + age +
", 是否担任班级职务=" + isHoldClassPosition +
(courseScore == null ? "" : ", 各科分数=" + courseScore.toString()) +
", 总分=" + totalScore +
", 平均分=" + meanScore +
'}';
}
/课程分数
public class CourseScore {
int biochemistry;//生物化学
int physicalChemistry;//物理化学
int electronic;//电子与电工技术
public CourseScore() {
}
public CourseScore(int biochemistry, int physicalChemistry, int electronic) {
this.biochemistry = biochemistry;
this.physicalChemistry = physicalChemistry;
this.electronic = electronic;
}
public double getBiochemistry() {
return biochemistry;
}
public void setBiochemistry(int biochemistry) {
this.biochemistry = biochemistry;
}
public double getPhysicalChemistry() {
return physicalChemistry;
}
public void setPhysicalChemistry(int physicalChemistry) {
this.physicalChemistry = physicalChemistry;
}
public double getElectronic() {
return electronic;
}
public void setElectronic(int electronic) {
this.electronic = electronic;
}
public int calculateTotalScore() {
return this.biochemistry + this.physicalChemistry + this.electronic;
}
public int calculateMeanScore() {
return calculateTotalScore() / 3;
}
@Override
public String toString() {
return "课程分数{" +
"生物化学 =" + biochemistry +
",物理化学=" + physicalChemistry +
",电子与电工技术=" + electronic +
'}';
}
import java.util.ArrayList;
import java.util.List;
public class ClassRoom {
private List<Student> mStudents;
double bMeanScore;//生物化学平均分
double pMeanScore;//物理化学平均分
double eMeanScore;//电子与电工技术平均分
double bStd;//生物化学标准差
double pStd;//物理化学标准差
double eStd;//电子与电工技术标准差
double bMedian;//生物化学中位数
double pMedian;//物理化学中位数
double eMedian;//电子与电工中位数
public ClassRoom() {
mStudents = new ArrayList<>();
}
public void addStudent(Student student) {
mStudents.add(student);
}
public List<Student> getStudents() {
return mStudents;
}
public void setStudentCourseScoreByName(String name, CourseScore courseScore) {
mStudents.get(mStudents.indexOf(new Student(name))).setCourseScore(courseScore);
}
public void setStudents(List<Student> students) {
mStudents = students;
}
public double getbMeanScore() {
return bMeanScore;
}
public void setbMeanScore(double bMeanScore) {
this.bMeanScore = bMeanScore;
}
public double getpMeanScore() {
return pMeanScore;
}
public void setpMeanScore(double pMeanScore) {
this.pMeanScore = pMeanScore;
}
public double geteMeanScore() {
return eMeanScore;
}
public void seteMeanScore(double eMeanScore) {
this.eMeanScore = eMeanScore;
}
public double getbStd() {
return bStd;
}
public void setbStd(double bStd) {
this.bStd = bStd;
}
public double getpStd() {
return pStd;
}
public void setpStd(double pStd) {
this.pStd = pStd;
}
public double geteStd() {
return eStd;
}
public void seteStd(double eStd) {
this.eStd = eStd;
}
public double getBmedian() {
return bMedian;
}
public void setBmedian(double bMedian) {
this.bMedian = bMedian;
}
public double getpMedian() {
return pMedian;
}
public void setpMedian(double pMedian) {
this.pMedian = pMedian;
}
public double geteMedian() {
return eMedian;
}
public void seteMedian(double eMedian) {
this.eMedian = eMedian;
}
@Override
public String toString() {
return "ClassRoom{" +
", bMeanScore=" + bMeanScore +
", pMeanScore=" + pMeanScore +
", eMeanScore=" + eMeanScore +
", bStd=" + bStd +
", pStd=" + pStd +
", eStd=" + eStd +
", bMedian=" + bMedian +
", pMedian=" + pMedian +
", eMedian=" + eMedian +
'}';
}
}
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
ClassRoom classRoom = new ClassRoom();
//录入学生基本信息
classRoom.addStudent(new Student("张三", 21, false));
classRoom.addStudent(new Student("李四", 22, false));
classRoom.addStudent(new Student("王五", 23, false));
//此处省略26名学生
classRoom.addStudent(new Student("考生姓名", 25, false));
//获取全部学生
List<Student> studentList = classRoom.getStudents();
for (Student student : studentList) {
System.out.println("获取全部学生");
System.out.println(student.toString());
}
//录入学生成绩
classRoom.setStudentCourseScoreByName("张三", new CourseScore(65, 76, 87));
classRoom.setStudentCourseScoreByName("李四", new CourseScore(66, 77, 88));
classRoom.setStudentCourseScoreByName("王五", new CourseScore(67, 78, 89));
//此处省略26名学生
classRoom.setStudentCourseScoreByName("考生姓名", new CourseScore(68, 79, 90));
//计算并存储每位学生三门课的总分和算术平均分功能
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
int totalScore = student.getCourseScore().calculateTotalScore();
int meanScore = student.getCourseScore().calculateMeanScore();
student.setTotalScore(totalScore);
student.setMeanScore(meanScore);
}
//对每位学生的算术平均分大小进行从高到低排序功能的Java代码,并通过代码输出排名前十位学生的名字。
Collections.sort(studentList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.meanScore - o1.meanScore;
}
});
for (int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i).toString());
}
//计算班级在这三门课中每门课整体的平均分、标准差以及中位数功能的Java代码。
double bTotalScore = 0, pTotalScore = 0, etotalScore = 0;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
bTotalScore += student.getCourseScore().biochemistry;
pTotalScore += student.getCourseScore().physicalChemistry;
etotalScore += student.getCourseScore().electronic;
}
//设置平均分
classRoom.setbMeanScore(bTotalScore / studentList.size());
classRoom.setpMeanScore(pTotalScore / studentList.size());
classRoom.seteMeanScore(etotalScore / studentList.size());
double bVar = 0, pVar = 0, eVar = 0;
for (int i = 0; i < studentList.size(); i++) {
bVar = (studentList.get(i).getCourseScore().getBiochemistry() - classRoom.getbMeanScore()) * (studentList.get(i).getCourseScore().getBiochemistry() - classRoom.getbMeanScore());
pVar = (studentList.get(i).getCourseScore().getPhysicalChemistry() - classRoom.getpMeanScore()) * (studentList.get(i).getCourseScore().getPhysicalChemistry() - classRoom.getpMeanScore());
eVar = (studentList.get(i).getCourseScore().getElectronic() - classRoom.geteMeanScore()) * (studentList.get(i).getCourseScore().getElectronic() - classRoom.geteMeanScore());
}
//标准差
classRoom.setbStd(Math.sqrt(bVar / studentList.size()));
classRoom.setpStd(Math.sqrt(pVar / studentList.size()));
classRoom.seteStd(Math.sqrt(eVar / studentList.size()));
//中位数
//获取成绩在中间的两个学生
Student student1 = studentList.get(studentList.size() / 2 - 1);
Student student2 = studentList.get(studentList.size() / 2);
classRoom.setBmedian((student1.getCourseScore().getBiochemistry() * student2.getCourseScore().getBiochemistry()) / 2F);
classRoom.setpMedian((student1.getCourseScore().getPhysicalChemistry() * student2.getCourseScore().getPhysicalChemistry()) / 2F);
classRoom.seteMedian((student1.getCourseScore().getElectronic() * student2.getCourseScore().getElectronic()) / 2F);
System.out.println(classRoom.toString());
}
}
```