# 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));
    }
}
  • 输出:
    1071573-20170517104053135-1678842369.png
  • 码云链接
    https://git.oschina.net/bestiisjava2017/java-besti-is-hwf/blob/master/src/Text/src/MathToolTest.java?dir=0&filepath=src%2FText%2Fsrc%2FMathToolTest.java&oid=f2a2d6e31260c33f492dea4792e96b6011d6e9af&sha=1c55a227e74f049846c11a5d9c26389cf029ec9f

    代码编程中的问题

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

    问题二

  • 设计并实现一个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;
    }

}
  • 截图
    1071573-20170517104105447-1838552975.png

  • 码云链接
    https://git.oschina.net/bestiisjava2017/java-besti-is-hwf/tree/master/src/Text/src?dir=1&filepath=src%2FText%2Fsrc&oid=91c6965369de2730e51bf3e3d676f60fac002f91&sha=dc0f73ee9bba9ea12f1eb7f5919b0bea276a6dc2

转载于:https://www.cnblogs.com/JIUSHA/p/6866340.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值