数据结构-排序(选做) 20175204
Posted zyzgl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构-排序(选做) 20175204相关的知识,希望对你有一定的参考价值。
数据结构-排序(选做)
任务要求
在数据结构和算法中,排序是很重要的操作,要让一个类可以进行排序,有两种方法:
- 有类的源代码,针对某一成员变量排序,让类实现Comparable接口,调用Collection.sort(List)
- 没有类的源代码,或者多种排序,新建一个类,实现Comparator接口 调用Collection.sort(List, Compatator)
针对下面的Student类,使用Comparator编程完成以下功能:
- 在测试类StudentTest中新建学生列表,包括自己和学号前后各两名学生,共5名学生,给出运行结果(排序前,排序后)
- 对这5名同学分别用学号和总成绩进行增序排序,提交两个Comparator的代码
- 课下提交代码到码云
给定student类
class Student {
private String id;//表示学号
private String name;//表示姓名
private int age;//表示年龄
private double computer_score;//表示计算机课程的成绩
private double english_score;//表示英语课的成绩
private double maths_score;//表示数学课的成绩
private double total_score;// 表示总成绩
private double ave_score; //表示平均成绩
public Student(String id, String name){
this.id = id;
this.name = name;
}
public Student(String id, String name, char sex, int age){
this(id, name);
this.sex = sex;
this.age = age;
}
public String getId(){
return id;
}//获得当前对象的学号,
public double getComputer_score(){
return computer_score;
}//获得当前对象的计算机课程成绩,
public double getMaths_score(){
return maths_score;
}//获得当前对象的数学课程成绩,
public double getEnglish_score(){
return english_score;
}//获得当前对象的英语课程成绩,
public void setId(String id){
this.id=id;
}// 设置当前对象的id值,
public void setComputer_score(double computer_score){
this.computer_score=computer_score;
}//设置当前对象的Computer_score值,
public void setEnglish_score(double english_score){
this.english_score=english_score;
}//设置当前对象的English_score值,
public void setMaths_score(double maths_score){
this.maths_score=maths_score;
}//设置当前对象的Maths_score值,
public double getTotalScore(){
return computer_score+maths_score+english_score;
}// 计算Computer_score, Maths_score 和English_score 三门课的总成绩。
public double getAveScore(){
return getTotalScore()/3;
}// 计算Computer_score, Maths_score 和English_score 三门课的平均成绩。
}
class Undergraduate extends Student{
private String classID;
public Undergraduate(String id, String name, char sex, int age,String classID){
super(id,name,sex,age);
this.classID=classID;
}
public String getClassID(){
return classID;
}
public void setClassID(String classID){
this.classID=classID;
}
}
修改增加student类
* task2
*
* @author 20175204
* @date 2019/5/5
*/
class Student {
private char sex = 0;
private String id;//表示学号
private String name;//表示姓名
private int age;//表示年龄
private double computer_score;//表示计算机课程的成绩
private double english_score;//表示英语课的成绩
private double maths_score;//表示数学课的成绩
private double total_score;// 表示总成绩
private double ave_score; //表示平均成绩
public int xuehao, mingci;
public double a1, a2, a3;
Student(int xuehao, String name, int age, double a1, double a2, double a3) {
this.xuehao = xuehao;
this.name = name;
this.age = age;
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
}
public Student(String id, String name) {
this.id = id;
this.name = name;
}
public Student(String id, String name, char sex, int age) {
this(id, name);
this.sex = sex;
this.age = age;
}
public String getId() {
return id;
}//获得当前对象的学号,
public double getComputer_score() {
return computer_score;
}//获得当前对象的计算机课程成绩,
public double getMaths_score() {
return maths_score;
}//获得当前对象的数学课程成绩,
public double getEnglish_score() {
return english_score;
}//获得当前对象的英语课程成绩,
public void setId(String id) {
this.id = id;
}// 设置当前对象的id值,
public void setComputer_score(double computer_score) {
this.computer_score = computer_score;
}//设置当前对象的Computer_score值,
public void setEnglish_score(double english_score) {
this.english_score = english_score;
}//设置当前对象的English_score值,
public void setMaths_score(double maths_score) {
this.maths_score = maths_score;
}//设置当前对象的Maths_score值,
public double getTotalScore() {
return computer_score + maths_score + english_score;
}// 计算Computer_score, Maths_score 和English_score 三门课的总成绩。
public double getAveScore() {
return getTotalScore() / 3;
}// 计算Computer_score, Maths_score 和English_score 三门课的平均成绩。
double sum() {
return (this.a1 + this.a2 + this.a3);
}
public String toString() {
return "名次:" + this.mingci + " 学号:" + this.xuehao + " 年龄: " + this.age + " " + this.name + " " + this.a1 + " " +
this.a2 + " " + this.a3 + " 总分:" + this.sum();
}
class Undergraduate extends Student {
private String classID;
public Undergraduate(String id, String name, char sex, int age, String classID) {
super(id, name, sex, age);
this.classID = classID;
}
public String getClassID() {
return classID;
}
public void setClassID(String classID) {
this.classID = classID;
}
}
}
测试代码
* task2
*
* @author 20175204
* @date 2019/5/5
*/
import junit.framework.TestCase;
public class StudentTest extends TestCase {
public static void main(String[] args) {
Student[] student = new Student[5];
student[0] = new Student(20175204, "zyz",20 ,99, 98, 95);
student[1] = new Student(20175205, "hy",19 ,87, 72, 93);
student[2] = new Student(20175206, "ldc", 20,88, 73, 91);
student[3] = new Student(20175207, "ln", 20,99, 94, 56);
student[4] = new Student(20175208, "zjh",20 ,100, 100, 100);
Student temp; //排序使用的临时对象
//按sum排序
for (int i = 0; i < student.length; i++) {
for (int j = i+1; j < student.length; j++) {
if(student[j].sum() > student[i].sum()){
temp = student[i];
student[i] = student[j];
student[j] = temp;
}
}
}
int mingci = 1;//名次
for (int i = 0; i < student.length-1; i++) {
int n = checkContinue(student, student[i].sum());
if (n == 1) {
student[i].mingci = mingci++;
}else {
//总分相同,名次相同
for (int j = 0; j < n; j++) {
student[i+j].mingci = mingci;
}
mingci++;
i = i + n -1;//连续n个相同的总分,排名一样
}
}
student[student.length-1].mingci = mingci;
for (int i = 0; i < student.length; i++) {
System.out.println(student[i]) ;
}
}
//判断是否连续
public static int checkContinue(Student[] student,double sum){
int count = 0 ;//统计多少个连续相同的sum
for (int i = 0; i < student.length; i++) {
if(student[i].sum() == sum){
count++;
}
}
return count;
}
}
截图
以上是关于数据结构-排序(选做) 20175204的主要内容,如果未能解决你的问题,请参考以下文章
20175209 《MySort》和《IO-myhead》选做