标签:word uml xtend work port 测试 string 教程 pass
1.初步掌握单元测试和TDD
2.理解并掌握面向对象三要素:封装、继承、多态
3.初步掌握UML建模
4.熟悉S.O.L.I.D原则
5.了解设计模式
1.没有Linux基础的同学建议先学习《Linux基础入门(新版)》《Vim编辑器》 课程
2.完成实验、撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用,程序的编辑,调试,运行等)、解决办法(空洞的方法如“查网络”、“问同学”、“看书”等一律得0分)以及分析(从中可以得到什么启示,有什么收获,教训等)。报告可以参考范飞龙老师的指导
3.严禁抄袭,有该行为者实验成绩归零,并附加其他惩罚措施。
4.请大家先在实验楼中的~/Code目录中用自己的学号建立一个目录,代码和UML图要放到这个目录中,截图中没有学号的会要求重做,然后跟着下面的步骤练习。
伪代码:
百分制转五分制:
如果成绩小于60,转成“不及格”
如果成绩在60与70之间,转成“及格”
如果成绩在70与80之间,转成“中等”
如果成绩在80与90之间,转成“良好”
如果成绩在90与100之间,转成“优秀”
其他,转成“错误”
产品代码:
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 "错误";
}
}
测试代码:
public class MyUtilTest {
public static void main(String[] args) {
if(MyUtil.percentage2fivegrade(55) != "不及格")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(65) != "及格")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(75) != "中等")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(85) != "良好")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(95) != "优秀")
System.out.println("test failed!");
else
System.out.println("test passed!");
}
}
运行截图:
TDD的编码节奏是:增加测试代码,JUnit出现红条->修改产品代码->JUnit出现绿条,任务完成。基于TDD,我们不会出现过度设计的情况,需求通过测试用例表达出来了,我们的产品代码只要让测试通过就可以了。 按照教程上的要求我完成了MyUtilTest,而老师提出的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);
}
}
对这个实验,设计测试类代码:
public class StringBufferDemoTest extends TestCase {
StringBuffer a = new StringBuffer("StringBuffer");//测试12个字符(<=16)
StringBuffer b = new StringBuffer("StringBufferStringBuffer");//测试24个字符(>16&&<=34)
StringBuffer c = new StringBuffer("StringBufferStringBufferStringBuffer");//测试36个字符(>=34)
@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 testlength() throws Exception{
assertEquals(12,a.length());
assertEquals(24,b.length());
assertEquals(36,c.length());
}
@Test
public void testindexOf() throws Exception{
assertEquals(0,a.indexOf("Str"));
assertEquals(5,a.indexOf("gBu"));
assertEquals(10,a.indexOf("er"));
}
}
实验结果截图:
让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确
运行截图:
// 定义属性并生成getter,setter
double RealPart;
double ImagePart;
// 定义构造函数
public Complex()
public Complex(double R,double I)
//Override Object
public boolean equals(Object obj)
public String toString()
// 定义公有方法:加减乘除
Complex ComplexAdd(Complex a)
Complex ComplexSub(Complex a)
Complex ComplexMulti(Complex a)
Complex ComplexDiv(Complex a)
实验代码:
public class MyComplex {
private double m;
private double n;
public double getm(double m)
{
return m;
}
public double getn(double n)
{
return n;
}
public void setm(double m)
{
this.m=m;
}
public void setn(double n)
{
this.n=n; }
public MyComplex(double m, double n) {
this.m = m;
this.n = n;
}
public MyComplex add(MyComplex c) {
return new MyComplex(m + c.m, n + c.n);
}
public MyComplex minus(MyComplex c) {
return new MyComplex(m - c.m, n - c.n);
}
public MyComplex multiply(MyComplex c) {
return new MyComplex(m * c.m - n * c.n, m * c.n + n * c.m);
}
public MyComplex ComplexDiv(MyComplex c)
{
return new MyComplex((mc.m+nc.n)/((c.mc.m)(c.nc.n)),(nc.m-mc.n)/((c.mc.m)(c.nc.n)));
}
public String toString() {
String str = new String();
if (this.n>=0)
{
str=m+"+"+n+"i";
}
else str=m+"-"+(-n)+"i";
return str;
}
public boolean equals(MyComplex c)
{
if(this.m==c.m&&this.n==c.n)
return true;
else
return false;
}
}
测试类代码:
import junit.framework.TestCase;
import org.junit.Test;
public class MyComplexTest extends TestCase {
MyComplex a = new MyComplex(1, 2);
MyComplex b = new MyComplex(1, -4);
MyComplex c = new MyComplex(19, 0);
MyComplex d = new MyComplex(0, -3);
MyComplex e = new MyComplex(1, 0);
MyComplex f = new MyComplex(1, 0);
@Test
public void testadd()
{
assertEquals("2.0-2.0i",a.add(b).toString());
assertEquals("1.0-1.0i",a.add(d).toString());
}
@Test
public void testminus()
{
assertEquals("0.0+6.0i", a.minus(b).toString());
assertEquals("-18.0+2.0i", a.minus(c).toString());
}
@Test
public void testmultiply()
{
assertEquals("9.0-2.0i", a.multiply(b).toString());
assertEquals("19.0+38.0i", a.multiply(c).toString());
}
@Test
public void testComplexDiv()
{
assertEquals("-0.4375+0.375i",a.ComplexDiv(b).toString());
assertEquals("1.1875+4.75i",c.ComplexDiv(b).toString());
}
@Test
public void testequals()
{
assertEquals(false,a.equals(b));
assertEquals(true,e.equals(f));
}
}
运行截图:
20145207 实验二《Java面向对象程序设计》实验报告
标签:word uml xtend work port 测试 string 教程 pass
原文地址:http://www.cnblogs.com/20145207lza/p/6759670.html