# 20155308 2016-2017-2《Java程序设计》课堂实践项目 5月17日

20155308 2016-2017-2《Java程序设计》课堂实践项目 5/17

本次因为git出现了问题,所以没有按时提交我的代码

问题一

  • 在IDEA中对P145 MathTool.java 使用JUnit进行单元测试,测试用例不少于三个,要包含正常情况,边界情况。

  • 提交测试代码和运行结果截图,加上学号水印,提交码云代码链接。
    -MathTool代码:

public class MathTool {
    public static int sum(int... numbers) {
        int sum = 0;
        for(int number : numbers){
            sum += number;
        }
        return sum;
    }
}

测试代码:

import junit.framework.TestCase;
import org.junit.Test;

import static org.junit.Assert.*;

public class MathToolTest extends TestCase {
    int[] numbers1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int[] numbers2 = {0};
    int[] numbers3 = {1,2,3,4,55};
    @Test
    public void testSum() {
        assertEquals(55, MathTool.sum(numbers1));
        assertEquals(0, MathTool.sum(numbers2));
        assertEquals(65, MathTool.sum(numbers3));
    }
}

代码编程中的问题

本次考察主要是注意边界值的测试,我进行了三组测试用例,分别对范围内的值和范围外的值进行了测试。

问题二

  • 设计并实现一个Book类,定义义成Book.java,Book 包含书名,作者,出版社和出版日期,这些数据都要定义getter和setter。定义至少三个构造方法,接收并初始化这些数据。覆盖(Override)toString方法,返回良好的含有多行的书的描述信息。覆盖equals方法,书名,作者,出版社和出版日期完全一致才说明两本书是一样的。

  • 创建一个测试类Bookshelf, 其中的main方法创建并更新几个Book对象。Book至少包含三本本学期教材内容。

  • Book.java

public class Book {
    private String name;
    private String author;
    private String printer;
    private int date;
    public Book(String name, String author, String printer, int date) {
        this.name = name;
        this.author = author;
        this.printer = printer;
        this.date = date;
    }
    public Book() {
        this("book","author","printer",20170517);
    }
    public Book(int date) {
        this("book","author","printer",date);
    }
    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }
    public String getPrinter() {
        return printer;
    }
    public int getDate() {
        return date;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public void setPrinter(String printer) {
        this.printer = printer;
    }
    public void setDate(int date) {
        this.date = date;
    }
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", printer='" + printer + '\'' +
                ", date=" + date +
                '}';
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (date != book.date) return false;
        if (name != null ? !name.equals(book.name) : book.name != null) return false;
        if (author != null ? !author.equals(book.author) : book.author != null) return false;
        return printer != null ? printer.equals(book.printer) : book.printer == null;
    }

}
  • Bookshelf
public class Book {
    private String name;
    private String author;
    private String printer;
    private int date;
    public Book(String name, String author, String printer, int date) {
        this.name = name;
        this.author = author;
        this.printer = printer;
        this.date = date;
    }
    public Book() {
        this("book","author","printer",20170517);
    }
    public Book(int date) {
        this("book","author","printer",date);
    }
    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }
    public String getPrinter() {
        return printer;
    }
    public int getDate() {
        return date;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public void setPrinter(String printer) {
        this.printer = printer;
    }
    public void setDate(int date) {
        this.date = date;
    }
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", printer='" + printer + '\'' +
                ", date=" + date +
                '}';
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (date != book.date) return false;
        if (name != null ? !name.equals(book.name) : book.name != null) return false;
        if (author != null ? !author.equals(book.author) : book.author != null) return false;
        return printer != null ? printer.equals(book.printer) : book.printer == null;
    }

}
posted @ 2017-05-17 10:44  郝文菲20155308  阅读(170)  评论(0编辑  收藏  举报