zoukankan      html  css  js  c++  java
  • 20165232 第十周课下作业

    第十周课下作业

    相关知识点总结

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

    List<Student> list = new LinkedList<Student>();

    • 向链表中添加新的结点

    list.add(new Student(XXXXXXXXXXXXXXX));

    • 删除结点

    list.remove("xxxxxxx");

    • 链表中数据的插入

    list.add("**");

    • 链表中数据的排序

    Collections.sort();

    课上内容补做

    第一题代码

    import java.util.*;
    class Student implements Comparable {
        int height=0;
        String name;
        Student(String n,int h) {
            name=n;
            height = h;
    
        }
        public int compareTo(Object b) { // 两个Student对象相等当且仅当二者的height值相等
            Student st=(Student)b;
            return (this.height-st.height);
        }
    }
    public class Example15_4 {
        public static void main(String args[ ]) {
            List<Student> list = new LinkedList<Student>();
            list.add(new Student("张三",188));
            list.add(new Student("李四",178));
            list.add(new Student("周五",198));
            list.add(new Student("张羽昕",32));
            list.add(new Student("刘津甫",33));
            list.add(new Student("祁瑛",35));
            Iterator<Student> iter=list.iterator();
            System.out.println("排序前,链表中的数据");
            while(iter.hasNext()){
                Student stu=iter.next();
                System.out.println(stu.name+ "身高:"+stu.height);
            }
            Collections.sort(list);
            System.out.println("排序后,链表中的数据");
            iter=list.iterator();
            while(iter.hasNext()){
                Student stu=iter.next();
                System.out.println(stu.name+ "身高:"+stu.height);
            }
            Student zhaoLin = new Student("zhao xiao lin",178);
            int index = Collections.binarySearch(list,zhaoLin,null);
            if(index>=0) {
                System.out.println(zhaoLin.name+"和链表中"+list.get(index).name+"身高相同");
            }
        }
    }
    

    运行结果截图

    image

    第二题代码

    import java.util.*;
    public class Example {
        public  static void main(String[] args) {
            List<Student> list = new LinkedList<>();
            list.add(new Student(20165233,"张雨昕",99,91,89));
            list.add(new Student(20165234,"刘京甫",76,66,95));
            list.add(new Student(20165235,"祁 瑛",77,81,68));
            list.add(new Student(20165236,"郭金涛",89,45,66));
            list.add(new Student(20165237,"方若鸿",82,80,86));
            SortTotal_score sortBytotal_score = new SortTotal_score();
            Collections.sort(list, sortBytotal_score);
            SortID sortByID = new SortID();
            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 " "+name+" "+id+" "+total_score;
        }
    
        public Student(int id, String name,double computer_score,
                       double english_score,double maths_score) {
            this.id = id;
            this.name = name;
            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 SortID implements Comparator<Student> {
    
        @Override
        public int compare(Student a, Student b) {
            return a.getId() - b.getId();
        }
    
    }
    class SortTotal_score implements Comparator<Student> {
    
        @Override
        public int compare(Student a, Student b) {
            return (int)( a.getTotalScore() - b.getTotalScore());
        }
    }
    

    运行结果截图

    image

    第三题代码

    import java.util.*;
    public class Mylist {
        public static void main(String [] args) {
            //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
            Node<String> node1 =new Node<String>("20165233",null);
            Node<String> node2 =new Node<String>("20165234",null);
            Node<String> node3 =new Node<String>("20165236",null);
            Node<String> node4 =new Node<String>("20165237",null);
    
            //把上面四个节点连成一个没有头结点的单链表
            node1.next=node2;
            node2.next=node3;
            node3.next=node4;
            node4.next=node1;
    
            //遍历单链表,打印每个结点的
            Node<String> node =node1 ;
            System.out.println("打印插入前的链表数据");
            for (int i=0;i<4;i++) {
                System.out.println(node.data.toString());
                node = node.next;
            }
            //把你自己插入到合适的位置(学号升序)
            //遍历单链表,打印每个结点的
            System.out.println("打印插入后的链表数据");
            node=node1;
            Node<String> s=new Node<String>("20165235",null);
            for(int i=0;i<5;i++){
                System.out.println(node.data.toString());
                node = node.next;
               if(node==node2){
                   node2.next=s;
                   s.next=node3;
               }
            }
            //从链表中删除自己
            node=node1;
            for(int i=0;i<4;i++){
                node = node.next;
                if(node==s){
                    s=null;
                    node2.next=node3;
                    node3.next=node4;
                }
            }
           node =node1 ;
            System.out.println("打印删除后的链表数据");
            for (int i=0;i<4;i++) {
                System.out.println(node.data.toString());
                node = node.next;
            }
    
        }
    }
    
    
    class Node<String>                             //单链表结点类,T指定结点的元素类型
    {
        public String data;                               //数据域,存储数据元素
        public Node<String> next;                         //地址域,引用后继结点
    
        public Node(String data, Node<String> next)            //构造结点,data指定数据元素,next指定后继结点
        {
            this.data = data;                        //T对象引用赋值
            this.next = next;                        //Node<T>对象引用赋值
        }
        public Node()
        {
            this(null, null);
        }
        @Override
        public java.lang.String toString()                     //返回结点数据域的描述字符串
        {
            return this.data.toString();
        }
    }
    

    运行结果截图

    image

    教材代码分析

    15——1

    class Cone<E> { 
       double height;
       E bottom;           //用泛型类E声明对象bottom
       public Cone (E b) {
          bottom=b;   //给泛型类对象赋值
       }
       public void setHeight(double h) {
          height=h;
       }
       public double computerVolume() {
          String s=bottom.toString();//泛型变量只能调用从Object类继承的或重写的方法
          double area=Double.parseDouble(s); 
          return 1.0/3.0*area*height; 
       }
    }
    
    class Rect {
       double sideA,sideB,area; 
       Rect(double a,double b) {
         sideA=a;
         sideB=b;
       } 
       public String toString() {
          area=sideA*sideB;
          return ""+area;
       }
    }
    
    class Circle {
       double area,radius; 
       Circle(double r) {
          radius=r;
       } 
       public String toString() { //重写Object类的toString()方法
          area=radius*radius*Math.PI;
          return ""+area;
       }
    }
    
    public class Example15_1 {
       public static void main(String args[]) {
          Circle circle=new Circle(10);
          Cone<Circle> coneOne=new Cone<Circle>(circle);//创建一个(圆)锥对象  
          coneOne.setHeight(16);
          System.out.println(coneOne.computerVolume());
          Rect rect=new Rect(15,23);
          Cone<Rect> coneTwo=new Cone<Rect>(rect);//创建一个(方)锥对象
          coneTwo.setHeight(98); 
          System.out.println(coneTwo.computerVolume());
      }
    }
    

    15——2

    import java.util.*;
    public class Example15_2 {
       public static void main(String args[]){
          List<String> list=new LinkedList<String>();
          //定义一个空链表
          for(int i=0;i<=60096;i++){
                 list.add("speed"+i);
          }
          Iterator<String> iter=list.iterator();  //获取链表中的迭代器
          long starttime=System.currentTimeMillis();
          while(iter.hasNext()){
               String te=iter.next();
          }
          //进行遍历
          long endTime=System.currentTimeMillis();
          long result=endTime-starttime;
          System.out.println("使用迭代器遍历集合所用时间:"+result+"毫秒");
          starttime=System.currentTimeMillis();
          for(int i=0;i<list.size();i++){
              String te=list.get(i);
          }
          endTime=System.currentTimeMillis();
          result=endTime-starttime;
          System.out.println("使用get方法遍历集合所用时间:"+result+"毫秒");
        }
    }
    

    15——3

    import java.util.*;
    public class Example15_3 {
        public static void main(String args[]){
            LinkedList mylist=new LinkedList();//定义一个空链表
            mylist.add("你");                 //链表中的第一个节点
            mylist.add("好");                 //链表中的第二个节点
            int number=mylist.size();         //获取链表的长度
            for(int i=0;i<number;i++){
              String temp=(String)mylist.get(i); //必须强制转换取出的数据
              System.out.println("第"+i+"节点中的数据:"+temp);
            } 
            Iterator iter=mylist.iterator();
            while(iter.hasNext()) {
              String te=(String)iter.next();  //必须强制转换取出的数据
              System.out.println(te);
            }
       }
    }
    

    15——4

    import java.util.*;
    class Student implements Comparable { 
       int height=0;
       String name;
       Student(String n,int h) {
          name=n;
          height = h;
         
       }
       public int compareTo(Object b) { // 两个Student对象相等当且仅当二者的height值相等
         Student st=(Student)b;
         return (this.height-st.height);
       }
    }
    public class Example15_4 {
        public static void main(String args[ ]) { 
           List<Student> list = new LinkedList<Student>();
           //定义一个空链表
           list.add(new Student("张三",188));
           list.add(new Student("李四",178));
           list.add(new Student("周五",198)); 
           //向链表中依次添加元素
           Iterator<Student> iter=list.iterator();
           System.out.println("排序前,链表中的数据");
           while(iter.hasNext()){
              Student stu=iter.next();
              System.out.println(stu.name+ "身高:"+stu.height);
           }
           //对链表进行遍历
           Collections.sort(list);
           //进行升序排序
           System.out.println("排序后,链表中的数据");
           iter=list.iterator();
           while(iter.hasNext()){
              Student stu=iter.next();
              System.out.println(stu.name+ "身高:"+stu.height);
           }
           Student zhaoLin = new Student("zhao xiao lin",178);
           int index = Collections.binarySearch(list,zhaoLin,null);
           if(index>=0) {
                System.out.println(zhaoLin.name+"和链表中"+list.get(index).name+"身高相同");
           }
        }
    }
    

    15——5

    import java.util.*;
    public class Example15_5 {
        public static void main(String args[ ]) { 
           List<Integer> list = new LinkedList<Integer>();
           //定义一个空链表
           for(int i=10;i<=50;i=i+10)
               list.add(new Integer(i));
           System.out.println("洗牌前,链表中的数据");
           Iterator<Integer> iter=list.iterator();
           while(iter.hasNext()){
              Integer n=iter.next();
              System.out.printf("%d	",n.intValue());
           }
           //遍历链表
           Collections.shuffle(list);
           //按洗牌算法重新随机排列
           System.out.printf("
    洗牌后,链表中的数据
    ");
           iter=list.iterator();
           while(iter.hasNext()){
              Integer n=iter.next();
              System.out.printf("%d	",n.intValue());
           }
           System.out.printf("
    再向右旋转1次后,链表中的数据
    ");
           Collections.rotate(list,1);
           //旋转链表中的数据
           iter=list.iterator();
           while(iter.hasNext()){
              Integer n=iter.next();
              System.out.printf("%d	",n.intValue());
           }
          
        }
    }
    

    15——6

    import java.util.*;
    public class Example15_6 {
       public static void main(String args[]) {
          Stack<Integer> stack=new Stack<Integer>();
          //定义一个堆栈对象
          stack.push(new Integer(1)); 
          stack.push(new Integer(1));
          //进行压栈
          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=new Integer(f1+f2);
              System.out.println(""+temp.toString()); 
              stack.push(temp);
              stack.push(F2);
              k++;
            }
          } 
       }
    }
    

    课后习题

    第一题运行截图

    image

    第二题运行截图

    image

    第三题运行截图

    image

  • 相关阅读:
    二叉树遍历问题、时间空间复杂度、淘汰策略算法、lru数据结构、动态规划贪心算法
    Django--csrf跨站请求伪造、Auth认证模块
    Django--中间件
    Django--Cookie和Session组件
    Django--form表单组件
    安装配置flask环境
    Django--模型层
    Django--路由层、视图层、模版层
    Eclipse SVN文件冲突及不能直接提交情况
    Eclipse开发Web常见异常
  • 原文地址:https://www.cnblogs.com/heyanda/p/8999577.html
Copyright © 2011-2022 走看看