20144303 《Java程序设计》第二次实验实验报告
20144303 《Java程序设计》第二次实验实验报告
北京电子科技学院(besti)实验报告
实验内容
- 初步掌握单元测试和TDD
- 理解并掌握面向对象三要素:封装、继承、多态
- 初步掌握UML建模
- 熟悉S.O.L.I.D原则
- 了解设计模式
实验步骤
一、三种代码:
产品代码
public class MyUtil{
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 "错误";
}
}
编写MyUtilTest的测试模块
public class MyUtilTest {
public static void main(String[] args) {
// 百分制成绩是50时应该返回五级制的“不及格”
if(MyUtil.percentage2fivegrade(50) != "不及格")
System.out.println("test failed!");
else
System.out.println("test passed!");
}
}
结果:
只有一组输入的测试是不充分的,我们把一般情况都测试一下,代码如下:
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:
一般步骤如下:
- 明确当前要完成的功能,记录成一个测试列表
- 快速完成编写针对此功能的测试用例
- 测试代码编译不通过(没产品代码呢)
- 编写产品代码
- 测试通过
- 对代码进行重构
- 循环完成所有功能
TDD的编码节奏
- 增加测试代码,JUnit出现红条
- 修改产品代码
- JUnit出现绿条,任务完成
二、面向对象三要素:
- 抽象
- 封装、继承与多态
- 设计模式初步
三、S.O.L.I.D原则
面向对象三要素是“封装、继承、多态”,任何面向对象编程语言都会在语法上支持这三要素。如何借助抽象思维用好三要素特别是多态还是非常困难的,S.O.L.I.D类设计原则是一个很好的指导:
SRP(Single Responsibility Principle,单一职责原则)
OCP(Open-Closed Principle,开放-封闭原则)
LSP(Liskov Substitusion Principle,Liskov替换原则)
ISP(Interface Segregation Principle,接口分离原则)
DIP(Dependency Inversion Principle,依赖倒置原则)
public abstract class Animal {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public abstract String shout();
}
public class Dog extends Animal{
public String shout(){
return "汪汪";
}
public String toString(){
return "The Dog's color is " + this.getColor() +", and it shouts "+ this.shout() + "!";
}
}
public class Cat extends Animal{
public String shout(){
return "喵喵";
}
public String toString(){
return "The Cat's color is " + this.getColor() +", and it shouts "+ this.shout() + "!";
}
}
四、练习
public class TestComplex
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("请输入复数A:");
float realpart1=sc.nextInt();float imagepart1=sc.nextInt();
System.out.println("请输入复数B:");
float realpart2=sc.nextInt();float imagepart2=sc.nextInt();
Complex c1=new Complex(realpart1,imagepart1);
Complex c2=new Complex(realpart2,imagepart2);
System.out.println("请选择运算:\n"+
"(选择 1 执行加法运算)\n"+
"(选择 2 执行减法运算)\n"+
"(选择 3 执行乘法运算)\n"+
"(选择 4 执行除法运算)");
int s=sc.nextInt();
Complex C=new Complex();
for(;;)
{
C.PressButton(s,c1,c2);
System.out.println("Please input complex A again:");
realpart1=sc.nextInt();imagepart1=sc.nextInt();
System.out.println("Please input complex B again:");
realpart2=sc.nextInt();imagepart2=sc.nextInt();
System.out.println("Please choose the operate pattern:\n"+
"(choose 1 will run add operation)\n"+
"(choose 2 will run sub operation)\n"+
"(choose 3 will run mul operation)\n"+
"(choose 4 will run div operation)");
s=sc.nextInt();
if(s==-1)
{
System.out.println("Game over!");
break;
}
}
}
}
结果:
import java.util.Scanner;
public class ComplexMain {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入两个复数的实部和虚部:(a+bi、c+di)");
System.out.printf("a =");
int a=scanner.nextInt();
System.out.printf("b =");
int b=scanner.nextInt();
System.out.printf("c =");
int c=scanner.nextInt();
System.out.printf("d =");
int d=scanner.nextInt();
Complex fushu1=new Complex(a,b);
Complex fushu2=new Complex(c,d);
while(true) {
System.out.println("请输入需要进行的运算:1、ADD 2、SUBTRACT 3、MULTIPLY ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println(Complex.addition(fushu1, fushu2));
break;
case 2:
System.out.println(Complex.subtract(fushu1, fushu2));
break;
case 3:
System.out.println(Complex.multiplication(fushu1, fushu2));
break;
default:System.out.println("ERROR!!!");
}
}
}
}
结果:
最新文章
- web自动化工具-开篇
- 网页JQ基础之jq-隐藏以及显示特效
- 2015弱校联盟(1) - E. Rectangle
- iOS segue 跳转
- 驱动笔记 - file_operations
- Linux makefile 教程 很具体,且易懂
- 史上最全的 UIWebview 的 JS 与 OC 交互
- Android-监听sdcard状态
- phpcms前端页面上传文件
- Intent是什么?
- Swordfish
- 独木舟上的旅行--nyoj题目71
- epoll的LT和ET模式
- Telnet 在win7 和 xp中的使用
- 无空格字符串的break-all的性能问题
- 剑指offer(55)链表中环的入口节点
- easyui datagrid去掉全选按钮
- cdqz2017-test10-柚的策略(期望DP &; 组合数学)
- IOS Using UIAlertView to show alerts
- 基于ZooKeeper和Thrift构建动态RPC调用
热门文章
- json写入到excel表
- 160321、ORACLE分页查询SQL语法——最高效的分页
- Java基础之Calendar类、JNDI之XML
- JavaScript面向对象OOP思想Class系统
- C# 使用KingAOP面向切面编程
- 修改Android模拟器的system分区,以及加入SuperSU
- Windows平台下解决Oracle12c使用PDB数据库创建SDE的问题 分类: oracle sde 2015-06-12 11:03 88人阅读 评论(0) 收藏
- 十個必用的 Vim Plugin
- Websocket - Websocket原理(握手、解密、加密)、基于Python实现简单示例
- 再次学习javascript中的參数传递