20155210潘滢昊 Java第二次试验
20155210潘滢昊 Java第二次试验
实验内容
学会JunitTest的使用
实验代码
MyUtilTest代码:
import org.junit.*;
import junit.framework.TestCase;
public class MyUtilTest extends TestCase {
@Test
public void testNormal(){
assertEquals("不及格",MyUtil.percentage2fivegrade(51));
assertEquals("及格",MyUtil.percentage2fivegrade(62));
assertEquals("中等",MyUtil.percentage2fivegrade(73));
assertEquals("良好",MyUtil.percentage2fivegrade(84));
assertEquals("优秀",MyUtil.percentage2fivegrade(95));
}
@Test
public void testExceptions(){
assertEquals("错误",MyUtil.percentage2fivegrade(-1));
assertEquals("错误",MyUtil.percentage2fivegrade(150));
}
@Test
public void testBoundary(){
assertEquals("不及格",MyUtil.percentage2fivegrade(50));
assertEquals("及格",MyUtil.percentage2fivegrade(60));
assertEquals("中等",MyUtil.percentage2fivegrade(70));
assertEquals("良好",MyUtil.percentage2fivegrade(80));
assertEquals("优秀",MyUtil.percentage2fivegrade(90));
assertEquals("优秀",MyUtil.percentage2fivegrade(100));
}
}
StringBufferTest代码:
import junit.framework.TestCase;
import org.junit.*;
import static org.junit.Assert.*;
public class StringBufferDemoTest extends TestCase {
StringBuffer a=new StringBuffer("StringBuffer");
StringBuffer b=new StringBuffer("StringBufferStringBuffer");
StringBuffer c=new StringBuffer("StringBufferStringBufferStringBuffer");
@Test
public void testcharAt() throws Exception{
assertEquals('t',a.charAt(1));
assertEquals('g',a.charAt(5));
assertEquals('r',a.charAt(11));
}
@Test
public void testcapacity() throws Exception{
assertEquals(28,a.capacity());
assertEquals(40,b.capacity());
assertEquals(52,c.capacity());
}
@Test
public void testlength() throws Exception{
assertEquals(12,a.length());
assertEquals(24,b.length());
assertEquals(36,c.length());
}
@Test
public void testindexOf() throws Exception{
assertEquals(0,a.indexOf("Str"));
assertEquals(5,a.indexOf("gBu"));
assertEquals(10,a.indexOf("er"));
}
}
ComplexTest代码:
import junit.framework.TestCase;
import org.junit.*;
public class ComplexTest extends TestCase {
@Test
public void testNormal() throws Exception{
Complex c=new Complex();
Complex c1=new Complex(4,5);
Complex c2=new Complex(6,7);
c1.Print();
c2.Print();
System.out.println("这两复数和为:");
System.out.println((c.jia(c1, c2).a+"+"+c.jia(c1, c2).b+"i").toString());
System.out.println("这两复数差为:");
System.out.println(c.jian(c1, c2).a+"+"+c.jian(c1, c2).b+"i");
System.out.println("这两复数乘积为:");
System.out.println(c.cheng(c1, c2).a+"+"+c.cheng(c1,c2).b+"i");
System.out.println("这两复数相除为:");
System.out.println(c.chu(c1, c2).a+"+"+c.chu(c1,c2).b+"i");
}
}
实验运行截图
以上传到云班课中
实验时间表
题目思考时间 | 代码编辑时间 | 代码调试时间 | |
---|---|---|---|
时间 | 60min | 60min | 30min |