标签:支持 tle 类图 对象 percent equal 4.0 讲解 cep
一、实验内容
二、实验要求
~/Code
目录中用自己的学号建立一个目录,代码和UML图要放到这个目录中,截图中没有学号的会要求重做,然后跟着下面的步骤练习。三、实验步骤
(一)使用JUnit单元测试
1.产品代码
```
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 "错误";
}
}
```
2.测试代码
```
import junit.framework.TestCase;
import org.junit.Test;
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 testException(){
assertEquals("错误",MyUtil.percentage2fivegrade(-55));
assertEquals("错误",MyUtil.percentage2fivegrade(115));
}
@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));
}
}
```
3.测试截图
(二)TDD测试驱动开发
1.知识点
TDD的一般步骤如下:
2.产品代码
```
public class StringBufferDemo{
StringBuffer buffer = new StringBuffer();
public StringBufferDemo(StringBuffer buffer){
this.buffer = buffer;
}
public Character charAt(int i){
return buffer.charAt(i);
}
public int capacity(){
return buffer.capacity();
}
public int length(){
return buffer.length();
}
public int indexOf(String buf) {
return buffer.indexOf(buf);
}
}
```
3.测试代码
```
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());
}
}
```
4.测试截图
(三)对设计模式示例进行扩充,让系统支持Boolean类
1.产品代码
```
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();
}
}
```
2.测试截图
(四)以TDD的方式开发一个复数类Complex
1.产品代码
```
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 a) {
return new Complex(r + a.r, i + a.i);
}
public Complex ComplexSub(Complex a) {
return new Complex(r - a.r, i - a.i);
}
public Complex ComplexMulti(Complex a) {
return new Complex(r * a.r - i * a.i, r * a.i + i * a.r);
}
public Complex ComplexDiv(Complex a) {
return new Complex((r * a.i + i * a.r)/(a.i * a.i + a.r * a.r), (i * a.i + r * a.r)/(a.i * a.i + a.r * a.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;
}
}
```
2.测试代码
```
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(2, 2);
Complex c3 = new Complex(2,0);
@Test
public void testgetRealPart() throws Exception {
assertEquals(-1.1, Complex.getRealPart(-1.1));
assertEquals(6.0, Complex.getRealPart(6.0));
assertEquals(0.0, Complex.getRealPart(0.0));
}
@Test
public void testgetImagePart() throws Exception {
assertEquals(-1.1, Complex.getImagePart(-1.1));
assertEquals(6.0, Complex.getImagePart(6.0));
assertEquals(0.0, Complex.getImagePart(0.0));
}
@Test
public void testComplexAdd() throws Exception {
assertEquals("3.0+4.0i", c1.ComplexAdd(c2).toString());
assertEquals("3.0+2.0i", c1.ComplexAdd(c3).toString());
assertEquals("4.0+2.0i", c2.ComplexAdd(c3).toString());
}
@Test
public void testComplexSub() throws Exception {
assertEquals("-1.0", c1.ComplexSub(c2).toString());
assertEquals("-1.0+2.0i", c1.ComplexSub(c3).toString());
assertEquals("0.0+2.0i", c2.ComplexSub(c3).toString());
}
@Test
public void testComplexMulti() throws Exception {
assertEquals("-2.0+6.0i", c1.ComplexMulti(c2).toString());
assertEquals("2.0+4.0i", c1.ComplexMulti(c3).toString());
assertEquals("4.0+4.0i", c2.ComplexMulti(c3).toString());
}
@Test
public void testComplexComplexDiv() throws Exception {
assertEquals("0.75+0.75i", c1.ComplexDiv(c2).toString());
assertEquals("1.0+0.5i", c1.ComplexDiv(c3).toString());
assertEquals("1.0+1.0i", c2.ComplexDiv(c3).toString());
}
}
```
3.测试截图
(五)UML类图
三、实验过程中遇到的问题
1.JUnit安装好后,在进行第一项任务时,代码无法运行,且JUnit和Test部分显示都为红色。
解决:单机红色的JUnit和Test,点击出现的小灯泡,IDEA就会帮助配置来解决问题。
2.
四、实验心得与体会
这次实验我们主要学习了利用JUnit和TDD学习Java,对我来说难度很大。在测试的过程中,因为自己刚刚初次接触,所以即使老师的博客讲解非常详细,自己还是出现了很多错误,要重新开始。这次实验让我遇到了很多困难,但最后实验按照要求的完成,让我的能力有了很大的提高,也有了更多的学好Java这门课的自信。在以后的学习中,我会更加耐心,继续紧跟老师的步伐,来更好的完成每项学习任务,加油。
2018-2019-2 20175202实验二《Java面向对象程序设计》实验报告
标签:支持 tle 类图 对象 percent equal 4.0 讲解 cep
原文地址:https://www.cnblogs.com/gexvyang/p/10733222.html