201621123005《java程序设计》第五周学习总结

201621123005《Java程序设计》第五周实验总结


  1. 本周学习总结

1.1 写出你认为本周学习中比较重要的知识点关键词
接口、多态、inferface、has-a、Comparable、Comparator
1.2 尝试使用思维导图将这些关键词组织起来。

  1. 书面作业

1. 面向对象设计大作业(团队项目,2-3人)

内容:继续完善上次的大作业。

1.1 项目简介表格:
码云地址[https://gitee.com/wangluo1611/java201621123005.git][1]

学生 负责任务 博客地址
张凯艳 ShoppingCart类的添加删除结算功能的编写addSnack(),delSnack(),totalAllPrice() 张凯艳【本人】的博客链接
张艺琳 Main的编写,以及ShoppingCart类的查看订单函数编写showAll(); 张艺琳的博客链接
安晏菊 Snack类的编写,以及建立货品的数组 安晏菊的博客链接

1.2 系统常用功能框架图

1.3 系统总体类图

1.4 购物车、商品、系统关键代码截图(主要截取自己负责的部分)

public class Shoppingcart {
private Map<Integer,Item> map = new LinkedHashMap<Integer,Item>();
public void addSnack(Snack snack ) {
    int GoodId = snack.getNumber();
    if (map.containsKey(GoodId))
    {
        Item items = map.get(GoodId);
        items.setCount(items.getCount()+1);
    }
    else {
        map.put(GoodId,new Item(snack,1));
    }
}
public boolean delSnack(int number){//删除商品
    if(map.containsKey(number)){
        map.remove(number);
        return true;
    }
    return false;
}
public double totalAllPrice(){//商品总钱数
    double total=0;
    Collection<Item> goodsItems = map.values();
    Iterator<Item> iterator = goodsItems.iterator();
    while(iterator.hasNext()){
        Item goodsItem = iterator.next();
        double totalPrice=goodsItem.totalPrice();
        total += totalPrice;
    }
    return total;
}
public void showAll(){//查看订单信息
    
}
public boolean changeCart(int GoodId,int count){
    
}
public boolean delGoods(int GoodId){
    if (map.containsKey(GoodId)){
        map.remove(GoodId);
        return true;
    }
    return false;
}

public void clearCart(){
    map.clear();
}
}

1.5 其他:跟上次的系统比较,系统的设计等有何修改。其他感想。
上次只是有一个基本的框架,以自己学过的java知识只能想到用ArrayList来添加和删除商品,后来经过查书和借鉴百度运用Map来进行添加和删除。

2. abstract:阅读GuessGame抽象类的设计与使用源代码
2.1 Guess改造前代码很简单,而改造后的代码使用了抽象类、抽象方法,看起来更复杂,这样的改造到底有什么好处呢?

改造前:
import java.util.Scanner;

public class Guess {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int number = (int) (Math.random() * 10);
        int guess;
        
        do {
            System.out.print("猜数字(0 ~ 9):");
            guess = scanner.nextInt();
        } while(guess != number);
        
        System.out.println("猜中了...XD");
    }
}

改造之后:
public abstract class GuessGame {
    public void go() {
        int number = (int) (Math.random() * 10); 
        int guess;
        do {
            print("输入数字");
            guess = nextInt();
        } while(guess != number);
        println("猜对了");
    }
    
    public abstract void print(String text);
    public abstract void println(String text);
    public abstract int nextInt();

因为改造前的只能在控制台输入和输出,用抽象的方法·改造后便可以在控制台、图形界面进行。,
2.2 GuessGame(改造后).java中有抽象方法与非抽象方法,你觉得抽象类中什么样的方法应该声明为abstract,什么方法不需要声明为abstract直接实现即可。
当对于一个方法没有明确的定义时,可用abstract。
2.3 重要:在这个例子中,变化的是什么,不变的是什么?尝试结合abstract等概念进行说明。
可以变化的是输入输出的方式,不变的是这个抽象类,它的功能并没有变化,输入输出方式变化后使程序运行更灵活。

3. Comparable与Comparator

3.1 结合PTA 7-1中你的代码说明,为什么某个类实现了Comparable接口,就可以直接使用Arrays.sort对该类型的数组进行排序?
Comparable 是告诉我们需要按照哪个对象排序,即是根据年龄还是身高排序,而Arrays.sort是等确定以后再排序。即比较器是给出排序规则,而怎么排用sort实现。

3.2 结合PTA 7-2 中你的代码说明,有了Comparable接口为什么还需要Comparator接口呢?
因为Comparator相对于Comparable来说可以根据不同的排序要求进行排序,利用Comparable接口,用Arrays.sort排序时形式比较单一。
3.3 以前的作业Shape, Rectangle,Cirlce中,Shape类中什么方法应声明为abstract?说出原因。
计算面积和周长的方法应该声明为abstract,因为不同的图形计算周长和面积的方法是不同的,如果有不同图形的想要继承父类的方法就需要声明为abstract
3.4 有很多Shape类型对象如Rectangle、Circle。希望使用Arrays.sort对他们进行排序,请写出相应代码。并简述应在哪个类上实现Comparable接口比较好?

abstract class Shape implements Comparable<Shape>
{
    final double PI = 3.14;
    abstract public double getArea();
    abstract public double getPerimeter();
    public int compareTo(Shape o) {
        if (this.getArea() > o.getArea())
            return 1;
        else if (this.getArea() < o.getArea())
            return -1;
        else
        {
            if (this.getPerimeter()>o.getPerimeter())
                return 1;
            else if (this.getPerimeter() < o.getPerimeter())
                return -1;
            else
                return 0;
        }


    }
}

在父类Shape实现Comparable比较好。因为这样可以使它的子类的周长和面积实现排序。
3.5 以3.4你编写的代码为例,简述面向Comparable接口编程、面向父类编程的好处。(注意:一定要结合自己编写的代码)
使用Comparable接口,我们可以根据它提供的比较方法,重写我们想要比较的元素的方法,可以使编程更加方便,简洁,灵活。

4. 面向接口案例分析
阅读Case-StudentDao.zip案例
4.1 a.画出类关系图。b.StudentDao接口有什么用?
在案例中StudentDao接口提供的public boolean writeStudent(Student student);
public Student readStudent(String name); public void diplayAllStudent();
作为共有的方法,可以让这些方法被调用。
4.2 StudenDaoListImpl与StudentDaoArrayImpl有何共同之处?有何区别?
StudenDaoListImplStudentDaoArrayImpl都继承了StudentDao接口,但StudenDaoListImpl使用动态数组来存放学生信息,而StudentDaoArrayImpl使用数组存放学生信息。
4.3 结合Test.java中的main函数,简述面向接口编程的好处。
面向接口编程比较简便,灵活。结构清晰,我们只需要定义一些方法,然后在当需要时实现这些方法。
5. 什么是面向接口编程?面向接口编程的好处是什么?
面向接口编程是先去定义一些方法,然后在当需要时几成并实现这些方法。面向接口编程比较简便,灵活。结构清晰
3.码云及PTA

题目集:面向对象2-进阶-多态接口内部类

3.1. 码云代码提交记录
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

3.2 截图PTA题集完成情况图

3.3 统计本周完成的代码量

周次 总代码量 新增代码量 总文件数 新增文件数
2 381 381 12 5
3 661 280 19 7
4 974 313 24 5
5 1358 384 33 9

posted on 2017-10-21 18:05  网路1611张凯艳  阅读(273)  评论(3编辑  收藏  举报

导航