20155207 《Java程序设计》实验报告二:Java面向对象程序设计
实验要求
1.初步掌握单元测试和TDD
2.理解并掌握面向对象三要素:封装、继承、多态
3.初步掌握UML建模
4.熟悉S.O.L.I.D原则
5.了解设计模式
实验内容
一、单元测试
1.三种代码
伪代码:以简洁的自然语言表明设计步骤
百分制转五分制: 如果成绩小于60,转成“不及格” 如果成绩在60与70之间,转成“及格” 如果成绩在70与80之间,转成“中等” 如果成绩在80与90之间,转成“良好” 如果成绩在90与100之间,转成“优秀” 其他,转成“错误”
产品代码:用以实现特定功能的程序或机器语言;
package exp1;
public class EVER{
public static String percentage2fivegrade(int grade){
//如果成绩小于60,转成“不及格”
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 "优秀";
//其他,转成“错误”
else
return "错误";
}
}
测试代码:用以对产品代码进行测试的代码
-
TDD测试
-
一般步骤
-
明确当前要完成的功能,记录成一个测试列表
-
快速完成编写针对此功能的测试用例
-
测试代码编译不通过(没产品代码呢)
-
编写产品代码
-
测试通过
-
对代码进行重构,并保证测试通过
-
循环完成所有功能的开发
-
-
TDD的编码节奏是
1、增加测试代码,JUnit出现红条
2、修改产品代码
3、JUnit出现绿条,任务完成
二、复数的四则运算
原设计代码如下:
import java.util.Scanner;
class complex {
double a, b;
complex() {
this.a = 0;
this.b = 0;
}
complex(double a) {
this.a = a;
this.b = 0;
}
complex(double a, double b) {
this.a = a;
this.b = b;
}
complex add(complex p1, complex p2) {
complex p = new complex(p1.a + p2.a, p1.b + p2.b);
return p;
}
complex minus(complex p1, complex p2) {
complex p = new complex(p1.a - p2.a, p1.b - p2.b);
return p;
}
void print() {
System.out.println("复数的值为:");
if (this.b != 0)
System.out.println(this.a + "+" + this.b + "i");
else
System.out.println(this.a);
}
}
public class compleXX {
public static void main(String[] args) {
System.out.printf("依次输入两组数字每组表示一个复数:");
Scanner scanner=new Scanner(System.in);
int q,w,e,r,t;
q=scanner.nextInt();
w=scanner.nextInt();
e=scanner.nextInt();
r=scanner.nextInt();
complex c = new complex();
complex c1 = new complex(q, w);
complex c2 = new complex(e, r);
c1.print();
c2.print();
System.out.printf("输入1来选择加法运算输入2来选择减法运算");
t=scanner.nextInt();
switch (t){
case (1):
System.out.println("这两个复数的和为:");
System.out.println((c.add(c1, c2).a + "+" + c.add(c1, c2).b + "i").toString());
case (2):
System.out.println("这两个复数的差为:");
System.out.println((c.minus(c1, c2).a + "+" + c.minus(c1, c2).b + "i").toString());}
}
}
修改plan
public class Complex
{
double real,img;
public Complex()
{
this.real=0;
this.img =0;
}
public Complex(double real,double img)
{
this.real=real;
this.img =img;
}
public double getReal()
{
return this.real;
}
public double getImage()
{
return this.img;
}
public double getReal(Complex c)
{
return c.real;
}
public double getImage(Complex c)
{
return c.img;
}
public void setReal (double real)
{
this.real=real;
}
public void setImage(double img)
{
this.img =img;
}
public Complex addComplex(Complex a,Complex b)
{
Complex temp =new Complex();
temp.real=a.real+b.real;
temp.img =a.img +b.img;
return temp;
}
public Complex decComplex(Complex a,Complex b)
{
Complex temp = new Complex();
temp.real = a.real - b.real;
temp.img = a.img - b.img;
return temp;
}
public Complex mulComplex(Complex a,Complex b)
{
Complex temp = new Complex();
temp.real = a.realb.real-a.imgb.img;
temp.img = a.realb.img+a.imgb.real;
return temp;
}
public Complex divComplex(Complex a,Complex b)
{
Complex temp = new Complex();
temp.real=(a.realb.real+a.imgb.img)/(b.realb.real+b.imgb.img);
temp.img =(a.imgb.real-a.realb.img)/(b.realb.real+b.imgb.img);
return temp;
}
public void printComplex()
{
System.out.println(""+this.real+"+"+this.img+"i");
}
public String toString()
{
String fin=" ";
if(img>0)
{
fin = real+"+"+img+"i";
}
else if(img<0)
{
fin = real+ ""+img+"i";
}
else
{
fin = fin;
}
return fin;
}
for test:
public static void main(String[] args)
{
Complex q = new Complex(6, 3);
q.printComplex();
Complex w = new Complex(2, 7);
w.printComplex();
Complex asd = new Complex();
asd = asd.addComplex(q, w);
asd.printComplex();
asd= asd.decComplex(q, w);
asd.printComplex();
asd = asd.mulComplex(q, w);
asd.printComplex();
asd = asd.divComplex(q, w);
asd.printComplex();
}