20192318 实验三《数据结构与面向对象程序设计》实验报告
20192318 2020-2021-1 《数据结构与面向对象程序设计》实验三报告
课程:《程序设计与数据结构》
班级: 1923
姓名: 邢苏
学号: 20192318
实验教师:王志强
实验日期:2020年10月10日
必修/选修: 必修
1.实验内容
下载安装并使用IDEA,完成下面实验
- 1.初步掌握单元测试和TDD
- 2.理解并掌握面向对象三要素:封装、继承、多态
- 3.初步掌握UML建模
- 4.完成蓝墨云上(1)-(5)实验
2. 实验过程及结果
- 第一个提交点
代码如下:
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 "错误";
}
}
测试代码如下:
package test;
import junit.framework.TestCase;
public class MyUtilTest extends TestCase
{
@Test
public void testNormal()
{
assertEquals("不及格", MyUtilTest.percentage2fivegrade(55));
assertEquals("及格", MyUtilTest.percentage2fivegrade(65));
assertEquals("中等", MyUtilTest.percentage2fivegrade(75));
assertEquals("良好", MyUtilTest.percentage2fivegrade(85));
assertEquals("优秀", MyUtilTest.percentage2fivegrade(95));
}
@Test
public void testBorder()
{
assertEquals("不及格", MyUtilTest.percentage2fivegrade(0));
assertEquals("及格", MyUtilTest.percentage2fivegrade(60));
assertEquals("中等", MyUtilTest.percentage2fivegrade(70));
assertEquals("良好", MyUtilTest.percentage2fivegrade(80));
assertEquals("优秀", MyUtilTest.percentage2fivegrade(90));
assertEquals("优秀", MyUtilTest.percentage2fivegrade(100));
}
@Test
public void testExceptions()
{
assertEquals("错误", MyUtilTest.percentage2fivegrade(-5));
assertEquals("错误", MyUtilTest.percentage2fivegrade(110));
}
private static String percentage2fivegrade(int i)
{
//如果成绩小于0,转成“错误”
if ((i < 0))
return "错误";
//如果成绩小于60,转成“不及格”
else if (i < 60)
return "不及格";
//如果成绩在60与70之间,转成“及格”
else if (i < 70)
return "及格";
//如果成绩在70与80之间,转成“中等”
else if (i < 80)
return "中等";
//如果成绩在80与90之间,转成“良好”
else if (i < 90)
return "良好";
//如果成绩在90与100之间,转成“优秀”
else if (i <= 100)
return "优秀";
//如果成绩大于100,转成“错误”
else
return "错误";
}
}
运行结果如下:
- 第二个提交点
代码如下:
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());
}
}
测试代码如下:
package test;
import junit.framework.TestCase;
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('g',a.charAt(5));
assertEquals('r',a.charAt(11));
}
@Test
public void testcapacity() throws Exception{
assertEquals(28,a.capacity());
assertEquals(40,b.capacity());
assertEquals(52,c.capacity());
}
@Test
public void testindexOf() throws Exception{
assertEquals(0,a.indexOf("Str"));
assertEquals(5,a.indexOf("gBu"));
assertEquals(9,a.indexOf("fer"));
}
}
运行结果如下:
- 第三个提交点:
代码如下:
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)20192318;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Byte extends Data {//Byte继承Data类
byte value;
Byte() {
value=(byte)20192318;
}
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 LongFactory extends Factory {
public Data CreateDataObject(){
return new Long();
}
}
class ByteFactory extends Factory {//ByteFactory继承工厂类
public Data CreateDataObject(){
return new Byte();
}
}
//Client classes
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();
}
}
运行结果如下;
-
第四个提交点
代码如下:
import java.lang.*;
public class Complex {
public void setRealpart(double R){
RealPart=R;
}
public double getRealpart(){
return RealPart;
}
public void setImagePart(double I){
ImagePart=I;
}
public double getImagePart(){
return ImagePart;
}double RealPart; double ImagePart; public Complex(double R,double I){ RealPart=R; ImagePart=I; } public boolean equals(Complex obj){ if(this.getRealpart()==obj.getRealpart() && this.getImagePart()==obj.getImagePart()) return true; else return false; } public String toString(){ return RealPart+"+"+ImagePart+"i"; } public Complex ComplexAdd(Complex a){ return new Complex(this.RealPart+a.RealPart,this.ImagePart+a.ImagePart); } public Complex ComplexSub(Complex a){ return new Complex(this.RealPart-a.RealPart,this.ImagePart-a.ImagePart); } public Complex ComplexMulti(Complex a){ return new Complex(this.RealPart*a.RealPart-this.ImagePart*a.ImagePart, this.RealPart*a.ImagePart+this.ImagePart*a.RealPart); } public Complex ComplexDiv(Complex a){ double x=this.RealPart; double y=this.ImagePart; double m=a.RealPart; double n=a.ImagePart; return new Complex((x*m+y*n)/(m*m+n*n),(y*m-x*n)/(m*m+n*n)); }
}
测试代码如下:
package test;
import junit.framework.TestCase;
public class ComplexTest extends TestCase {
Complex p1=new Complex(1,1);
Complex p2=new Complex(1,-1);
@Test
public void testAdd(){
assertEquals("2.0+0.0i",p1.ComplexAdd(p2).toString());
System.out.println(p1.ComplexAdd(p2));
}
@Test
public void testSub(){
assertEquals("0.0+2.0i",p1.ComplexSub(p2).toString());
System.out.println(p1.ComplexSub(p2));
}
@Test
public void testMulti(){
assertEquals("2.0+0.0i",p1.ComplexMulti(p2).toString());
System.out.println(p1.ComplexSub(p2));
}
@Test
public void testDiv(){
assertEquals("0.0+1.0i",p1.ComplexDiv(p2).toString());
System.out.println(p1.ComplexDiv(p2));
}
public class Complex {
public void setRealpart(double R){
RealPart=R;
}
public double getRealpart(){
return RealPart;
}
public void setImagePart(double I){
ImagePart=I;
}
public double getImagePart(){
return ImagePart;
}
double RealPart;
double ImagePart;
public Complex(double R,double I){
RealPart=R;
ImagePart=I;
}
public boolean equals(Complex obj){
if(this.getRealpart()==obj.getRealpart() && this.getImagePart()==obj.getImagePart())
return true;
else
return false;
}
public String toString(){
return RealPart+"+"+ImagePart+"i";
}
public Complex ComplexAdd(Complex a){
return new Complex(this.RealPart+a.RealPart,this.ImagePart+a.ImagePart);
}
public Complex ComplexSub(Complex a){
return new Complex(this.RealPart-a.RealPart,this.ImagePart-a.ImagePart);
}
public Complex ComplexMulti(Complex a){
return new Complex(this.RealPart*a.RealPart-this.ImagePart*a.ImagePart,
this.RealPart*a.ImagePart+this.ImagePart*a.RealPart);
}
public Complex ComplexDiv(Complex a){
double x=this.RealPart;
double y=this.ImagePart;
double m=a.RealPart;
double n=a.ImagePart;
return new Complex((x*m+y*n)/(m*m+n*n),(y*m-x*n)/(m*m+n*n));
}
}
}
运行结果如下:
- 第五个提交点
代码UML建模
3. 实验过程中遇到的问题和解决过程
- 问题一:在测试代码中出现了教程链接之外的报错,如下图
- 问题一解决方案:在图中所示的报错红名之后均添加一个Test即可解决问题
- 问题二:在编写测试代码时,在每一个测试案例之前均添加一个@Test均报错
- 问题二解决方案:
1.File-->Project Structure-->Modules-->右侧Dependencies-->+号-->JARs or directories...
2.在弹出窗口中找到IDEA的安装目录-->点击lib展开-->选择所需的两个包(hamcrest-core-1.3.jar与junit-4.12.jar)-->点击ok
即可解决问题
其他
由于之前没有学习过测试代码的编写以及如何去使用它,所以这次的实验有很多地方下不去手
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步