zoukankan      html  css  js  c++  java
  • 课下作业(选做)第十周

    课下作业(选做)第十周

    一 相关知识点总结

    创建一个空的链表

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

    向链表中添加新的结点

    list.add(new Student());

    删除结点

    list.remove("");

    链表中数据的插入

    list.add("");

    链表中数据的排序

    Collections.sort();

    将list中的元素按升序排序

    public static sort(List<E>list)

    二 课上内容补做

    排序
    import java.util.*;
    public class Example {
        public  static void main(String[] args) {
            List<Student> list = new LinkedList<>();
            list.add(new Student(20165217,"叶  佺",99,91,89));
            list.add(new Student(20165218,"赵冰雨",76,66,95));
            list.add(new Student(20165219,"王彦博",77,81,68));
            list.add(new Student(20165220,"葛宇豪",89,45,66));
            list.add(new Student(20165221,"谭  笑",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());
        }
    }
    
    

    单链表
    import java.util.*;
    public class Mylist {
        public static void main(String [] args) {
            //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
            Node<String> node1 =new Node<String>("20165217",null);
            Node<String> node2 =new Node<String>("20165218",null);
            Node<String> node3 =new Node<String>("20165220",null);
            Node<String> node4 =new Node<String>("20165221",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>("20165219",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();
        }
    }
    
    

    教材代码分析

    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());
      }
    }
    

    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+"毫秒");
        }
    }
    

    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);
            }
       }
    }
    

    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+"身高相同");
           }
        }
    }
    
    

    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());
           }
          
        }
    }
    

    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++;
            }
          } 
       }
    }
    

    import java.util.*;
    class Student implements Comparable {
       int english=0;
       String name;
       Student(int english,String name) {
          this.name=name;
          this.english=english;
       }
       public int compareTo(Object b) {
          Student st=(Student)b;
          return (this.english-st.english);
       }
    }
    public class Example15_8 {
      public static void main(String args[]) {
         TreeSet<Student> mytree=new TreeSet<Student>();
         //定义一个树集对象
         Student st1,st2,st3,st4;
         st1=new Student(90,"赵一");
         st2=new Student(66,"钱二");
         st3=new Student(86,"孙三");
         st4=new Student(76,"李四");
         mytree.add(st1);
         mytree.add(st2);
         mytree.add(st3);
         mytree.add(st4);
         //依次添加数据
         Iterator<Student> te=mytree.iterator();
         while(te.hasNext()) {
            Student stu=te.next();
            System.out.println(""+stu.name+" "+stu.english);
            //遍历并打印数据
         }
      }
    }
    

    课后编程题

    第一题

    import java.util.*;
    public class Series {
        public static void main(String args[]) {
            Stack<Integer> stack=new Stack<Integer>();
            stack.push(new Integer(3));
            stack.push(new Integer(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=new Integer(2*f1+2*f2);
                    System.out.println(""+temp.toString());
                    stack.push(temp);
                    stack.push(F2);
                    k++;
                }
            }
        }
    }
    

    第二题

    import java.util.*;
    public class bce {
        public static void main(String args[]) {
            TreeSet<Score> mytree = new TreeSet<>();
            List<Score> mylist = new LinkedList<Score>();
            mylist.add(new Score("赵一", 89));
            mylist.add(new Score("钱二", 99));
            mylist.add(new Score("孙三", 100));
            mylist.add(new Score("李四", 92));
            mylist.add(new Score("周五", 88));
            //添加节点
            Iterator<Score> score = mylist.iterator();
            System.out.println("排序之前的:");
            while (score.hasNext()) {
                Score sco = score.next();
                mytree.add(sco);
                System.out.println(sco.name + " " + sco.english);
            }
            //将链表中的成绩放入树集中
            System.out.println("排序之后的:");
            Iterator<Score> te = mytree.iterator();
            while (te.hasNext()) {
                Score stu = te.next();
                System.out.println("" + stu.name + " " + stu.english);
            }
        }
    }
    
        class Score implements Comparable {
            int english = 0;
            String name = "";
    
            Score(String name, int english) {
                this.name = name;
                this.english = english;
            }
    
            @Override
            public int compareTo(Object a) {
                Score b = (Score) a;
                return this.english - b.english;
            }
        }
    

    第三题

    import java.util.*;
    class Sort implements Comparable {
        double d=0;
        Sort (double d) {
            this.d=d;
        }
        @Override
        public int compareTo(Object b) {
            Sort st=(Sort) b;
            if((this.d-st.d)==0) {
                return -1;
            }
            else {
                return (int) ((this.d - st.d) * 1000);
            }
        }
    }
    class U {
        String name="";
        double speed,capacity;
        U(String name,double speed,double capacity) {
            this.name=name;
            this.capacity=capacity;
            this.speed=speed;
        }
    }
    public class PriceSort {
        public static void main(String args[ ]) {
            TreeMap<Sort,U>  treemap= new TreeMap<Sort,U>();
            String name[]={"A","B","C","D","E","F","G","H","I","J"};
            double speed[]={111,100,101,345,213,433,565,333,454,454};
            double capacity[]={4,10,32,64,128,34,68,120,112,1024};
            U u[]=new U[10];
            for(int k=0;k<u.length;k++) {
                u[k]=new U(name[k],speed[k],capacity[k]);
            }
            Sort key[]=new Sort[10] ;
            for(int k=0;k<key.length;k++) {
                key[k]=new Sort(u[k].speed);
            }
            for(int k=0;k<u.length;k++) {
                treemap.put(key[k],u[k]);
            }
            System.out.println("按speed排序:");
            Collection<U> collection=treemap.values();
            Iterator<U> iter=collection.iterator();
            while(iter.hasNext()) {
                U stu=iter.next();
                System.out.println("品牌"+stu.name+"speed:"+stu.speed);
            }
            treemap.clear();
            for(int k=0;k<key.length;k++) {
                key[k]=new Sort(u[k].capacity);
            }
            for(int k=0;k<u.length;k++) {
                treemap.put(key[k],u[k]);
            }
            System.out.println("按capacity排序:");
            collection=treemap.values();
            iter=collection.iterator();
            while(iter.hasNext()) {
                U stu=(U)iter.next();
                System.out.println("品牌"+stu.name+" capacity :"+stu.capacity);
            }
        }
    }
    

  • 相关阅读:
    WPF ObservableCollection,INotifyPropertyChanged
    WPF MainWindow的TopMost,Resizemode
    WPF WebBrowser抑制Suppress 弹出 脚本错误 对话框 但是样式改变 需要继续改善
    WPF MultiBinding,多值转化器IMultiValueConverter,自制调色板 palette
    WPF Slider设置整数
    ABAP-Generate dynpro动态屏幕
    ABAP-Generate subroutine
    ABAP-索引
    Java IO/NIO
    微分中值定理--小笔记
  • 原文地址:https://www.cnblogs.com/wyb-1998/p/8999380.html
Copyright © 2011-2022 走看看