2018-2019-2 20175215 实验二《面向对象程序设计》实验报告
一、实验内容及步骤
1.单元测试
-
(1)安装JUnit
点击File
,然后点击setting
搜索框中输入JUnit
后选择JunitGenerator V2.0
安装完成之后重启IDEA就可以了,安装成功之后的效果如图所示:
-
(2)TDD
TDD的编码节奏是:- 增加测试代码,JUnit出现红条
- 修改产品代码
- JUnit出现绿条,任务完成
2.任务一:在一个MyUtil类中解决一个百分制成绩转成“优、良、中、及格、不及格”五级制成绩的功能。
- 产品代码:
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(105));
}
@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));
}
}
- 系统反馈结果,如果测试失败,IDEA会指出具体哪个测试用例出现错误,修改代码直至所有测试均通过
测试成功截图
3.任务二:以TDD的方式研究学习StringBuffer
- 老师给出的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("tring"));
System.out.println("buffer = " + buffer.toString());
}
}
- 对代码进行改写,使得其可以进行JUnit测试
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);
}
}
- 各方法的作用如下:
- charAt(int i):得到字符串中第i个位置的字符,考虑到数组下标从0开始,字符串的位置也从0开始记。
- capacity():返回当前容量。容量指可用于最新插入的字符的存储量,超过这一容量就需要再次进行分配。
- length():得到字符串长度
- indexOf(String buf):得到buf字符串第一次出现的位置,该位置为buf中第一个字符的位置
- 测试代码:
import junit.framework.TestCase;
import org.junit.Test;
public class StringBufferDemoTest extends TestCase {
StringBuffer a1 = new StringBuffer("StringBuffer");
StringBuffer a2 = new StringBuffer("StringBufferStringBuffer");
StringBuffer a3 = new StringBuffer("StringBuffer used by 20175215");
@Test
public void testCharAt() throws Exception{//验证返回是否是整个字符串中的第x个字符
assertEquals('S',a1.charAt(0));
assertEquals('t',a2.charAt(13));
assertEquals('b',a3.charAt(18));
}
@Test
public void testcapacity() throws Exception{//验证容量
assertEquals(28,a1.capacity());
assertEquals(40,a2.capacity());
assertEquals(45,a3.capacity());
}
@Test
public void testlength() throws Exception{//验证字符串的长度
assertEquals(12,a1.length());
assertEquals(24,a2.length());
assertEquals(29,a3.length());
}
@Test
public void testindexOf(){//验证所在位置
assertEquals(6,a1.indexOf("Buff"));
assertEquals(1,a2.indexOf("tring"));
assertEquals(25,a3.indexOf("5215"));
}
}
测试成功截图
4.任务三:对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式
我的学号%6进行取余运算的结果(15%6
)为3, 让系统支持Long类,并在MyDoc类中添加测试代码表明添加正确。
引用20175308杨元同学关于S.O.L.I.D原则的笔记
依据OCP原则和DIP原则,我们在扩充示例使其支持Long类时不应该直接添加或修改已有类中的方法(OCP原则要求软件实体对修改封闭),而是采用一种抽象工厂
的方法来进行对于程序的扩充。具体代码如下:
abstract class Data {
abstract public void DisplayValue();
}
class Integer extends Data {
int value;
Integer() {
value=100;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Long extends Data{
long value;
Long(){
value=1234567890;
}
public void DisplayValue(){
System.out.println(value);
}
}
abstract class Factory {
abstract public Data CreateDataObject();
}
class IntFactory extends Factory {
public Data CreateDataObject(){
return new Integer();
}
}
class LongFactory extends Factory{
public Data CreateDataObject(){
return new Long();
}
}
class Document {
Data pd;
Document(Factory pf){
pd = pf.CreateDataObject();
}
public void DisplayData(){
pd.DisplayValue();
}
}
//Test class
public class MyDoc {
static Document d;
static Document e;
public static void main(String[] args) {
d = new Document(new IntFactory());
d.DisplayData();
e=new Document(new LongFactory()); //20175215
e.DisplayData();
}
}
运行成功截图:
5.任务四:以TDD的方式开发一个复数类Complex。
参考Java DecimalFormat 用法(数字格式化),加入import java.text.DecimalFormat;
进行编程。
- 测试代码:
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
Complex a1 =new Complex(3.0,4.0);
Complex a2 =new Complex( 2.0,-4.0);
Complex a3 =new Complex(0.0,0.0);
Complex a4 =new Complex(-3.0,0.0);
Complex a5 =new Complex(-6.0,-0.8);
@Test
public void testgetRealPart()throws Exception{
assertEquals(3.0,a1.getReal());
assertEquals(2.0,a2.getReal());
assertEquals(0.0,a3.getReal());
assertEquals(-3.0,a4.getReal());
assertEquals(-6.0,a5.getReal());
}
@Test
public void testgetImagePart()throws Exception{
assertEquals(4.0,a1.getImaginary());
assertEquals(-4.0,a2.getImaginary());
assertEquals(0.0,a3.getImaginary());
assertEquals(0.0,a4.getImaginary());
assertEquals(-0.8,a5.getImaginary());
}
@Test
public void testtoString()throws Exception{
assertEquals("3.0+4.0i",a1.toString());
assertEquals("2.0-4.0i",a2.toString());
assertEquals("0",a3.toString());
assertEquals("-3.0",a4.toString());
assertEquals("-6.0-0.8i",a5.toString());
}
@Test
public void testComplexAdd()throws Exception{
assertEquals("5.0",a1.ComplexAdd(a2).toString());
assertEquals("2.0-4.0i",a2.ComplexAdd(a3).toString());
assertEquals("-3.0",a3.ComplexAdd(a4).toString());
}
@Test
public void testComplexSub()throws Exception{
assertEquals("1.0+8.0i",a1.ComplexSub(a2).toString());
assertEquals("-2.0+4.0i",a3.ComplexSub(a2).toString());
assertEquals("3.0",a3.ComplexSub(a4).toString());
}
@Test
public void testComplexMulti()throws Exception{
assertEquals("22.0-4.0i",a1.ComplexMulti(a2).toString());
assertEquals("0",a2.ComplexMulti(a3).toString());
assertEquals("18.0+2.4i",a4.ComplexMulti(a5).toString());
}
@Test
public void testComplexDiv()throws Exception{
assertEquals("-0.2-1.2i",a1.ComplexDiv(a2).toString());
assertEquals("0",a3.ComplexDiv(a2).toString());
}
@Test
public void testequals()throws Exception{
assertEquals(true,a1.equals(a1));
assertEquals(false,a1.equals(a2));
}
}
- 产品代码
import java.text.DecimalFormat;//引入DecimalFormat包取一位整数和一位小数
public class Complex {
double Real=0;
double Imaginary=0;
public Complex(){}
public Complex(double Real,double Imaginary){
this.Real=Real;
this.Imaginary=Imaginary;
}
public double getReal(){
return Real;
}
public double getImaginary(){
return Imaginary;
}
public String toString(){
String s = "";
double r=Real;
double i=Imaginary;
if(r==0&&i==0){
s="0";
}
else if(r==0&&i!=0){
s=i+"i";
}
else if(r!=0&&i<0){
s=r+""+i+"i";
}
else if(r!=0&&i==0){
s=r+"";
}
else
{
s=r+"+"+i+"i";
}
return s;
}
public boolean equals(Object obj){//重写equals方法,使其不用来对比字符序列
if(this==obj){
return true;
}
else
return false;
}
DecimalFormat df = new DecimalFormat( "0.0");
public Complex ComplexAdd(Complex a){
return new Complex(Real+a.getReal(),Imaginary+a.getImaginary());
}
public Complex ComplexSub(Complex a){
return new Complex(Real-a.getReal(),Imaginary-a.getImaginary());
}
public Complex ComplexMulti(Complex a){
double r=Real*a.getReal()-Imaginary*a.getImaginary();
double i =Imaginary*a.getReal()+Real*a.getImaginary();
return new Complex(Double.valueOf(df.format(r)),Double.valueOf(df.format(i)));
}
public Complex ComplexDiv(Complex a){
double r=(Real * a.Imaginary + Imaginary * a.Real) / (a.Imaginary * a.Imaginary + a.Real * a.Real);
double i=(Imaginary * a.Imaginary + Real * a.Real) / (a.Real * a.Real + a.Real * a.Real);
return new Complex(Double.valueOf(df.format(r)),Double.valueOf(df.format(i)));
}
}
-
测试成功截图:
-
PSP图:
步骤 | 耗时 | 占比 |
---|---|---|
需求分析 | 5min | 7% |
设计 | 10min | 13% |
代码实现 | 20min | 28% |
测试 | 30min | 42% |
分析总结 | 7min | 10% |
6.任务五:对实验二中的代码进行建模
任务要求类图中至少要有两个类,于是我参照其他同学,也使用MyDoc.java的代码来画UML图,UML图如下:
二、实验中碰到的问题
1.Junit显示红色
解决方案
点击File
,然后点击Project Structure
点击+
,然后点击JARs or directorys
在自己的IDEA安装路径下找到下面第二张图的两个jar
文件就可以了
三、参考资料
1.20175308 实验二《Java面向对象程序设计》实验报告
2.积极主动敲代码,使用JUnit学习Java
3.实验二《Java面向对象程序设计》的一点说明
4.Intellj IDEA 简易教程
5.实验二 Java面向对象程序设计
6.UML类图简介
7.Java DecimalFormat 用法(数字格式化)
好运永远只留给努力奋斗的人