实验封面:
实验二 面向对象程序设计-1
实验要求:
参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST
参考http://www.cnblogs.com/rocedu/p/6736847.html
提交最后三个测试用例都通过的截图,截图上要有画图加水印,输入自己的学号。
实验截图:
相关代码:
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)); } }
实验二 面向对象程序设计-2
实验要求:
参考 积极主动敲代码,使用JUnit学习Java (http://www.cnblogs.com/rocedu/p/4837092.html)
参考http://www.cnblogs.com/rocedu/p/6736847.html 以 TDD的方式研究学习StringBuffer,提交你的单元测试用例和测试通过的截图,截图要加上学号水印。
实验截图:
相关代码:
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 junit.framework.TestCase; import org.junit.Test; public class StringBufferDemoTest extends TestCase { StringBuffer a = new StringBuffer("StringBuffer"); StringBuffer b = new StringBuffer("StringBufferStringBuffer"); StringBuffer c = new StringBuffer("StringBufferStringBufferStringBuffer"); StringBuffer d = new StringBuffer("StringBufferStringBufferStringBufferStr"); @Test public void testcharAt() { assertEquals('S',a.charAt(0)); assertEquals('e',b.charAt(10)); assertEquals('f',c.charAt(20)); assertEquals('B',d.charAt(30)); } @Test public void testcapacity() { assertEquals(28,a.capacity()); assertEquals(40,b.capacity()); assertEquals(52,c.capacity()); assertEquals(55,d.capacity()); } @Test public void testindexOf() { assertEquals(0,a.indexOf("Str")); assertEquals(3,b.indexOf("ing")); assertEquals(6,c.indexOf("Buffer")); assertEquals(8,d.indexOf("ff")); } @Test public void testlength() { assertEquals(12,a.length()); assertEquals(24,b.length()); assertEquals(36,c.length()); assertEquals(39,d.length()); } }
实验二 面向对象程序设计-3
实验要求:
实验二 Java面向对象程序设计(http://www.cnblogs.com/rocedu/p/4472842.html)
参考http://www.cnblogs.com/rocedu/p/6736847.html 对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式 用自己的学号%6进行取余运算,根据结果进行代码扩充:
0: 让系统支持Byte类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
1: 让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
2: 让系统支持Boolean类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
3: 让系统支持Long类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
4: 让系统支持Float类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
5: 让系统支持Double类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
实验截图:
我的学号是20165220,余数是2所以选择:让系统支持Boolean类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
相关代码:
abstract class Data{ public abstract void DisplayValue(); } class Integer extends Data { int value; Integer(){ value=100; } public void DisplayValue(){ System.out.println(value); } } class Boolean extends Data{ boolean value; Boolean(){ value=true; } public void DisplayValue(){ System.out.println(value); } } class Document { Data pd; Document() { pd=new Boolean(); } public void DisplayData(){ pd.DisplayValue(); } } public class MyDoc { static Document d; public static void main(String[] args) { d = new Document(); d.DisplayData(); } }
实验二 面向对象程序设计-4
实验要求:
提交:单元测试代码和运行成功截图及码云上代码链接,截图要加上学号水印 参考http://www.cnblogs.com/rocedu/p/6736847.html
任务:以TDD的方式开发一个复数类Complex,要求如下:
// 定义属性并生成getter,setter
double RealPart;
double ImagePart;
// 定义构造函数
public Complex()
public Complex(double R,double I)
//Override Object
public boolean equals(Object obj)
public String toString()
// 定义公有方法:加减乘除
Complex ComplexAdd(Complex a)
Complex ComplexSub(Complex a)
Complex ComplexMulti(Complex a)
Complex ComplexDiv(Complex a)
实验截图:
相关代码:
public class Complex{ private double r; private double i; public Complex(double r, double i) { this.r = r; this.i = i; } public static double getRealPart(double r) { return r; } public static double getImagePart(double i) { return i; } public Complex ComplexAdd(Complex c) { return new Complex(r + c.r, i + c.i); } public Complex ComplexSub(Complex c) { return new Complex(r - c.r, i - c.i); } public Complex ComplexMulti(Complex c) { return new Complex(r * c.r - i * c.i, r * c.i + i * c.r); } public Complex ComplexDiv(Complex c) { return new Complex((r * c.i + i * c.r)/(c.i * c.i + c.r * c.r), (i * c.i + r * c.r)/(c.i * c.i + c.r * c.r)); } public String toString() { String s = " "; if (i > 0) s = r + "+" + i + "i"; if (i == 0) s = r + ""; if (i < 0) s = r + " " + i + "i"; return s; } }
import junit.framework.TestCase; import org.junit.Test; public class ComplexTest extends TestCase { Complex c1 = new Complex(0, 3); Complex c2 = new Complex(-1, -1); Complex c3 = new Complex(2,1); @Test public void testgetRealPart() throws Exception { assertEquals(-1.0, Complex.getRealPart(-1.0)); assertEquals(5.0, Complex.getRealPart(5.0)); assertEquals(0.0, Complex.getRealPart(0.0)); } @Test public void testgetImagePart() throws Exception { assertEquals(-1.0, Complex.getImagePart(-1.0)); assertEquals(5.0, Complex.getImagePart(5.0)); assertEquals(0.0, Complex.getImagePart(0.0)); } @Test public void testComplexAdd() throws Exception { assertEquals("-1.0+2.0i", c1.ComplexAdd(c2).toString()); assertEquals("2.0+4.0i", c1.ComplexAdd(c3).toString()); assertEquals("1.0", c2.ComplexAdd(c3).toString()); } @Test public void testComplexSub() throws Exception { assertEquals("1.0+4.0i", c1.ComplexSub(c2).toString()); assertEquals("-2.0+2.0i", c1.ComplexSub(c3).toString()); assertEquals("-3.0 -2.0i", c2.ComplexSub(c3).toString()); } @Test public void testComplexMulti() throws Exception { assertEquals("3.0 -3.0i", c1.ComplexMulti(c2).toString()); assertEquals("-3.0+6.0i", c1.ComplexMulti(c3).toString()); assertEquals("-1.0 -3.0i", c2.ComplexMulti(c3).toString()); } @Test public void testComplexComplexDiv() throws Exception { assertEquals("-1.5 -1.5i", c1.ComplexDiv(c2).toString()); assertEquals("1.2+0.6i", c1.ComplexDiv(c3).toString()); assertEquals("-0.6 -0.6i", c2.ComplexDiv(c3).toString()); } }
实验二 面向对象程序设计-5
实验PSP:
步骤 | 耗时 | 百分比 |
需求分析 | 20min | 12.5% |
设计 | 40min | 25% |
实现 | 40min | 25% |
测试 | 20min | 12.5% |
分析总结 | 40min | 25% |
问题:
Q:不太了解@Test是什么意思?
A:@Test注解是JUnit测试的基础,它提供了其他作用
1.指定将会抛出的异常类型,如果没有抛出异常或者抛出的一场不属于我们指定的类型,就会算是测试失败了。
2.测试一断代码运行时间。
java中@Test的作用
实验总结:
这周感觉相对来说还是比较困难的,从一开始安装插件开始出错到后来跟着娄老师的博客慢慢解决,这一过程可谓说是非常艰辛+愉快了,这周渐渐地用Java结合实际来解决简单数学问题,感觉对Java有了新的兴趣与了解,但还是有许多要补充的地方,应该回头多复习前几章的内容,多巩固知识点。