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!!!");
}
}
}
}
结果:
20144303 《Java程序设计》第二次实验实验报告的更多相关文章
- 201621123005《Java程序设计》第九次实验总结
201621123005<Java程序设计>第九周实验总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 ...
- Java程序设计第四次实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程:java程序设计 班级:1352 姓名:何伟钦 学号:20135223 成绩: 指导教师:娄嘉鹏 ...
- 201621123005《Java程序设计》第十次实验总结
201621123005<Java程序设计>第十周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 1. 常用异常 1.1 自己 ...
- 201521123008<java程序设计>第三周实验总结
1.本周学习总结 2.书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; pub ...
- 201521123063 JAVA程序设计 第二周学习总结
1.本周学习重点(2.27-3.5) java中的数组 以二维数组为例,数组名为scores,则 (1)先声明数组 int[][] scores;或int scores[][];或int[] scor ...
- 20145304 刘钦令 Java程序设计第二周学习总结
20145304 <Java程序设计>第2周学习总结 教材学习内容总结 java可区分基本类型和类类型(即参考类型)两大类型系统. 基本类型主要可区分为整数.字节.浮点数.字符与布尔. 整 ...
- java程序设计第二次实验报告
北京电子科技学院(BESTI) 实验报告 课程:数据结构 班级:1352 姓名:何伟钦 学号:20135223 成绩: 指导教师:娄嘉鹏 实验日期: ...
- java程序设计第二课
抽象基类和接口 能够使用keywordabstact来创建抽象类,该抽象类不能被实例化 也能够使用keywordabstact来描写叙述一个尚未被详细实现的方法,该方法不能包括方法体 一个抽象方法仅仅 ...
- java程序设计第二次作业
- 20165206 2017-2018-2《Java程序设计》课程总结
20165206 2017-2018-2<Java程序设计>课程总结 一.每周作业链接汇总 预备作业1:对师生关系的看法和期望 预备作业2:c语言基础和学习技能的理解 预备作业3:Linu ...
随机推荐
- LeetCode-Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- SpringBoot--属性加载顺序
属性加载顺序: 1.在命令行中传入的参数: 2.SPRING_APPLICATION_JSON中的属性:SPRING_APPLICATION_JSON是以JSON格式配置在系统环境变量中内容: 3.j ...
- 基于注解的形式配置Bean
基于注解的方式配置Bean:也就说我们在每个Bean的类名前面注解一下,Spring会自动帮我们扫描Bean放进IOC容器中 I基于注解的方式配置Bean(没有依赖关系的Bean)有两个步骤: 1组件 ...
- CH5101 LCIS【线性dp】
5101 LCIS 0x50「动态规划」例题 描述 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们研究最长公共上升子序列了 ...
- Ubuntu16.4下RStudio1.1.447 中文输入问题的解决方案
Ubuntu16.4下RStudio1.1.447 中文输入问题的解决方案参照:https://blog.csdn.net/matteoshenl/article/details/78603528 R ...
- 【转载】Java并发编程:volatile关键字解析
http://www.cnblogs.com/dolphin0520/p/3920373.html
- md5sum检验MD5值
md5sum命令检验MD5值 md5sum命令用于生成和校验文件的md5值.它会逐位对文件的内容进行校验.是文件的内容,与文件名无关,也就是文件内容相同,其md5值相同.md5值是一个128位的二进制 ...
- JUnit4.12 源码分析之Statement
1. Statement 抽象类Statement作为命令模式的Command,只有一个方法 各种Runner作为命令模式中的Invoker,将发出各种Statement,来表示它们运行JUnit测试 ...
- linux页缓存
2017-04-25 本节就聊聊页缓存这个东西…… 一.概述 页缓存是一个相对独立的概念,其根本目的是为了加速对后端设备的IO效率,比如文件的读写.页缓存顾名思义是以页为单位的,目前我能想到的在两个地 ...
- MySQL优化(三):优化数据库对象
二.优化数据库对象 1.优化表的数据类型 应用设计的时候需要考虑字段的长度留有一定的冗余,但不推荐很多字段都留有大量的冗余,这样既浪费磁盘空间,也在应用操作时浪费物理内存. 在MySQL中,可以使用函 ...