实验二《面向对象程序设计》_实验报告
实验二《面向对象程序设计》_实验报告
一、实验前期预习准备
Junit的安装和配置
- 选择
File->Setting
打开设置对话框,在设置对话框中选择Plugins
, 单击Install JetBrains plugin...
按钮打开插件安装界面: - 在插件安装对话框的搜索中输入
junit
,单击JunitGenerator V2.0,单击右边绿色的Install1
按钮安装: - 结果如下
二、实验内容与主要步骤
提交点1:测试MyUtil类
- 实验要求
- 参考 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 {
@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));
}
}
- 运行测试结果
提交点2:以TDD的方式研究学习StringBuffer
- 实验要求
- 参考 积极主动敲代码,使用JUnit学习Java (http://www.cnblogs.com/rocedu/p/4837092.html)
- 参考http://www.cnblogs.com/rocedu/p/6736847.html
- 以 TDD的方式研究学习StringBuffer,提交你的单元测试用例和测试通过的截图,截图要加上学号水印。
- 实验过程
- 产品代码
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("tring"));
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('t', a.charAt(1));
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, b.indexOf("gBu"));
assertEquals(8, c.indexOf("ffer"));
}
}
- 实验结果截图
提交点3:对设计模式示例进行扩充
- 实验要求
- 参考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类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
- 实验代码
abstract class Data {
abstract public void DisplayValue();
}
class Integer extends Data {
int value;
Integer() {
value=20175226%6;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Byte extends Data {
byte value;
Byte() {
value=20175226%6;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Short extends Data {
short value;
Short() {
value=20175226%6;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Boolean extends Data {
boolean value;
Boolean() {
value=20175226%6==4;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Long extends Data {
long value;
Long() {
value=20175226;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Double extends Data {
double value;
Double() {
value=20175226%6;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Float extends Data {
float value;
Float() {
value=20175226%6;
}
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 ByteFactory extends Factory {
public Data CreateDataObject(){
return new Byte();
}
}
class ShortFactory extends Factory {
public Data CreateDataObject(){
return new Short();
}
}
class BoolenFactory extends Factory {
public Data CreateDataObject(){
return new Boolean();
}
}
class LongFactory extends Factory {
public Data CreateDataObject(){
return new Long();
}
}
class DoubletFactory extends Factory {
public Data CreateDataObject(){
return new Double();
}
}
class FloatFactory extends Factory {
public Data CreateDataObject(){
return new Float();
}
}
//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();
d = new Document(new ByteFactory());
d.DisplayData();
d = new Document(new ShortFactory());
d.DisplayData();
d = new Document(new BoolenFactory());
d.DisplayData();
d = new Document(new LongFactory());
d.DisplayData();
d = new Document(new DoubletFactory());
d.DisplayData();
d = new Document(new FloatFactory());
d.DisplayData();
}
}
- 实验结果
提交点4:以TDD的方式开发一个复数类Complex
- 实验要求
- 方式开发一个复数类Complex
- 代码要求
// 定义属性并生成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 Complex {
// 定义属性并生成getter,setter
double RealPart;
double ImagePart;
// 定义构造函数
public Complex(){
RealPart = 0;
ImagePart = 1;
}
public Complex(double R,double I){
ImagePart = I;
RealPart = R;
}
//Override Object
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) {
double b=0,c=0;
b = RealPart+a.RealPart;
c = ImagePart+a.ImagePart;
System.out.println("("+RealPart+"+"+ImagePart+"i) + ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
return new Complex(b,c);
}
Complex ComplexSub(Complex a) {
double b=0,c=0;
b = RealPart-a.RealPart;
c = ImagePart-a.ImagePart;
System.out.println("("+RealPart+"+"+ImagePart+"i) - ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
return new Complex(b,c);
}
Complex ComplexMulti(Complex a) {
double b=0,c=0;
b = RealPart*a.RealPart;
c = ImagePart*a.ImagePart;
System.out.println("("+RealPart+"+"+ImagePart+"i) * ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
return new Complex(b,c);
}
Complex ComplexDiv(Complex a) {
if(a.RealPart==0||a.ImagePart==0) {
System.out.println("被除数不能为0,错误");
return new Complex();
}
double d = Math.sqrt(a.RealPart*a.RealPart)+Math.sqrt(a.ImagePart*a.ImagePart);
double b=0,c=0;
b = (RealPart*a.RealPart+ImagePart*a.ImagePart)/d;
c = Math.round((RealPart*a.ImagePart-ImagePart*a.RealPart)/d);
System.out.println("("+RealPart+"+"+ImagePart+"i) / ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
return new Complex(b,c);
}
}
- 测试代码
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
Complex complex = new Complex(1, 1);
@Test
public void testAdd() {
assertEquals(new Complex(3.0, 3.0), complex.ComplexAdd(new Complex(2.0, 2.0)));
}
@Test
public void testSub() {
assertEquals(new Complex(-5.0, -2.0), complex.ComplexSub(new Complex(6.0, 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)));
//边缘测试
}
}
- 结果截图
提交点5:类图
实验中碰到的问题
- 问题1:在第二个提交点写测试类时,测试
charAt
方法时我写的语句是
public void testcharAt() throws Exception {
assertEquals("t", a.charAt(1));
assertEquals("g", a.charAt(5));
assertEquals("r", a.charAt(11));
}
结果发现怎么样测试都是红色条,不通过。
- 问题1解决过程:经过反复测试我将测试的代码换成了下面这样才能通过
public void testcharAt() throws Exception {
assertEquals('t', a.charAt(1));
assertEquals('g', a.charAt(5));
assertEquals('r', a.charAt(11));
}
这个问题体现了一些我们的细节,像两个字符串比较一样,虽然" s "
和' s '
看起来内容一模一样但事实上两者比较起来两者之间有很大差异。两者占用的内存空间就不一样至少会有"\0".所以显而易见字符可以被视为长度为1的字符串这个想法是错误的。
(代码托管)[https://gitee.com/wpyzka/20175226/tree/master/src/shiyan2]
步骤 | 耗时 | 百分比 |
---|---|---|
需求分析 | 10min | 5.7% |
设计 | 25min | 14.7% |
代码实现 | 80min | 47.1% |
测试 | 10min | 5.7% |
分析总结 | 45min | 26.5% |