答:
1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误。并分析输出结果。 1.2 另外一个包中的OutOfParentPackage.java,能否编译通过?提示什么错误?分析原因。 1.3 回答:如果为了访问到protected修饰的属性或方法应该怎么办?
1.1 答:不能够编译通过;Child类main方法中的c.getParenti()和getParenti()方法中的System.out.println(i);;为什么会出错呢?看一下Parent类中的属性,发现Parent中的i的属性是private;前面几节课的内容提到父类中属性为private的不能够被子类继承与引用;因此此处会出现编译错误。我们可以修改Parent中i的属性为protected。改正错误后的输出结果是:首先在Child类中创建两个对象分别是Parent类和Child类,c.getParenti();获得父类的i,并输出,所以第一个数是1;c.getParentj();中首先输出父类的j,输出自己的j值,接着输出Child类的geti(),接下来引用并输出父类的geti(),所以接着四个数依次是2、2、1、1;接着调用Other类中的showParentj(Parent p)方法依次输出2 1,同时无返回值;每次输出均回车换行。以下是编译器运行结果:
1 2 2 1 1 2 11.2 答:不能编译通过;编译器提示错误The type Parent is not visible。翻译该句提示:父类是不可见的类型。因为Parent类前没有访问权限词,所以应当在前面加上public指定词,即public class Parent。
1.3 答:protected修饰的属性和方法可以在类内部,子类,同一个包内的其他类访问,所以为了访问到protected的属性和方法,a、可以在类内部直接访问;b、可以将属性和方法为protected的类和其他类置于一个包中;c、可以使用继承的方法。
2.abstract进阶:阅读GuessGame抽象类的设计与使用源代码 2.1 Guess改造前代码很简单,而改造后的代码使用了抽象类、抽象方法看起来很复杂,那这样的改造到底有什么好处呢? 2.2 如果想将该游戏改造成图形界面,应该进行一些什么操作? 2.3 结合该例子,你觉得什么时候应该使用abstract? 2.4 重要:在这个例子中,变化的是什么,不变的是什么?尝试结合abstract、继承等概念进行说明。
2.1 答:改造前是完全由控制台输入,程序完全和控制台捆绑在一起。改造后是使用了抽象类,可以通过控制台输入,也通过对话框图形界面输入,不会局限于控制台。
2.2 答:设计一个图形界面类继承自抽象类。还没接触不了解图形界面的定义。但是可以与c语言相似使用函数,使用方法。
2.3 答:抽象类具有以下特征:
(1) 使用abstract关键词修饰类;
(2) 抽象类必须被继承;
(3) 抽象类不能够被实例化; 结合例子:我觉得类中的方法没有具体实现要用abstract修饰,比如:public abstract void print(String text); 、public abstract void println(String text);、public abstract int nextInt();这三个方法中都没有具体的实现,所以要用abstract修饰,同时在子类中会有相应的方法将之覆盖。与此同时该类也要用abstract修饰,否则会出现错误。该类必须被继承,abstract方法在子类中也要被覆盖。
2.4 答:变的是控制台,运行平台等,不变的是abstract类,它固定提供输入和输出,完成这个游戏。
3.Comparable与Comparator 3.1 描述Comparable接口的用途。为什么某个类实现了Comparable接口就可以直接使用Arrays.sort对其进行排序? 3.2 有了Comparable接口为什么还需要Comparator接口呢? 3.3 可选:使用匿名内部类、Lambda表达式实现PTA编程5-2。
3.1 答:首先看一下Comparable的源代码
public interface Comparable<T> { public int compareTo(T o); }我把其中的注释全部删除了;但是我们也可以从这一小段代码看出来Comparable接口只有一个方法comparaTo(),从方法名可以看出是一个比较器的名称。此接口强行对实现它的每个类的对象进行整体排序,Arrays.sort()方法的算法如下;算法使用了(Comparable)这是类型指派,也就是实现Comparable才能使用,否则就会出错。
if (length < INSERTIONSORT_THRESHOLD) { for (int i=low; i<high; i++) for (int j=i; j>low && ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--) swap(dest, j, j-1); return; } for(int i = destLow, p = low, q = mid; i < destHigh; i++) { if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0) dest[i] = src[p++]; else dest[i] = src[q++]; }3.2 答:Comparable接口只能按照一种规则排序;而Comparator接口则可以编写多个比较器去实现Comparable接口,并覆盖了Comparable中的comparaTo方法;每个比较器的排序规则均可不同,这样如果想对单一属性进行排列就更加便利。
4.面向接口案例分析 4.1 画出类关系图,描述每个类与接口的作用。 4.2 StudenDaoListImpl与StudentDaoArrayImpl有何区别?
4.1
答:
StudentDao接口:声明了三个方法,第一个方法判断是否成功写入写入学生信息;第二个方法读取学生信息;第三个方法显示学生信息;Student类:只有一个属性name,有相应的setter/getter,还有其构造函数,以及toString方法;StudentDaoArrayImpl类:实现StudentDao接口。设置一个长度为size的students数组;public boolean writeStudent输入一个名字,若students数组中有学生的name与输入的名字相等,则返回该学生,否则返回null;public Student readStudent输入一个学生,若数组中有空位(null),则把此学生加入students数组,并返回true,否则返回false;法三、输出全部的学生;StudenDaoListImpl类:实现StudentDao接口。创建了一个对象为Student的ArrayList,且命名为List;public Student readStudent输入一个名字,若List中的元素与输入名字相等,则返回该学生,否则返回null;public boolean writeStudent输入一个学生,加入List,并返回true;public void diplayAllStudent()输出List的全部学生;4.2 答:StudentDaoArrayImpl是用数组存放Student对象,这种方法有个弊端,一旦确定了数组长度就不能够修改,后期如果还想添加信息的化会溢出,同时如果前期存储学生数量过少会造成内存空间的浪费;StudentDaoArrayImpl则是用ArrayList来存储信息,这样的好处在后期添加学生信息有相应的方法,也不会有内存空间浪费的现象。
5.什么是面向接口编程?面向接口编程的好处是什么? 结合题目3与4中的Test.java的代码讨论分析。不要百度原封不动照搬!
答:接口实质上是一种特殊的抽象类,内部只有方法和属性的声明;接口内部的方法没有具体的实现;面向接口编程:在一个系统中,系统的各个功能需要不同对象合作完成。那么各个对象之间的协作关系就很关键。题三中的Comparable和Comparator两个接口,通过这两个接口,我们可以很方便地对自己定义的类进行比较;题四中StudentDao接口,StudenDaoListImpl是实现该接口的一个类,但类内部使用列表存储;StudentDaoArrayImpl是也实现该接口的一个类,类内部用数组存储;接口和两个实现接口类之间有一个小小的联系,其内部存储对象均是Student类;在题四中Student类就是几个类之间的一个协调关系。
6.结对编程:面向对象设计(大作业2-非常重要) 内容:使用Java代码完成上周做的面向对象设计大作业,需要有初步界面。实现的功能尽量简单,少而精,只包含必要的功能,不要追求高大全。 写出:类图(尽量精简,不用太多子类,两个即可)、系统常用功能描述、关键代码与界面 形式: 两人依托码云合作完成。请在这里贴出你们的学号、姓名与任务分工。 注意: 再过几次课要讲Java图形界面编程,到时候要将该系统升级为图形界面。系统的业务逻辑部分应该变化不大,变化大的是输入与输出部分。所以编码的时候,输入(Scanner)与输出(System.out)的代码,请不要将其与某个业务处理函数绑死。 选做加分: 给出两人在码云上同一项目的提交记录截图,额外加分。注:两个人在码云上新建一个项目。 参考资料: 结对编程参考资料 可以使用Processon画图
第6题提交作业的结构: 6.1
学生A学生B项目地址nullnull[码云地址]6.2 常用功能描述框架图 6.3 关键代码 6.4 运行界面
答:6.1
学生A学生B项目地址吴剑通null码云地址6.2 框架图:
6.3 关键代码:
package Shopping; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); ArrayList<ShoppingCar> Car=new ArrayList<ShoppingCar>(); Menu menu=new Menu(); User user=new User(); Goods[] goods=new Goods[15]; goods[0]=new Goods("篮球",100,new Nature("球类"),"001"); goods[1]=new Goods("足球",80,new Nature("球类"),"002"); goods[2]=new Goods("排球",80,new Nature("球类"),"003"); goods[3]=new Goods("足球",80,new Nature("球类"),"004"); goods[4]=new Goods("九分裤",120,new Nature("裤子"),"005"); goods[5]=new Goods("七分裤",120,new Nature("裤子"),"006"); goods[6]=new Goods("牛仔裤",120,new Nature("裤子"),"007"); goods[7]=new Goods("JAVA从入门到精通",220,new Nature("图书"),"008"); goods[8]=new Goods("乔布斯自传",220,new Nature("图书"),"009"); goods[9]=new Goods("数学之美",200,new Nature("图书"),"010"); goods[10]=new Goods("数字逻辑",180,new Nature("图书"),"011"); goods[11]=new Goods("离散数学",160,new Nature("图书"),"012"); goods[12]=new Goods("算法导论",200,new Nature("图书"),"013"); goods[13]=new Goods("JAVA入门",150,new Nature("图书"),"014"); goods[14]=new Goods("C++编程辅导",166,new Nature("图书"),"015"); menu.Menu1(); int n=sc.nextInt(); if (n==0) {sc.close();return;} System.out.println("用户名:"); String username=sc.next(); System.out.println("密码:"); String password=sc.next(); if (username.equals(user.Username)&&password.equals(user.Password)) { for (int i = 0; i < goods.length; i++) { System.out.println(goods[i]); } for (int i = 0; i < 100; i++) { System.out.println("请输入商品的id:"); String goods1=sc.next(); for (int j = 0; j < goods.length; j++) { if (goods1.equals(goods[j].id)) { ShoppingCar s=new ShoppingCar(goods[j].goodsname,goods[j].price,goods[j].nature,goods[j].id,sc.nextInt()); Car.add(s); } else continue; } System.out.println("是否继续添加商品?Yes?No?"); String str=sc.next(); if (str.equalsIgnoreCase("yes")) continue; if (str.equalsIgnoreCase("no")) break; } System.out.println("是否显示购物车?Yes?No?"); String str2=sc.next(); if (str2.equalsIgnoreCase("yes")){ Total total=new Total(); for (Goods good : Car) { System.out.println(good); } System.out.println("\n\t\t总价:"+ total.totalprice(Car)); } if (str2.equalsIgnoreCase("no")) { menu.Menu2(); sc.close(); return; } }else System.out.println("用户名密码不匹配!!!"); menu.Menu2(); sc.close(); } } class Menu{ void Menu1() { System.out.println("======================="); System.out.println("\n\t淘宝系统\n"); System.out.println("======================="); System.out.println("\n\t1、登陆淘宝\n"); System.out.println("\t0、退出淘宝\n\n"); System.out.println("======================="); } void Menu2(){ System.out.println("======================="); System.out.println("\n\t谢谢惠顾!"); System.out.println("\n\t欢迎下次继续光临!"); System.out.println("======================="); } } class User { String Username = "wujiantong"; String Password = "123456789"; public ShoppingCar car; public String getUsername() { return Username; } public void setUsername(String username) { Username = username; } public String getPassword() { return Password; } public void setPassword(String password) { Password = password; } @Override public String toString() { return "User [Username=" + Username + ", Password=" + Password + ", car=" + car + "]"; } } class Goods { String goodsname; double price; Nature nature; String id; int numble; public Goods(String goodsname, double price, Nature nature,String id) { this.goodsname = goodsname; this.price = price; this.nature = nature; this.id = id; } public String getGoodsname() { return goodsname; } public void setGoodsname(String goodsname) { this.goodsname = goodsname; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Nature getNature() { return nature; } public void setNature(Nature nature) { this.nature = nature; } public String getId() { return id; } public void setId(String id) { this.id = id; } @Override public String toString() { return "名称:"+this.goodsname+"\t\tID:"+this.id+"\t价格:"+this.price+"\t"+this.nature; } } class Nature { String name; public Nature(String name) { this.name = name; } @Override public String toString() { return "商品属性:" + name; } } class ShoppingCar extends Goods{ int numble; public ShoppingCar(String goodsname, double price, Nature nature, String id,int numble) { super(goodsname, price, nature, id); this.numble=numble; } @Override public String toString() { return "名称:"+this.goodsname+"\t\tID:"+this.id+"\t购买数量:"+this.numble+"\t总价格:"+this.price*this.numble; } } interface shop{ public double totalprice(ArrayList<ShoppingCar> car); } class Total implements shop{ public double totalprice(ArrayList<ShoppingCar> car){ double totalprice=0; for (ShoppingCar good : car) { totalprice = totalprice + good.getPrice()*good.numble; } return totalprice; } }6.4 答:
实验总结:
5-1. 编程时按实验要求编写,在编写comparaTo时要注意其返回值否则会出现排序与预期相反。还有就是对输入运用,以及输出格式的确认。5-2. 编程时按实验要求一步一步写,编写Comparator实现类时,要注意各个类的排序要求。转载于:https://www.cnblogs.com/wjt960310/p/6616745.html
