20145307第二次JAVA学习实验报告
20145307《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 "错误";
}
}
测试代码:用以对产品代码进行测试的代码
二、复数的四则运算
原设计代码如下:
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.real*b.real-a.img*b.img;
temp.img = a.real*b.img+a.img*b.real;
return temp;
}
public Complex divComplex(Complex a,Complex b)
{
Complex temp = new Complex();
temp.real=(a.real*b.real+a.img*b.img)/(b.real*b.real+b.img*b.img);
temp.img =(a.img*b.real-a.real*b.img)/(b.real*b.real+b.img*b.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();
}
需求分析&总结
设计一个可以进行复数的简单四则运算的程序。
首先要定义接口引用API:java.util.Scanner;防止报错更加精准定义double类,之后明晰数学的基础知识附属的相关运算。
之后明晰思路,如何去设计,还是一个宗旨,定义函数,之后在主函数中进行引用之前定义过的函数,如果喜欢,自己也可去定义一个print函数来使用,方便在后期的界面中输出更多的函数,也会方便管理。add,sub,multi函数在前文代码中已经给出,之后只需要引用一下就好。