20165233 第十周课下补做

20165233 第十周课下补做

相关知识点总结&代码与结果补充

课上习题2

  • 数据结构和算法中,两种重要的排序方法:

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

List<Student> list = new LinkedList<Student>();
  • 向链表中添加新的结点
list.add(new Student(5233,"张雨昕","female",20));
  • 代码实现
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.LinkedList;
public class ComparatorUse {
    public  static void main(String[] args) {
        List<Student> list = new LinkedList<>();
        list.add(new Student(5233,"张雨昕","female",20,80,67,89));
        list.add(new Student(5232,"何彦达","male",20,79,66,45));
        list.add(new Student(5235,"祁瑛","male",20,75,90,78));
        list.add(new Student(5231,"王杨鸿永","male",20,69,56,76));
        list.add(new Student(5234,"刘津甫","male",20,82,80,83));
        System.out.println("排序前的数据:");
        for (Student student : list) {
            System.out.println(student);
        }
        SortByID sortByID = new SortByID();
        Collections.sort(list, sortByID);
        System.out.println("根据学生学号由低到高排序:");
        for (Student student : list) {
            System.out.println(student);
        }
        SortByTotal_score sortBytotal_score = new SortByTotal_score();
        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 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 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());
    }
}
  • 结果截图

课上习题3

  • 有关单链表的创建以及新结点的添加知识点,见上一道习题知识点总结。

  • 把结点连成一个没有头结点的单链表:链表中的结点是自动链接在一起的,不需要我们做链接,也就是说,不需要操作安排结点中所存放的下一个或上一个结点的引用。

  • 遍历链表,利用迭代器

Iterator<String> iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
  • 按照学号进行升序排序:利用public static sort(List<E>list)方法,可以将list中的元素按升序排序。

  • 删除结点

list.remove("20165233");
  • 代码实现
import java.util.*;
public class MyList {
    public static void main(String [] args) {
        List<String> list=new LinkedList<String>();
        list.add("20165231");
        list.add("20165232");
        list.add("20165234");
        list.add("20165235");
        System.out.println("打印初始链表");
        //把上面四个节点连成一个没有头结点的单链表
        Iterator<String> iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍历单链表,打印每个结点的
        list.add("20165233");
        //把你自己插入到合适的位置(学号升序)
        System.out.println("插入我的学号后排序,打印链表");
        Collections.sort(list);
        iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍历单链表,打印每个结点的
        list.remove("20165233");
        //从链表中删除自己
        System.out.println("删除我的学号后打印链表");
        iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍历单链表,打印每个结点的
    }
}
  • 运行结果截图

第十五章课后编程题

第1题:

使用堆栈结构输出an的若干项,其中an=2an-1+2an-2,a1=3,a2=8.

  • 代码实现
import java.util.*;
public class Ep15_1 {
    public static void main(String[] args) {
        Stack<Integer> stack=new Stack<Integer>();
        stack.push(3);
        stack.push(8);
        int k=1;
        while (k<=10) {
            for (int i=1;i<=2;i++) {
                Integer F1=stack.pop();
                int f1=F1.intValue();
                Integer F2=stack.pop();
                int f2=F2.intValue();
                Integer temp= 2 * f1 + 2 * f2;
                System.out.println(""+temp.toString());
                stack.push(temp);
                stack.push(F2);
                k++;
            }
        }
    }
}
  • 运行结果截图

第2题:

将链表中的学生英语成绩单存放到一个树集中,使得按成绩自动排序,并输出排序结果

  • 代码实现
import java.util.*;
class CollegeStu implements Comparable {
    int english=0;
    String name;
    CollegeStu(int english,String name) {
        this.name=name;
        this.english=english;
    }
    @Override
    public int compareTo(Object b) {
        CollegeStu stu=(CollegeStu)b;
        return (this.english-stu.english);
    }
}
public class Ep15_2 {
    public static void main(String[] args) {
        List<CollegeStu> list=new LinkedList<CollegeStu>();
        int score []={67, 66, 90, 56, 80};
        String name []={"王杨鸿永","何彦达","张雨昕","刘津甫","祁瑛"};
        for (int i=0;i<score.length;i++) {
            list.add(new CollegeStu(score[i],name[i]));
        }
        Iterator<CollegeStu> iter=list.iterator();
        TreeSet<CollegeStu> mytree=new TreeSet<CollegeStu>();
        while (iter.hasNext()) {
            CollegeStu stu=iter.next();
            mytree.add(stu);
        }
        Iterator<CollegeStu> te=mytree.iterator();
        while (te.hasNext()) {
            CollegeStu stu=te.next();
            System.out.println(""+stu.name+" "+stu.english);
        }
    }
}
  • 运行结果截图

第3题:

有10个U盘,有两个重要的属性:价格和容量,编写一个应用程序,使用TreeMap<K,V>类,分别按照价格和容量排序输出10个U盘的详细信息。

  • 代码实现
import java.util.*;
class UDiscKey implements Comparable {
    double key = 0;

    UDiscKey(double d) {
        key = d;
    }

    @Override
    public int compareTo(Object b) {
        UDiscKey disc = (UDiscKey) b;
        if ((this.key - disc.key) == 0) {
            return -1;
        } else {
            return (int) ((this.key - disc.key) * 1000);
        }
    }
}
    class UDisc {
        int amount;
        double price;
        UDisc(int m,double e) {
            amount=m;
            price=e;
        }
}
public class Ep15_3 {
    public static void main(String[] args) {
        TreeMap<UDiscKey,UDisc> treeMap=new TreeMap<UDiscKey,UDisc>();
        int amount[]={1,2,4,8,16};
        double price[]={867,266,390,556};
        UDisc UDisc[]=new UDisc[4];
        for (int k=0;k<UDisc.length;k++) {
            UDisc[k]=new UDisc(amount[k],price[k]);
        }
        UDiscKey key[]=new UDiscKey[4];
        for (int k=0;k<key.length;k++) {
            key[k]=new UDiscKey(UDisc[k].amount);
        }
        for (int k=0;k<UDisc.length;k++) {
            treeMap.put(key[k],UDisc[k]);
        }
        int number=treeMap.size();
        Collection<UDisc> collection=treeMap.values();
        Iterator<UDisc> iter=collection.iterator();
        while (iter.hasNext()) {
            UDisc disc=iter.next();
            System.out.println(""+disc.amount+"G "+disc.price+"元");
        }
        treeMap.clear();
        for (int k=0;k<key.length;k++) {
            key[k]=new UDiscKey(UDisc[k].price);
        }
        for (int k=0;k<UDisc.length;k++) {
            treeMap.put(key[k],UDisc[k]);
        }
        number=treeMap.size();
        collection=treeMap.values();
        iter=collection.iterator();
        while (iter.hasNext()) {
            UDisc disc=iter.next();
            System.out.println(""+disc.amount+"G "+disc.price+"元");
        }
    }
}
  • 运行结果截图

posted @ 2018-05-05 11:22  minchan0624  阅读(488)  评论(1编辑  收藏  举报