实践一

教材p448 Example15_4

  1. list中增加自己学号后三名同学,学号是最后三名的从1号开始加入
  2. 提交运行结果截图
  3. 刻下推送代码到码云

题目分析:

将代码简单修改学号姓名就好
截图如下:

实践二

数据结构和算法中,排序是很重要的操作,要让一个类可以进行排序,有两种方法:

  • 有类的源代码,针对某一成员变量排序,让类实现Comparable接口,调用Collection.sort(List)
  • 没有类的源代码,或者多种排序,新建一个类,实现Comparator接口 调用Collection.sort(List, Compatator)

针对下面的Student类,使用Comparator编程完成以下功能:

  1. 在测试类StudentTest中新建学生列表,包括自己和学号前后各两名学生,共5名学生,给出运行结果(排序前,排序后)
  2. 对这5名同学分别用学号和总成绩进行增序排序,提交两个Comparator的代码
  3. 课下提交代码到码云

题目分析:

comparator接口(01,02)->{}

排序默认是按照升序排序
如果返回-1,就认为01 小于02,(注意01和02的顺序)
如果返回0,认为两个相等
如果返回1,就认为01大于02,(注意01和02的顺序)
对student对象数组进行排序,用的sort方法,在实现comparator接口时,sort方法需要传进来两个参数,即stu对象数组,以及重写的实现了comparator比较方法类。
代码如下:

import java.util.*;
class StudentData {
    public  static void main(String[] args) {
        List<Student> list = new LinkedList<>();
        list.add(new Student(20155327,"李百乾","m",21,66,88,89));
        list.add(new Student(20165328,"张三","m",21,77,99,76));
        list.add(new Student(20165329,"李四","w",20,88,77,78));
        list.add(new Student(20165330,"王五","m",21,99,87,90));
        list.add(new Student(20165331,"周六","m",20,98,78,93));
        SortByTotal_score sortBytotal_score = new SortByTotal_score();
        Collections.sort(list, sortBytotal_score);
        SortByID sortByID = new SortByID();
        Collections.sort(list, sortByID);
        System.out.println("根据学生学号由低到高排序:");
        for (Student student : list) {
            System.out.println(student);
        }

        Collections.sort(list, sortBytotal_score);
        System.out.println("根据学生成绩由低到高排序:");
        for (Student student : list) {
            System.out.println(student);
        }
    }
}
class Student {

    private int id;//表示学号
    private String name;//表示姓名
    private int age;//表示年龄
    private String sex;//表示性别
    private double computer_score;//表示计算机课程的成绩
    private double english_score;//表示英语课的成绩
    private double maths_score;//表示数学课的成绩
    private double total_score;// 表示总成绩
    private double ave_score; //表示平均成绩

    @Override
    public String toString() {
        return "Student[name:"+name+",age:"+age+",number:"+id+",total_score"+total_score+"]";
    }

    public Student(int id, String name, String sex, int age, double computer_score, double english_score, double maths_score) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.computer_score = computer_score;
        this.english_score = english_score;
        this.maths_score = maths_score;
    }

    public String getName() {
        return name;
    }
    public int 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 setName(String name) {
        this.name = name;
    }
    public void setId(int 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() {
        total_score=computer_score + maths_score + english_score;
        return total_score;
    }// 计算Computer_score, Maths_score 和English_score 三门课的总成绩。

    public double getAveScore() {
        return getTotalScore() / 3;
    }// 计算Computer_score, Maths_score 和English_score 三门课的平均成绩。

}

class SortByID implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getId() - o2.getId();
    }

}
class SortByTotal_score implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return (int)( o1.getTotalScore() - o2.getTotalScore());
    }
}

截图如下

实践三

参见附件,补充MyList.java的内容,提交运行结果截图(全屏)
课下推送代码到码云

题目分析:

1.编写一个Node类来充当结点的模型。我们知道,其中有两个属性,1存放数据的data,2存放下一结点的引用

public class Node<T>                             //单链表结点类,T指定结点的元素类型
{
    public T data;                               //数据域,存储数据元素
    public Node<T> next;                         //地址域,引用后继结点

    public Node(T data, Node<T> next)            //构造结点,data指定数据元素,next指定后继结点
    {
        this.data = data;                        //T对象引用赋值
        this.next = next;                        //Node<T>对象引用赋值
    }
    public Node()
    {
        this(null, null);
    }
    @Override
    public String toString()                     //返回结点数据域的描述字符串
    {
        return this.data.toString();
    }
}

2.单链表的简单操作(增加,删除,获取总长度,链表元素排序,链表遍历):
增加结点操作,addNode(Node):通过移动的指针遍历整个链表,找到最后一个结点,往后添加即可
插入结点到链表的指定位置。 insertNodeByIndex(int index,Node node)
删除指定位置上的结点  delNodeByIndex(int index)
代码如下:

/**
 * Created by lz50 on 2018/6/15.
 */
public class MyList {
    public static void main(String[] args) {
        //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
        Node<Integer> S1 = new Node<Integer>(20155325, null);
        Node<Integer> S2 = new Node<Integer>(20155326, null);
        Node<Integer> S3 = new Node<Integer>(20155328, null);
        Node<Integer> S4 = new Node<Integer>(20155329, null);
        //把上面四个节点连成一个没有头结点的单链表
        S1.next = S2;
        S2.next = S3;
        S3.next = S4;
        //遍历单链表,打印每个结点的
        Node<Integer> s = S1;
        while (s != null) {
            System.out.println(s.data);
            s = s.next;
        }
        System.out.println();
        //把你自己插入到合适的位置(学号升序)
        Node<Integer> M = new Node<Integer>(20155327, null);
        s = S1;
        while (s != null) {
            if (s.data < 20155327 && s.next.data > 20155327) {
                M.next = s.next;
                s.next = M;
                break;
            }
            else {
                s = s.next;
            }
        }
        System.out.println();
        //遍历单链表,打印每个结点的
        s = S1;
        while (s != null) {
            System.out.println(s.data);
            s = s.next;
        }
        System.out.println();
        //从链表中删除自己
        s = S1;
        while (s != null) {
            if (s.next.data == 20155327) {
                s.next = s.next.next;
                break;
            }
            else {
                s = s.next;
            }
        }
        System.out.println();
        //遍历单链表,打印每个结点的
        s = S1;
        while (s != null) {
            System.out.println(s.data);
            s = s.next;
        }
    }
}

截图如下:

 posted on 2018-05-06 18:04  20155327李百乾-  阅读(181)  评论(0编辑  收藏  举报