# MathTool测试类课堂实践（5月17日）20155318

### 对P145 MathTool.java （码云链接）

#### 代码

1. 产品代码（同课本P145 MathTool.java）
public class mathTool {
public static int sum(int...numbers) {

int sum=0;
for(int number:numbers){
sum+=number;
}
return sum;
}
}

1. 测试正常情况
public void testNormal()throws Exception{
assertEquals(6,mathTool.sum(1,2,3));
if(mathTool.sum(a1)==15)
{
System.out.println("Passed!");
}
else{
System.out.println("Failed!");
}
if(mathTool.sum(a3)==-28){
System.out.println("Passed!");
}
else {
System.out.println("Failed!");
}
}

1. 测试边界情况 （我的理解是数组只有一个元素时，不知道理解的对不对。。。）
 public void testBoundary()throws Exception{
assertEquals(1,mathTool.sum(1));
assertEquals(0,mathTool.sum());
}

1. 完整代码（码云链接）

### 图书馆Book类 （码云链接）

#### 代码 （码云链接）

1. Book类
package exam20170517;
import java.lang.String;
import java.io.StringWriter;
import java.lang.Object;
public class Book {
public String name;
public String writer;
public String publisher;
public long date;

public Book(String name, String writer, String publisher, long date) {  //构造方法
this.setname(name);
this.setWriter(writer);
this.setPublisher(publisher);
this.setDate(date);
}

public String getname() {
return name;
}

public void setname(String name) {
this.name = name;
}

public String getWriter() {
return writer;
}

public void setWriter(String writer) {
this.writer = writer;
}

public String getPublisher() {
return publisher;
}

public void setPublisher(String publisher) {
this.publisher = publisher;
}

public long getDate() {
return date;
}

public void setDate(long date) {
this.date = date;
}

@Override
public String toString() {
return "|--书名：" + this.getname() + "\n\t" + "|--作者：" + this.getWriter() +
"\n\t" + "|--出版社：" + this.getPublisher() + "\n\t" + "|--出版日期：" + this.getDate();
}
@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.equals(book.name)) return false;
if (!writer.equals(book.writer)) return false;
return publisher.equals(book.publisher);

}
}


1. 书架（测试类，测试输出和比较两本书是否相同）
package exam20170517;
import java.lang.Object;
public class BookShelf {
public static void main(String[] args) {
Book b1 = new Book("Java程序设计", "林xx", "出版社1", 20110101);
System.out.println(b1);
Book b2 = new Book("数据结构", "王xx", "出版社2", 20120101);
System.out.println(b2);
Book b3 = new Book("密码学", "刘xx", "出版社3", 20130101);
System.out.println(b3);
Book b4 = new Book("Java程序设计", "林xx", "出版社1", 20110101);
System.out.println(b4);
System.out.print("书一和书二;");
System.out.println(b1.equals(b2));
System.out.print("书一和书四;");
System.out.println(b1.equals(b4));
}

}


#### 补充

posted @ 2017-05-17 10:34  lxy1997  阅读(117)  评论(0编辑  收藏  举报