20192306 2020-2021-1 《数据结构与面向对象程序设计》实验三报告
20192306 2020-2021-1 《数据结构与面向对象程序设计》实验三报告
课程:《程序设计与数据结构》
班级: 1923
姓名: 孙洪丽
学号: 20192306
实验教师:王志强
实验日期:2020年10月8日
必修/选修: 必修
1.实验内容
- 初步掌握单元测试和TDD
- 理解并掌握面向对象三要素:封装、继承、多态
- 初步掌握UML建模
- 完成蓝墨云上 (1)-(5)实验:
(1)提交点一:单元测试
(2)提交点二:以TDD的方式研究学习StringBuffer
(3)提交点三:体会OCP原则和DIP原则的应用
(4)提交点四:以TDD的方式开发一个复数类Complex
(5)提交点五:UML建模
2. 实验过程及结果
- Myutil及MyutilTest的编写
- 代码
产品代码
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 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 testExceptions(){
assertEquals("错误",MyUtil.percentage2fivegrade(-55));
assertEquals("错误",MyUtil.percentage2fivegrade(-55));
}
@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));
}
}
- 运行结果截图
- 码云托管链接
https://gitee.com/besti1923/sun-hongli-20192306/commit/ffc35e1647fbcf634e06d4dbd77953e95949db74
https://gitee.com/besti1923/sun-hongli-20192306/commit/66789ada933d0fdf4487f2edd28a6e49f4b75f47
- StringBuffer及StringBufferTest的编写
- 代码
产品代码
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buffer=new StringBuffer(20);
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");
@Test
public void testcharAt() throws Exception{
assertEquals('S',a.charAt(0));
assertEquals('t',b.charAt(13));
assertEquals('b',c.charAt(30));
}
@Test
public void testindexOf() throws Exception{
assertEquals(0,a.indexOf("S"));
assertEquals(1,b.indexOf("tri"));
assertEquals(4,c.indexOf("ng"));
}
@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 testtoString() throws Exception{
assertEquals("Stringbuffer",a.toString());
assertEquals("StringbufferStringbuffer",b.toString());
assertEquals("StringbufferStringbufferStringbuffer",c.toString());
}
}
- 运行结果截图
- 码云托管链接
https://gitee.com/besti1923/sun-hongli-20192306/commit/cf94a5e0dc952014a0b00744cfbe34c81cd7127e
- OCP原则和DIP原则的应用:支持Byte类
- 代码
设计模式示例——支持Int类
// 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();
}
}
//Client classes
class Document {
Data pd;
Document(Factory pf){
pd = pf.CreateDataObject();
}
public void DisplayData(){
pd.DisplayValue();
}
}
//Test class
public class MyDoc {
static Document d;
public static void main(String[] args) {
d = new Document(new IntFactory());
d.DisplayData();
}
}
扩充——支持Byte类
import java.util.Objects;
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=(long)20192306;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Byte extends Data {
byte value;
Byte() {
value=(byte)20192306;
}
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 ByteFactory extends Factory {
public Data CreateDataObject(){
return new Byte();
}
}
class Document {
Data data;
Document(Factory factory){
data = factory.CreateDataObject();
}
public void DisplayData(){
data.DisplayValue();
}
}
public class MyDoc {
static Document d;
static Document c;
public static void main(String[] args) {
d = new Document(new ByteFactory());
d.DisplayData();
c = new Document(new LongFactory());
c.DisplayData();
}
}
- 运行结果截图
- 码云托管链接
https://gitee.com/besti1923/sun-hongli-20192306/commit/1c384f85f24974aef43b76ffc62916d16446ba98
- 以TDD的方式开发一个复数类Complex
- 代码
产品代码
public class Complex {
private double RealPart;
private double ImagePart;
public Complex(){
}
public Complex(double R, double I){
this.RealPart = R;
this.ImagePart = I;
}
public double getRealPart() {
return RealPart;
}
public void setRealPart(double realPart) {
RealPart = realPart;
}
public double getImagePart() {
return ImagePart;
}
public void setImagePart(double imagePart) {
ImagePart = imagePart;
}
public boolean equals(Object obj){
if(this == obj) {
return true;
}
if(!(obj instanceof Complex)) {
return false;
}
Complex complex = (Complex) obj;
if(complex.RealPart != ((Complex) obj).RealPart) {
return false;
}
if(complex.ImagePart != ((Complex) obj).ImagePart) {
return false;
}
return true;
}
public String toString() {
String string = "";
if (ImagePart > 0)
string = RealPart + "+" + ImagePart + "i";
if (ImagePart == 0)
string = RealPart + "";
if (ImagePart < 0)
string = RealPart + " " + ImagePart + "i";
return string;
}
Complex ComplexAdd(Complex a) {
return new Complex(RealPart+a.RealPart,ImagePart+a.ImagePart);
}
Complex ComplexSub(Complex a) {
return new Complex(RealPart-a.RealPart,ImagePart-a.ImagePart);
}
Complex ComplexMulti(Complex a) {
return new Complex(RealPart*a.RealPart-ImagePart*a.ImagePart,ImagePart*a.RealPart+RealPart*a.ImagePart);
}
Complex ComplexDiv(Complex a) {
Complex d = new Complex();
d.RealPart = (this.RealPart * a.RealPart + this.ImagePart * a.ImagePart)/(a.RealPart*a.RealPart+a.ImagePart*a.ImagePart);
d.ImagePart = (this.ImagePart * a.RealPart - this.RealPart * a.ImagePart)/(a.RealPart*a.RealPart+a.ImagePart*a.ImagePart);
return d;
}
}
测试代码
import static org.junit.Assert.*;
import org.junit.Test;
import junit.framework.TestCase;
public class ComplexTest extends TestCase {
Complex complex = new Complex(1,1);
@Test
public void testAdd(){
assertEquals(new Complex(2.0,1.6), complex.ComplexAdd(new Complex(5.2,3.0)));
}
@Test
public void testSub(){
assertEquals(new Complex(-2.0,-1.6), complex.ComplexSub(new Complex(5.2,3.0)));
}
@Test
public void testMulti(){
assertEquals(new Complex(3.0,2.0), complex.ComplexMulti(new Complex(3.0,2.0)));
}
@Test
public void testDiv(){
assertEquals(new Complex(1.0,1.0), complex.ComplexDiv(new Complex(1.0,1.0)));
assertEquals(new Complex(0.0,0.0), complex.ComplexDiv(new Complex(1.0,0.0)));
}
@Test
public void testequals(){
assertEquals(true, complex.equals(new Complex(1.0,1.0)));
}
}
- 运行结果截图
- 码云托管链接
https://gitee.com/besti1923/sun-hongli-20192306/commit/900431a500f1265fe158d6aeb1356c5b4f754d7e
- UML建模
- MyUtil
- StringBufferDemo
- Complex
3. 实验过程中遇到的问题和解决过程
- 问题1:使用junit时,无法New->Directory
- 问题1解决方案:在安装IDEA和链接码云的时候,因为操作的问题,造成了文件夹的“套娃”现象,在平时所使用的文件夹同级目录下还有一个src文件夹,把这个src删掉,就可以正常New->Directory了;
- 问题2:找不到jar包
- 问题2解决方案:按照教程在电脑中搜索“junit.jar”,没有找到,通过查询其他博客得知,可以直接在IDEA安装文件夹lib中选择,应选择“junit-4.12.jar”,“junit.jar”没有用;
其他(感悟、思考等)
- 单元测试的好处
- 单元测试可最大限度的减少bug
- 可快速定位bug,减少调试时间
- 可突破构思瓶颈
- 提高代码质量
- 在这次实验中学习到了如何进行Junit测试,并初步掌握了面向对象编程的过程,同时了解了TDD方式,OCP和DIP原则,知道了模式的重要作用和设计模式在Java中的重要地位;通过对老师给出的例子进行学习扩充,运用了举一反三的学习方法,对编程有了进一步的认识和熟悉。