实验报告封面
实验内容及步骤
实验一
1.试验要求:
参考 (http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST) 完成单元测试的学习。
提交最后三个JUnit测试用例(正常情况,错误情况,边界情况)都通过的截图,截图上要有画图加水印,输入自己的学号。
本提交点考查JUnit会不会使用,测试用例至少要包含正常情况,错误情况,边界情况的测试。
2.实验截图:
3.试验代码
public class MyUtil{
public static String percentage2fivegrade(int grade){
//如果成绩小于0,转成“错误”
if ((grade < 0))
return "错误";
//如果成绩小于60,转成“不及格”
else if (grade < 60)
return "不及格";
//如果成绩在60与70之间,转成“及格”
else if (grade < 70)
return "及格";
//如果成绩在70与80之间,转成“中等”
else if (grade < 80)
return "中等";
//如果成绩在80与90之间,转成“良好”
else if (grade < 90)
return "良好";
//如果成绩在90与100之间,转成“优秀”
else if (grade <= 100)
return "优秀";
//如果成绩大于100,转成“错误”
else
return "错误";
}
}
import org.junit.Test;
import junit.framework.TestCase;
public class MyUtilTest extends TestCase {
@Test
public void testNormal() {
assertEquals("不及格", MyUtil.percentage2fivegrade(55));
assertEquals("及格", MyUtil.percentage2fivegrade(65));
assertEquals("中等", MyUtil.percentage2fivegrade(75));
assertEquals("良好", MyUtil.percentage2fivegrade(85));
assertEquals("优秀", MyUtil.percentage2fivegrade(95));
}
@Test
public void testExceptions(){
assertEquals("错误", MyUtil.percentage2fivegrade(-55));
assertEquals("错误", MyUtil.percentage2fivegrade(120));
}
@Test
public void testBoundary(){
assertEquals("不及格", MyUtil.percentage2fivegrade(0));
assertEquals("及格", MyUtil.percentage2fivegrade(60));
assertEquals("中等", MyUtil.percentage2fivegrade(70));
assertEquals("良好", MyUtil.percentage2fivegrade(80));
assertEquals("优秀", MyUtil.percentage2fivegrade(90));
assertEquals("优秀", MyUtil.percentage2fivegrade(100));
}
}
实验二
1.实验要求:
参考 [积极主动敲代码,使用JUnit学习Java](http://www.cnblogs.com/rocedu/p/4837092.html
以TDD的方式研究学习StringBuffer
提交你的单元测试用例和测试通过的截图,截图要加上学号水印
测试自己会不会写JUnit测试用例
2.实验截图:
3.试验代码:
public class StringBufferDemo{
public static void main(String [] args){
StringBuffer buffer = new StringBuffer();
buffer.append('S');
buffer.append("tringBuffer");
// System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
// System.out.println(buffer.indexOf("tring12345"));
System.out.println("buffer = " + buffer.toString());
System.out.println(buffer.length());
}
}
import org.junit.Test;
import junit.framework.TestCase;
public class StringBufferDemoTest extends TestCase {
StringBuffer a =new StringBuffer ("abcdefghijkl");
StringBuffer b =new StringBuffer ("abcdefghijklabcdefghijkl");
StringBuffer c =new StringBuffer ("abcdefghijklabcdefghijklabcdefghijkl");
@Test
public void testcharAt() throws Exception{
assertEquals('a',a.charAt(0));
assertEquals('f',b.charAt(5));
assertEquals('l',b.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());
}
实验三
1.实验要求:
参考实验二 Java面向对象程序设计
对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式
用自己的学号%6进行取余运算,根据结果进行代码扩充:
0:让系统支持Byte类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
1:让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
2:让系统支持Boolean类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
3:让系统支持Long类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
4:让系统支持Float类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
5:让系统支持Double类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学
2.实验截图:
3.试验代码:
// Server Classes
abstract class Data {
abstract public void DisplayValue();
}
class Integer extends Data {
int value;
Integer() {
value=100;
}
public void DisplayValue(){
System.out.println (value);
}
}
// Pattern Classes
abstract class Factory {
abstract public Data CreateDataObject();
}
class IntFactory extends Factory {
public Data CreateDataObject(){
return new Integer();
}
}
class Short extends Data{
short value;
Short(){
value=127;
}
public void DisplayValue(){
System.out.println (value);
}
}
class ShortFactory extends Factory {
public Data CreateDataObject(){
return new Short();
}
}
//Client classes
class Document {
Data pd;
Document(Factory pf){
pd = pf.CreateDataObject();
}
public void DisplayData(){
pd.DisplayValue();
}
}
//Test class
public class MyDos {
static Document d;
public static void main(String[] args) {
d = new Document(new IntFactory());
d.DisplayData();
d = new Document(new ShortFactory());
d.DisplayData();
}
}
实验四
1.实验要求:
提交:单元测试代码和运行成功截图及码云上代码链接,截图要加上学号水印
任务:以TDD的方式开发一个复数类Complex,要求如下:
2.实验截图:
3.试验代码:
public class Complex {
public double x;
public double y;
public Complex(double x,double y){
this.x=x;
this.y=y;
}
public double getx(Complex a){
return a.x;
}
public double gety(Complex a){
return a.y;
}
Complex Complexadd(Complex a){
return new Complex(this.x+a.x,this.y+a.y);
}
Complex ComplexSub(Complex a){
return new Complex(this.x-a.x,this.y-a.y);
}
Complex ComplexMul(Complex a){
return new Complex(this.x*a.x-this.y*a.y,this.x*a.y+this.y*a.x);
}
Complex ComplexDiv(Complex a){
return new Complex
((this.x*a.x+this.y*a.y)/(a.x*a.x+a.y*a.y),(this.y*a.x-this.x*a.y)/(a.x*a.x+a.y*a.y));
}
public void inputComplex(Complex a){
System.out.println(a.getx(a));
System.out.println(a.gety(a));
}
}
import org.junit.Test;
import junit.framework.TestCase;
public class ComplexTest extends TestCase {
@Test
public void testadd(){
double x = 10;
double y = 30;
Complex a = new Complex(x, y);
double x2 = 20;
double y2 = 90;
Complex b = new Complex(x2, y2);
a = a.Complexadd(b);
System.out.println(a.x);
System.out.println(a.y);
}
@Test
public void testsub(){
double x = 10;
double y = 50;
Complex a = new Complex(x, y);
double x2 = 70;
double y2 = 60;
Complex b = new Complex(x2, y2);
a = a.ComplexSub(b);
System.out.println(a.x);
System.out.println(a.y);
}
@Test
public void testDiv(){
double x = 40;
double y = 40;
Complex a = new Complex(x, y);
double x2 = 10;
double y2 = 50;
Complex b = new Complex(x2, y2);
a = a.ComplexDiv(b);
System.out.println(a.x);
System.out.println(a.y);
}
@Test
public void testmul(){
double x = 20;
double y = 40;
Complex a = new Complex(x, y);
double x2 = 30;
double y2 = 50;
Complex b = new Complex(x2, y2);
a = a.ComplexMul(b);
System.out.println(a.x);
System.out.println(a.y);
}
}
试验五
1.试验要求
使用WhiteStarUML对实验二中的代码进行建模,发类图的截图,加上学号水印。
参考http://www.cnblogs.com/rocedu/p/6736847.html
类图中只少两个类。
2.试验截图
实验中出现的问题与解决方法
问题:
实验三种定义哦short类后,变量输入值过大
解决方法:
根据short 类的数值范围重新定义
PSP时间
步骤 | 耗时 | 百分比 |
---|---|---|
需求分析 | 30min | 18.75% |
设计 | 40min | 25% |
代码实现 | 30min | 18.75% |
测试 | 20min | 12.5% |
分析总结 | 40min | 25% |