20155229第十三周--课上实践
我使用的是虚拟机上的IDEA,运行起来很卡,在Idea上也无法直接git,需将代码粘贴到vi中才能git,时间上没把控好,所以现将实践内容写成博客。
一、p145 MathTool建立单元测试
在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;
}
}
给MathToolMath建立的单元测试的代码为:
import junit.framework.TestCase;
import junit.framework.TestResult;
import org.junit.jupiter.api.Test;
/**
* Created by fyjfyh on 17-5-17.
*/
public class MathToolTest extends TestCase {
@Test
public void testsum() throws Exception {
assertEquals(8,MathTool.sum(2,2,4));
assertEquals(10,MathTool.sum(1,2,3,4));
}
}
MathToolTest的码云链接:https://git.oschina.net/bestiisjava2017/fyh20155229/blob/c84abce8abc87febf446dbc874f730098d6aefd7/src/MathToolTest.java
- 图片如下:
二、实现一个Book类
设计并实现一个Book类,定义义成Book.java,Book 包含书名,作者,出版社和出版日期,这些数据都要定义getter和setter。定义至少三个构造方法,接收并初始化这些数据。覆盖(Override)toString方法,返回良好的含有多行的书的描述信息。覆盖equals方法,书名,作者,出版社和出版日期完全一致才说明两本书是一样的。
创建一个测试类Bookshelf, 其中的main方法创建并更新几个Book对象。Book至少包含三本本学期教材内容。
Book.java的代码
package Book;
public class Book {
private String name;
private String author;
private String press;
private int date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
public void abc(String name,String author,String press,int date){
Book a = new Book();
name="JAVA";
author = "abc";
press="清华";
date = 2015;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", press='" + press + '\'' +
", 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 press != null ? press.equals(book.press) : book.press == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (author != null ? author.hashCode() : 0);
result = 31 * result + (press != null ? press.hashCode() : 0);
result = 31 * result + date;
return result;
}
}
Bookshelf的代码
package Book;
public class Booklei {
public static void main(String[] args) {
Book book1 = new Book();
Book book2 = new Book();
Book book3 = new Book();
book1.setName("Java");
book1.setAuthor("abc");
book1.setPress("清华出版社");
book1.setDate(2015);
book2.setName("密码学");
book2.setAuthor("def");
book2.setPress("金城");
book2.setDate(2017);
book3.setName("汇编");
book3.setAuthor("xyz");
book3.setPress("人民");
book3.setDate(2016);
System.out.println(book1.toString());
System.out.println(book2.toString());
System.out.println(book3.toString());
}
}
- Book代码的连接: