20165324 第十周课下补做

20165324 第十周课下补做

未完成作业为:

数据结构排序:

  • 在数据结构和算法中,排序是很重要的操作,要让一个类可以进行排序,有两种方法:
    1. 有类的源代码,针对某一成员变量排序,让类实现Comparable接口,调用Collection.sort(List)
    2. 没有类的源代码,或者多种排序,新建一个类,实现Comparator接口 调用Collection.sort(List, Compatator)
  • 针对下面的Student类,使用Comparator编程完成以下功能:
    1. 在测试类StudentTest中新建学生列表,包括自己和学号前后各两名学生,共5名学生,给出运行结果(排序前,排序后)
    2. 对这5名同学分别用学号和总成绩进行增序排序,提交两个Comparator的代码
    3. 课下提交代码到码云
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;
   }
}

补做点一:在测试类StudentTest中新建学生列表,包括自己和学号前后各两名学生,共5名学生,给出运行结果(排序前,排序后)

补做点二:对这5名同学分别用学号和总成绩进行增序排序,提交两个Comparator的代码。
完整代码为:

/*
数据结构—排序
 */
/*
数据结构—排序
 */
package yan;
import java.util.*;
class StudentKey implements Comparable {
    double d=0;
    StudentKey (double d) {
        this.d=d;
    }
    public int compareTo(Object b) {
        StudentKey st=(StudentKey)b;
        if((this.d-st.d)==0)
            return -1;
        else
            return (int)((this.d-st.d)*1000);
    }
}
class Student {
    String name=null;
    int ID;
    double mathScore,phyScore,engScore;
    double sum=0;
    Student(String s,int i,double m,double p,double e) {
        name=s;
        ID=i;
        mathScore=m;
        phyScore=p;
        engScore=e;
    }
    public double getSum() {
        return sum=mathScore+phyScore+engScore;
    }
}
public class Test1 {
    public static void main(String args[ ]) {
        TreeMap<StudentKey,Student>  treemap= new TreeMap<StudentKey,Student>();
        String str[]={"王瑶佳","杨金川","何春江","李东骏","陈卓"};
        int ID[]={22,23,24,25,26};
        double m[]={78,84,85,98,87};
        double p[]={79,77,87,89,91};
        double e[]={99,78,88,74,68};
        Student student[]=new Student[5];
        for(int k=0;k<student.length;k++) {
            student[k]=new Student(str[k],ID[k],m[k],p[k],e[k]);
        }
        StudentKey key[]=new StudentKey[5] ;
        for(int k=0;k<key.length;k++) {
            key[k]=new StudentKey(student[k].ID);
        }
        for(int k=0;k<student.length;k++) {
            treemap.put(key[k],student[k]);  //put方法添加结点
        }
        int number=treemap.size();
        System.out.println("树映射中有"+number+"个对象,按学号排序::");
        Collection<Student> collection=treemap.values();//遍历
        Iterator<Student> iter=collection.iterator();//迭代
        while(iter.hasNext()) {
            Student stu=iter.next();
            System.out.println("姓名 "+stu.name+" 学号 "+stu.ID);
        }
        treemap.clear();
        for(int k=0;k<key.length;k++) {
            key[k]=new StudentKey(student[k].getSum());
        }
        for(int k=0;k<student.length;k++) {
            treemap.put(key[k],student[k]);
        }
        number=treemap.size();
        System.out.println("树映射中有"+number+"个对象,按总成绩排序::");
        collection=treemap.values();
        iter=collection.iterator();
        while(iter.hasNext()) {
            Student stu=(Student)iter.next();
            System.out.println("姓名 "+stu.name+" 成绩 "+stu.getSum());
        }
    }
}



  • 测试截图:

未完成分析:

  • 问题1:出现测试类错误如图,未能在课上解决。

  • 问题1解决方法:
  • 问题2:分类混乱,尤其Student类在src目录下多次改写
  • 问题2解决方法:灵活使用包来对类进行区分,在课上进行测试时,重建一个IDEA项目,统一编写。
  • 问题3:时间未能把握,未能提交任何截图
  • 问题3解决方法:完成一部分内容,就可以尽快上传相应截图。

猜你喜欢

转载自www.cnblogs.com/20165324hcj/p/8987233.html
今日推荐