zoukankan      html  css  js  c++  java
  • 20145236 《Java程序设计》实验二实验报告

    北京电子科技学院(BESTI)实验报告

    课程:Java程序设计

    班级:1452

    指导教师:娄嘉鹏

    实验日期:2016.04.08

    实验名称: Java面向对象程序设计

    实验内容:

    1. 初步掌握单元测试和TDD
    2. 理解并掌握面向对象三要素:封装、继承、多态
    3. 初步掌握UML建模
    4. 熟悉S.O.L.I.D原则
    5. 了解设计模式

    实验目的与要求:
    1.使用TDD的方式设计关实现复数类Complex。
    2.实验报告中统计自己的PSP(Personal Software Process)时间:

    步骤 耗时 百分比
    需求分析 1h 20%
    设计 2h 40%
    代码实现 40min 15%
    测试 40min 15%
    分析总结 30min 10%

    3.实现要有伪代码,产品代码,测试代码。
    4.总结单元测试的好处。

    实验内容、步骤与体会

    一、实验内容:

    (一)使用TDD的方式设计关实现复数类Complex:

    1.伪代码

    请用户输入第一个复数的实部与虚部:
    请用户输入第二个复数的实部与虚部:
    这两个复数的和为:
    这两个复数的差为:
    这两个复数的乘为:
    这两个复数的除为:
    

    2.产品代码

    public class MyUtil{
       public static Complex{
           //两个复数相加
            double real2 = a.getReal();  
            double image2 = a.getImage();  
            double newReal = real + real2;  
            double newImage = image + image2;  
            Complex result = new Complex(newReal,newImage);  
            return result;   
           //两个复数相减
            double real2 = a.getReal();  
            double image2 = a.getImage();  
            double newReal = real - real2;  
            double newImage = image - image2;  
            Complex result = new Complex(newReal,newImage);  
            return result;   
           //两个复数相乘
           double real2 = a.getReal();  
            double image2 = a.getImage();  
            double newReal = real*real2 - image*image2;  
            double newImage = image*real2 + real*image2;  
            Complex result = new Complex(newReal,newImage);  
            return result;
           //两个复数相除
            double real2 = a.getReal();  
            double image2 = a.getImage();  
            double newReal = (real*real2 + image*image2)/(real2*real2 + image2*image2);  
            double newImage = (image*real2 - real*image2)/(real2*real2 + image2*image2);  
            Complex result = new Complex(newReal,newImage);  
            return result;  
       }
    }
    

    3.测试代码

    public class MainClass { // 用于测试复数类
    
    	public static void main(String[] args) {
    		System.out.println("请用户输入第一个复数的实部和虚部:");
            Complex data1 = new Complex();
                    System.out.println("请用户输入第二个复数的实部和虚部:");
            Complex data2 = new Complex();
           
            // 以下分别为加减乘除
            Complex result_add = data1.add(data2);
            Complex result_sub = data1.sub(data2);
            Complex result_mul = data1.mul(data2);
            Complex result_div = data1.div(data2);
            
            result_add.print();
            result_sub.print();
            result_mul.print();
            result_div.print();
    	}
    }
    

    程序源代码

    import java.util.Scanner;
    
    public class Complex { // 复数类
        double real;  // 实部
        double image; // 虚部
    
        Complex(){  // 不带参数的构造方法
            Scanner input = new Scanner(System.in);
            double real = input.nextDouble();
            double image = input.nextDouble();
            Complex(real,image);
        }
    
        private void Complex(double real, double image) { // 供不带参数的构造方法调用
            // TODO Auto-generated method stub
            this.real = real;
            this.image = image;
        }
    
        Complex(double real,double image){ // 带参数的构造方法
            this.real = real;
            this.image = image;
        }
    
        public double getReal() {
            return real;
        }
    
        public void setReal(double real) {
            this.real = real;
        }
    
        public double getImage() {
            return image;
        }
    
        public void setImage(double image) {
            this.image = image;
        }
    
        Complex add(Complex a){ // 复数相加
            double real2 = a.getReal();
            double image2 = a.getImage();
            double newReal = real + real2;
            double newImage = image + image2;
            Complex result = new Complex(newReal,newImage);
            return result;
        }
    
        Complex sub(Complex a){ // 复数相减
            double real2 = a.getReal();
            double image2 = a.getImage();
            double newReal = real - real2;
            double newImage = image - image2;
            Complex result = new Complex(newReal,newImage);
            return result;
        }
    
        Complex mul(Complex a){ // 复数相乘
            double real2 = a.getReal();
            double image2 = a.getImage();
            double newReal = real*real2 - image*image2;
            double newImage = image*real2 + real*image2;
            Complex result = new Complex(newReal,newImage);
            return result;
        }
    
        Complex div(Complex a){ // 复数相除
            double real2 = a.getReal();
            double image2 = a.getImage();
            double newReal = (real*real2 + image*image2)/(real2*real2 + image2*image2);
            double newImage = (image*real2 - real*image2)/(real2*real2 + image2*image2);
            Complex result = new Complex(newReal,newImage);
            return result;
        }
    
        public void print(){ // 输出
            if(image > 0){
                System.out.println(real + " + " + image + "i");
            }else if(image < 0){
                System.out.println(real + "" + image + "i");
            }else{
                System.out.println(real);
            }
        }
    }
    public class MainClass { // 用于测试复数类
    
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("请用户输入第一个复数的实部和虚部:");
            Complex data1 = new Complex();
            System.out.println("请用户输入第二个复数的实部和虚部:");
            Complex data2 = new Complex();
    
            // 以下分别为加减乘除
            Complex result_add = data1.add(data2);
            Complex result_sub = data1.sub(data2);
            Complex result_mul = data1.mul(data2);
            Complex result_div = data1.div(data2);
    
            result_add.print();
            result_sub.print();
            result_mul.print();
            result_div.print();
        }
        }
    
    

    测试结果:

    - 以下是建立的模型:

    - 从模型中可以看见在Complex类中定义了如下方法:

    首先定义了实部real与虚部imaginary,并赋初值0
    - getComplex()方法用于置数
    - add()方法用于两个复数相加
    - multi()方法用于两个复数相乘
    - Minus()方法用于两个复数相减
    - GetRealPatr()方法得到实部
    - GetimaginaryPatr()方法得到实部
    - tostring()方法把复数结合成为a+bi形势的字符串
    

    (二)面向对象三要素

    1.抽象
    "去粗取精、化繁为简、由表及里、异中求同"。抽象就是抽出事物的本质特征而暂时不考虑他们的细节。对于复杂系统问题人们借助分层次抽象的方法进行问题求解;在抽象的最高层,可以使用问题环境的语言,以概括的方式叙述问题的解。在抽象的较低层,则采用过程化的方式进行描述。在描述问题解时,使用面向问题和面向实现的术语。程序设计中,抽象包括两个方面,一是过程抽象,二是数据抽象。

    2.封装、继承与多态
    面向对象(Object-Oriented)的三要素包括:封装、继承、多态。过程抽象的结果是函数,数据抽象的结果是抽象数据类型(Abstract Data Type,ADT),类可以作具有继承和多态机制的ADT,数据抽象才是OOP的核心和起源。

    OOP三要素的第一个要素是封装,封装就是将数据与相关行为包装在一起以实现信息就隐藏。Java中用类进行封装。
    封装实际上使用方法(method)将类的数据隐藏起来,控制用户对类的修改和访问数据的程度,从而带来模块化(Modularity)和信息隐藏(Information hiding)的好处;接口(interface)是封装的准确描述手段。

    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() + "!";
        }
    }
    

    3.设计模式初步
    (1)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,依赖倒置原则)

    (2)模式与设计模式
    计算机科学中有很多模式:

    GRASP模式
    分析模式
    软件体系结构模式
    设计模式:创建型,结构型,行为型
    管理模式: The Manager Pool 实现模式
    界面设计交互模式

    这里面最重要的是设计模式

    (3)设计模式实示例

    设计模式有四个基本要素:

    Pattern name:描述模式,便于交流,存档
    Problem:描述何处应用该模式
    Solution:描述一个设计的组成元素,不针对特例
    Consequence:应用该模式的结果和权衡(trade-offs)

    二、单元测试的好处:

    • 减少问题出现,提高程序质量,在测试中进行项目,边改边写,可以将自己设计的项目不断优化

    • 更快地定位问题出现在哪个模块,提高效率,减少成本,这有助于实现模块的“块内高内聚,块间低耦合”

    三、心得体会:

    这次实验较第一次实验而言,增加了软件的利用性,不仅要编辑、编译、运行、调试代码,还要建模,单元测试,所以这次实验遇到的最大问题就是软件的使用方面,安装,运行,每一步都有问题,首先是StarUML的安装,在下载完了之后不知道如何安装,好不容易安装好之后发现不能运行,感觉这次实验真的好复杂,有时候在想,不就就是编程嘛,咋就这么麻烦呢,难道编译好代码直接运行代码不行么!也许是自己接触的编程太少太浅显,还没有达到能将编程运用自如的境界,体会不到其中的便利。

    恍惚中,时光停滞,岁月静好。。。
  • 相关阅读:
    How to solve the problem "A project with an Output Type of Class Library cannot be started directly "
    Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
    JavaScript实现startWith、endWith效果函数
    不同格式证书导入keystore方法
    用KeyTool生成安全证书
    怎样让操作系统的虚拟机退出全屏?
    对https的理解
    JDK中keytool常用命令
    百折不回结局凄惨的金庸反角
    主题数据库的特征
  • 原文地址:https://www.cnblogs.com/feng886779/p/5393489.html
Copyright © 2011-2022 走看看