zoukankan      html  css  js  c++  java
  • 实践

    第一题

    1.封装一个汽车类,包含String  name、int  speed属性,在测试类中实例化三个对象:c1,c2,c3,分别设置name为:“奥拓”,“宝马”,“奔驰”,速度分别设置为:100,200,300

    1)使用Map集合对象m1将这三个汽车类对象保存成key,然后将int型的汽车价钱作为值保存在m1的value中,上述三款汽车分别对应的价钱是10000,500000,2000000

    2)遍历m1的键,打印name属性

    3)通过合适的方法,求出m1中“宝马”的价格,并打印结果;

    4)经过折旧,所有汽车都降价到原来的80%,请打印降价后“宝马”的价格

    public class Test {
    
        public static void main(String[] args) {
            //在测试类中实例化三个对象,“奥拓”,“宝马”,“奔驰”,速度分别设置为:100,200,300
            Car c1 = new Car("奥拓", 100);
            Car c2 = new Car("宝马", 200);
            Car c3 = new Car("奔驰", 300);
            //使用Map集合对象m1将这三个汽车类对象保存成key,
            //然后将int型的汽车价钱作为值保存在m1的value中,上述三款汽车分别对应的价钱是10000,500000,2000000
            Map<Car,Integer> m1= new HashMap<>();
            m1.put(c1, 10000);
            m1.put(c2, 500000);
            m1.put(c3, 2000000);
            //遍历m1的键,打印name属性
            Set<Car> cars = m1.keySet();
            Iterator<Car> it = cars.iterator();
            while(it.hasNext()){
                System.out.println(it.next().getName());
            }
            //通过合适的方法,求出m1中“宝马”的价格,并打印结果;
            Set<Entry<Car, Integer>> en = m1.entrySet();
            for (Entry<Car, Integer> e : en) {
                if(e.getKey().getName().equals("宝马")){
                    System.out.println("宝马价格:"+e.getValue());
                }
            }
            //经过折旧,所有汽车都降价到原来的80%,请打印降价后“宝马”的价格
            Set<Entry<Car, Integer>> en1 = m1.entrySet();
            for (Entry<Car, Integer> ee : en1) {
                Integer price = ee.getValue();
                m1.put(ee.getKey(), (int)(price*0.8));//覆盖原来的m1中的数据
                if(ee.getKey().getName().equals("宝马")){
                    System.out.println("宝马折旧后价格:"+ee.getValue());
                }
            }
            
            
        }
    
    }

    创建用户类:

    2.1.创建一个Map集合对象,存储3个用户信息,用户信息包括用户名和年龄。

    2.2.要求集合中的元素按照年龄升序排序,遍历Map集合,在控制台打印输出用户信息。

    public class Test {
    
        public static void main(String[] args) {
            Map<User, Integer> m = new HashMap<>();
            m.put(new User("张三", 18), 18);
            m.put(new User("李四", 20), 20);
            m.put(new User("王五", 12), 12);
            m.put(new User("赵六", 30), 30);
            System.out.println("排序前:");
            //遍历
            Set<Entry<User, Integer>> es = m.entrySet();
            for (Entry<User, Integer> e : es) {
                System.out.println(e.getKey());
            }
            System.out.println("排序后:");
            //排序key
            Map<User, Integer> m1 = new TreeMap<>();
            m1.putAll(m);
            Set<Entry<User, Integer>> es1 = m1.entrySet();
            for (Entry<User, Integer> e : es1) {
                System.out.println(e.getKey());
            }
        }
    
    }

    有一个班采用民主投票方法推选班长,班长候选人共4位,每个人姓名及代号分别为 张三  1,李四  2,王五  3,赵六  4。程序操作员将每张选票上所填的代号(1、2、3、或4)循环输入电脑,输入数字0结束输入,然后将所有候选人的得票情况显示出来,并显示最终当选者的信息。

    public class Student implements Serializable, Comparable<Student> {
        private String name;
        private int id;
        private int num;// 票数
    
        public Student() {
            super();
        }
    
        public Student(String name, int id, int num) {
            super();
            this.name = name;
            this.id = id;
            this.num = num;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public int getNum() {
            return num;
        }
    
        public void setNum(int num) {
            this.num = num;
        }
    
        @Override
        public String toString() {
            return name + "," + id + "," + num;
        }
    
        @Override
        public int compareTo(Student o) {
            if (this.num > o.getNum()) {
                return 1;
            } else if (this.num < o.getNum()) {
                return -1;
            } else {
                return 0;
            }
        }
    
    }
    
    
    public class Test {
        //序列化
        public static  void  saveObj(Student s,String fname) throws FileNotFoundException, IOException{
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fname));
            out.writeObject(s);
            out.close();
        }
        //反序列化
        public static Student readObj(String fname) throws FileNotFoundException, IOException, ClassNotFoundException{
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(fname));
            Student s= (Student) in.readObject();
            in.close();
            return s;
        }
    
        public static void main(String[] args) throws Exception {
            // 实例化四个学生
            Student s1 = new Student("张三", 1, 0);
            Test.saveObj(s1, "zs");
            Student s2 = new Student("李四", 2, 0);
            Test.saveObj(s2, "ls");
            Student s3 = new Student("王五", 3, 0);
            Test.saveObj(s3, "ww");
            Student s4 = new Student("赵六", 4, 0);
            Test.saveObj(s4, "zl");
    //       序列化
            // 投票
            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("请输入班长候选人代号(数字0结束):");
                int id = sc.nextInt();
                if(id==0){
                    //读取四个学生 
                    Student zs1 =Test.readObj("zs");
                    Student zs2 =Test.readObj("ls");
                    Student zs3 =Test.readObj("ww");
                    Student zs4 =Test.readObj("zl");
                    System.out.println("1:"+zs1.getName()+"【"+zs1.getNum()+"票】");
                    System.out.println("2:"+zs2.getName()+"【"+zs2.getNum()+"票】");
                    System.out.println("3:"+zs3.getName()+"【"+zs3.getNum()+"票】");
                    System.out.println("4:"+zs4.getName()+"【"+zs4.getNum()+"票】");
                    List<Student> list = new ArrayList<>();
                    list.add(zs1);
                    list.add(zs2);
                    list.add(zs3);
                    list.add(zs4);
                    Collections.sort(list);
                    
                    System.out.println("投票最终结果:"+list.get(3).getName()+"同学,最后以"+list.get(3).getNum()+"票当选班长!");
                    break;
                }
                switch (id) {
                case 1:
                    Student stu = Test.readObj("zs");
                    stu.setNum(stu.getNum()+1);
                    Test.saveObj(stu, "zs");
                    break;
                case 2:
                    Student stu1 = Test.readObj("ls");
                    stu1.setNum(stu1.getNum()+1);
                    Test.saveObj(stu1, "ls");
                    break;
                case 3:
                    Student stu2 = Test.readObj("ww");
                    stu2.setNum(stu2.getNum()+1);
                    Test.saveObj(stu2, "ww");
                    break;
                case 4:
                    Student stu3 = Test.readObj("zl");
                    stu3.setNum(stu3.getNum()+1);
                    Test.saveObj(stu3, "zl");
                    break;
    
                default:
                    System.out.println("此选票无效,请输入正确的候选人代号!");
                    break;
                }
            }
            sc.close();
        }
    
    }
    View Code
  • 相关阅读:
    Reactive Extensions (Rx) 入门(5) —— Rx的事件编程
    Reactive Extensions (Rx) 入门(4) —— Rx的事件编程
    Reactive Extensions (Rx) 入门(3) —— Rx的事件编程
    Reactive Extensions (Rx) 入门(2) —— 安装 Reactive Extensions
    Reactive Extensions (Rx) 入门(1) —— Reactive Extensions 概要
    Xamarin NuGet 缓存包导致 already added : Landroid/support/annotation/AnimRes 问题解决方案
    Android 系统Action大全
    Xamarin Forms 实现发送通知点击跳转
    如何理解灰度发布
    推荐一款分布式微服务框架 Surging
  • 原文地址:https://www.cnblogs.com/taozizainali/p/10883320.html
Copyright © 2011-2022 走看看