20175324 2018-2019-2 实验二 面向对象程序设计 实验报告
20175324 2018-2019-2 实验二 面向对象程序设计 实验报告
实验要求
1、初步掌握单元测试和TDD
2、理解并掌握面向对象三要素:封装、继承、多态
3、初步掌握UML建模
4、熟悉S.O.L.I.D原则
5、了解设计模式
实验内容与运行结果
- 实验二 面向对象程序设计-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 {
public void testZhengchang() {
assertEquals("不及格", MyUtil.percentage2fivegrade(55));
assertEquals("及格", MyUtil.percentage2fivegrade(65));
assertEquals("中等", MyUtil.percentage2fivegrade(75));
assertEquals("良好", MyUtil.percentage2fivegrade(85));
assertEquals("优秀", MyUtil.percentage2fivegrade(95));
}
@Test
public void testYichang(){
assertEquals("错误",MyUtil.percentage2fivegrade(-55));
assertEquals("错误",MyUtil.percentage2fivegrade(105));
}
@Test
public void testBianjie(){
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{
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);
}
}
测试代码:
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 20175312");
@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("5312"));
}
}
截图如下:
- 实验二 面向对象程序设计-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类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
学号20175312,12%6=0,做“0: 让系统支持Byte类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印”
实验代码:
public class MyDoc {
static Document d;
static Document e;
public static void main(String[] args) {
e=new Document(new ByteFactory());
e.DisplayData();
}
}
abstract class Data {
abstract public void DisplayValue();
}
class Integer extends Data {
int value;
Integer() {
value=5312;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Byte extends Data {
byte value;
Byte(){
value=12;
}
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 ByteFactory extends Factory {
public Data CreateDataObject(){
return new Byte();
}
}
class Document {
Data pd;
Document(Factory pf){
pd = pf.CreateDataObject();
}
public void DisplayData(){
pd.DisplayValue();
}
}
截图如下:
- 实验二 面向对象程序设计-4
提交:单元测试代码和运行成功截图及码云上代码链接,截图要加上学号水印
参考http://www.cnblogs.com/rocedu/p/6736847.html
任务:以TDD的方式开发一个复数类Complex,要求如下:
// 定义属性并生成getter,setter
double RealPart;
double ImagePart;
待测试代码:
public class Complex{
double RealPart;
double ImagePart;
public double getRealPart(){
return RealPart;
}
public double getImagePart(){
return ImagePart;
}
public Complex(){}
public Complex(double R,double I){
RealPart = R;
ImagePart = I;
}
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 s = new String();
if(ImagePart>0)
s = getRealPart()+"+"+getImagePart()+"i";
if(ImagePart==0)
s = getRealPart()+"";
if(ImagePart<0)
s = getImagePart()+getImagePart()+"i";
if(RealPart==0)
s = getImagePart()+"i";
if(ImagePart==0&&RealPart==0)
s = "0";
return s;
}
public Complex ComplexAdd(Complex a){
return new Complex(RealPart+a.RealPart,ImagePart+a.ImagePart);
}
public Complex ComplexSub(Complex a){
return new Complex(RealPart-a.RealPart,ImagePart-a.ImagePart);
}
public Complex ComplexMulti(Complex a){
return new Complex(RealPart*a.RealPart,ImagePart*a.ImagePart);
}
public Complex ComplexDiv(Complex a){
return new Complex((RealPart * a.ImagePart + ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart), (ImagePart * a.ImagePart + RealPart * a.RealPart) / (a.RealPart * a.RealPart + a.RealPart * a.RealPart));
}
}
测试代码:
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
Complex a = new Complex(2.0,4.0);
Complex b = new Complex(0.0,-0.0);
Complex c = new Complex(-4.0,0.0);
@Test
public void testgetRealpart(){
assertEquals(2.0,a.getRealPart());
assertEquals(0.0,b.getRealPart());
assertEquals(-4.0,c.getRealPart());
}
@Test
public void testgetImagePart(){
assertEquals(4.0,a.getImagePart());
assertEquals(-0.0,b.getImagePart());
assertEquals(0.0,c.getImagePart());
}
@Test
public void testComplexAdd(){
String q = a.ComplexAdd(b).toString();
String w = b.ComplexAdd(c).toString();
String e = c.ComplexAdd(a).toString();
assertEquals("2.0+4.0i",q);
assertEquals("-4.0",w);
assertEquals("-2.0+4.0i",e);
}
@Test
public void testComplexSub(){
String r = a.ComplexSub(b).toString();
String t = b.ComplexSub(c).toString();
String y = c.ComplexSub(a).toString();
assertEquals("2.0+4.0i",r);
assertEquals("4.0",t);
assertEquals("-8.0i",y);
}
@Test
public void testComplexMulti(){
String u = a.ComplexMulti(b).toString();
String i = b.ComplexMulti(c).toString();
String o = c.ComplexMulti(a).toString();
assertEquals("0",u);
assertEquals("0",i);
assertEquals("-8.0",o);
}
@Test
public void testComplexDiv(){
String p = c.ComplexDiv(a).toString();
assertEquals("-2.0i",p);
}
@Test
public void testtoString(){
assertEquals("2.0+4.0i",a.toString());
assertEquals("0",b.toString());
assertEquals("-4.0",c.toString());
}
}
截图如下:
- 实验二 面向对象程序设计-5
使用WhiteStarUML对实验二中的代码进行建模,发类图的截图,加上学号水印。
参考http://www.cnblogs.com/rocedu/p/6736847.html
类图中只少两个类。
截图如下:
遇到的问题
-
问题一
在调试test的时候不小心删除了输出项,所有的项目都出现了一个红色的J,无法run,我还认为是idea坏了。
解决办法
-
解决问题一
我上网查找方法后,在idea设置里删除了原有路径,重新设定了output,就ok了。
码云链接