• java第五次作业


    java第五次作业

    (一)学习总结
    1.在上周完成的思维导图基础上,补充本周的学习内容,对Java面向对象编程的知识点做一个全面的总结。
    参考资料: XMind

    2.汽车租赁公司,出租汽车种类有客车、货车和皮卡三种,每辆汽车除了具有编号、名称、租金三个基本属性之外,客车有载客量,货车有载货量,皮卡则同时具有载客量和载货量。用面向对象编程思想分析上述问题,将其表示成合适的类、抽象类或接口,说明设计思路并画出类图。
    工具:PowerDesigner
    参考教程:UML 简介

    1,创建接口busisness得到汽车的编号,名称和租金,然后构造设置属性。
    2,分别设置类keche,huoche,pika ,得到zaikeliang,zaihuolang,zaikeliang和zaihuolaing ,并设置构造属性。
    3,测试类test,进行设置实例化并调用输出操作。

    3.阅读下面程序,分析代码是否能编译通过,如果不能,说明原因,并进行改正。如果能,列出运行结果

    interface Animal{    
        void breathe();
        void run();
        void eat();
    }
    class Dog implements Animal{
        public void breathe(){
            System.out.println("I'm breathing");
        }
        void eat(){
            System.out.println("I'm eating");
        }
    }
    public class Test{
        public static void main(String[] args){
            Dog dog = new Dog();
            dog.breathe();
            dog.eat();
        }
    }
    

    改正后的程序:

    interface Animal{    
            void breathe();
            void run();
            void eat();
        }
        class Dog implements Animal{
            public void breathe(){
                System.out.println("I'm breathing");
            }
    		public void run() {
    			System.out.println("I'm running");
    		}	
            public void eat(){
                System.out.println("I'm eating");
            }
    		
        }
        public class Test{
            public static void main(String[] args){
                Dog dog = new Dog();
                dog.breathe();
                dog.run();
                dog.eat();
            }
        }
    

    运行结果:

    I'm breathing
    I'm running
    I'm eating
    

    修改方案:
    接口Animal中定义了三个抽象方法 :

    void breathe();
    void run();
    void eat();  
    

    所以在定义接口实现类中要覆写这三个的抽象方法:
    public void breathe(){},public void run(){},public void eat(){}
    在test类中也要对这三个类对象进行实例化并调用:

            Dog dog = new Dog();
            dog.breathe();
            dog.run();
            dog.eat();
    

    4.运行下面的程序

    import java.util.Arrays;
    public class Test{
        public static void main(String[] args){
            String[] fruits = {"peach","banana","orange","apple"};
            Arrays.sort(fruits);
            for(int i = 0;i < fruits.length;i++)
            {
                System.out.println(fruits[i]);
            }
        }
    }
    

    程序输出的结果是升序排序的。查看String 类的源码,说明是如何实现的?如果现在希望对输出的结果进行降序排序,该如何处理?修改上述代码,实现按照字母顺序逆序排序。

    程序修改后:

    import java.util.Arrays;
        public class Test{
         public static void main(String[] args){
             String[] fruits = {"peach","banana","orange","apple"};
            Arrays.sort(fruits);
            System.out.println("正序:
    ");
            for(int i = 0;i < fruits.length;i++)
            {
                System.out.println(fruits[i]);
            }
            Arrays.sort(fruits);
            System.out.println("倒序:
    ");
            for(int i = fruits.length-1;i >= 0;i--)
            {
                System.out.println(fruits[i]);
            }
        }
    }
    

    运行结果:

    正序:
    
    apple
    banana
    orange
    peach
    倒序:
    
    peach
    orange
    banana
    apple
    

    5.其他需要总结的内容。
    (二)实验总结
    实验内容:
    1.某工厂生产各种音乐盒,客户无需知道音乐盒的制作过程,只需知道如何播放音乐盒即可。用简单工厂设计模式实现该过程:接口MusicBox具有方法play(),两个音乐盒类PianoBox,ViolinBox,MusicBoxFactory 产生MusicBox的实例。

    1,在设置加工类中使用:

    if("piano".equals(className)){
    			m=new PianoBox();
    		}
    		if("violin".equals(className)){
    			m=new ViolinBox();
    		}
    

    进行曲目的名称输入。
    2,在定义子类ViolinBox和PianoBox 中分别 覆写play()方法
    ViolinBox中:
    public void play(){System.out.println("演奏小提琴曲.");}
    3,测试类中:Scanner 方法进行输入字符串:piano和violin进行选择曲目并调用:

    Scanner input=new Scanner(System.in);
    	String s=input.next();
    	
    	m=Factory.getlnstance(s);
    	if(m!=null){
    		m.play();
    	}
    

    2.修改第三次作业的第一题,使用java.util.Date类表示职工的生日和参加工作时间,并将职工信息按照生日大小排序后输出。(分别用comparable和comparator实现)
    1,创建类PersonComparator进行比较,然而这个没搞懂啥意思于是就直接搬上去了。

       public int compare(Employee e1, Employee e2) { 
                if (e1.getBirthday() .compareTo (e2.getBirthday())==1 ) { 
                      return 1; 
               } else if (e1.getBirthday() .compareTo (e2.getBirthday())==-1 ) { 
                  return -1; 
              } else { 
                    return 0; 
             } 
      } 
    

    2,使用方法:new SimpleDateFormat("yyyy-mm-dd").parse("1999-3-15")对时间进行具体的输出。

    Employee[] e=new Employee[3];
    		try{
    			e[0]=new Employee("c酱","女",new SimpleDateFormat("yyyy-mm-dd").parse("1999-3-15"),new SimpleDateFormat("yyyy-mm-dd").parse("2017-1-25"));
    			e[1]=new Employee("l酱","男",new SimpleDateFormat("yyyy-mm-dd").parse("1987-8-15"),new SimpleDateFormat("yyyy-mm-dd").parse("2016-11-15"));
    			e[2]=new Employee("k酱","男",new SimpleDateFormat("yyyy-mm-dd").parse("1977-5-10"),new SimpleDateFormat("yyyy-mm-dd").parse("2015-9-16"));
    		}
    		catch(ParseException c){
    			c.printStackTrace();
    		}
    

    3,使用

    Arrays.sort(e,new PersonComparator()); 
    		for(int i=0;i<e.length;i++)                        
    		System.out.println(e[i].toString());
    		
    	}
    

    对时间进行升序排序。

    3.在案例宠物商店的基础上,实现以下功能:
    (1)展示所有宠物
    (2)购买宠物
    (3)显示购买清单

    没做完,已经实现选择购买单个宠物的功能,不能实现多个宠物的购买的功能(及计算总价格)。

    (三)代码托管(务必链接到你的项目)
    码云commit历史截图
    上传实验项目代码到码云,在码云项目中选择“统计-commits”,设置搜索时间段,搜索本周提交历史,并截图。

    https://git.oschina.net/hebau_cs15/java-cs02zt5.git

  • 相关阅读:
    sqlHelp.java
    IIS7.0 检测到在集成的托管管道模式下不适用的ASP.NET设置 的解决方法
    [转]安装程序在安装此软件包时遇到一个错误,这可能表示此软件包有错。错误码是29506
    单表中的sql语句
    网页::::无法访问请求的页面,因为该页的相关配置数据无效。
    <authentication mode="Windows"/>
    无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。这个错误很难判断错在哪里,刚开……
    [转] css实现透明度(兼容IE6、IE7、Firefox2.0)
    企业微信机器人消息发送
    阴阳历自动转换工具函数
  • 原文地址:https://www.cnblogs.com/zhaotong189800/p/6761442.html
Copyright © 2020-2023  润新知