(一)学习总结java
1.在上周完成的思惟导图基础上,补充本周的学习内容,对Java面向对象编程的知识点作一个全面的总结。
git
2.汽车租赁公司,出租汽车种类有客车、货车和皮卡三种,每辆汽车除了具备编号、名称、租金三个基本属性以外,客车有载客量,货车有载货量,皮卡则同时具备载客量和载货量。用面向对象编程思想分析上述问题,将其表示成合适的类、抽象类或接口,说明设计思路并画出类图。编程
设计思路:设计一个出租汽车类 interface接口定义编号、名称、租金三个的get方法,再分别设计一个客车、货车、皮卡三个类,客车除了三个公共属性外还有载客量属性,货车还有载货量属性,皮卡还有载客量和载货量属性,再设计一个汽车租赁公司。数组
类图:
app
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(); } }
不能。Dog继承Animal,因此要继承Animal类中的全部方法,Dog类的方法应为public的ui
正确结果为:this
I'm breathing I'm eating I'm running
正确程序为:.net
interface Animal{ void breathe(); void run(); void eat(); } class Dog implements Animal{ public void breathe(){ System.out.println("I'm breathing"); } public void eat(){ System.out.println("I'm eating"); } public void run(){ System.out.println("I'm running"); } } public class Test{ public static void main(String[] args){ Dog dog = new Dog(); dog.breathe(); dog.eat(); dog.run(); } }
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 类的源码,说明是如何实现的?若是如今但愿对输出的结果进行降序排序,该如何处理?修改上述代码,实现按照字母顺序逆序排序。
String类源码:
public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator(); private static class CaseInsensitiveComparator implements Comparator<String>, java.io.Serializable { // use serialVersionUID from JDK 1.2.2 for interoperability private static final long serialVersionUID = 8575799808933029326L; public int compare(String s1, String s2) { int n1 = s1.length(); int n2 = s2.length(); int min = Math.min(n1, n2); for (int i = 0; i < min; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (c1 != c2) { c1 = Character.toUpperCase(c1); c2 = Character.toUpperCase(c2); if (c1 != c2) { c1 = Character.toLowerCase(c1); c2 = Character.toLowerCase(c2); if (c1 != c2) { // No overflow because of numeric promotion return c1 - c2; } } } } return n1 - n2; } }
降序输出的程序为:
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 = fruits.length- 1 ;i >= 0;i--) { System.out.println(fruits[i]); } } }
(二)实验总结
实验内容:
1.程序设计思路:设计一个接口MusicBox具备方法play(),再设计两个音乐盒类分别为PianoBox,ViolinBox,对方法play()进行加工,在设计一个MusicBoxFactory定义接口对象经过工厂取得实例,对方法进行调用,最后设计一个Test类,由用户输入来选择播放哪一个音乐盒。
问题1:
public static MusicBox getInstance(String className){ MusicBox m=null; if("PianoBox".equals(className)){ m=new PianoBox(); } if("ViolinBox".equals(className)){ m=new ViolinBox(); } return m; }
缘由:在写className的时候卡了一段时间
解决方案:在写 MusicBox getInstance的时候忽略了MusicBox,后来屡次看书及根据提示找了出来进行改正
2.程序设计思路:在原有的程序上进行改动,将Date类去掉,用
import java.text.SimpleDateFormat; import java.util.Date;
来代替Date类,实现ComparaTo比较在Employee类中添加CompareTo方法用getTime()来比较日期的大小,实现Comparator比较创建一个EmployeeComparator类,定义两个对象用getTime()来进行比较.对Test类也进行改动,在实例化时日期为new SimpleDateFormat("yyyy-mm-dd").parse("1995-01-12"),对实例化完的对象数组调用排序的两个方法分别输出。
问题1:
public int compareTo(Employee o){ if(this.birth.getTime()>o.birth.getTime()){ return 1; }else if(this.birth.getTime()<o.birth.getTime()){ return -1; } else{ return 0; } }
缘由:忘记在接口写<>
解决方案:运行老是有错就仔细看书及课件,后来发现少写了,而后加上,就对了
问题2:
class EmployeeComparator implements Comparator<Employee>{ public int compare(Employee o1,Employee o2){ if(o1.getBirth().getTime()>o2.getBirth().getTime()){ return 1; } else if(o1.getBirth().getTime()<o2.getBirth().getTime()){ return -1; } else{ return 0; } } }
缘由:在写Comparator接口的时候不知道应该怎么写
解决方案:问同窗、看课件、看书后来知道应该先建一个类而后定义两个对象来比较
3.程序设计思路:先设计一个Pet接口用interface Pet来写,在里面定义每一个属性的get方法,再设计一个Cat类,分别定义属性编号、品种、颜色、年龄、价格和每一个属性的set、get方法,再设计一个Dog类,与Cat类基本相同,再定义一个PetShop类,来定义增长、查询、输出方法,最后再建一个Test类,对对象实例化,来实现展现全部宠物,用户购买宠物,显示列表清单操做。
问题1:
public Pet search(String no){ Pet p=null; for(int i=0;i<this.pets.length;i++){ if(this.pets[i].getNo().indexOf(no)!=-1){ p=this.pets[i]; } } return p; }
缘由:返回老是为空
解决方案:刚开始写的是判断用户输入的编号是否存在与对象数组当中,而后在判断又返回值在循环结束完以后仍是须要一个返回值,与判断的返回有冲突,判断的返回值并无起做用,返回的都为空,后来试着定义一个变量,将查找出的结果赋给该变量,而后返回变量,使返回不为空。
问题2:
System.out.println("请用户输入要购买几只:"); x[i]=in.nextInt();
缘由:输出的个数都为最后一个的数量
解决方案:起初设置的x为一个变量而非数组,而后在发现问题后将变量改成数组,运行正确。
(三)代码托管
码云commit历史截图