Java Lab Report II

Source: Internet
Author: User

Beijing Institute of Electronic Technology (BESTI)

Real Inspection report

Lesson: Java Programming Experiment class: 1353 name: Fu Yunjin No.: 20135323

Score: Instructor: Lou Jia Peng Experimental Date: 2015.5.8

Experimental classification: No preview degree: Experimental time: 15:30~18:00

Instrument Group: 23 Compulsory/elective: Elective experiment number: 2

Experiment Name: Java Object-oriented programming

Experimental purposes and requirements:

Objective: 1. Initial mastery of unit testing and TDD

2. Understanding and mastering the object-oriented three elements: encapsulation, inheritance, polymorphism

3. Initial mastery of UML modeling

4. Familiarity with S.O.L.I.D principles

5. Understanding Design Patterns

Requirements:

1. Students who do not have a Linux base are advised to start with the Linux basics (new version) Vim Editor course

2. Complete the experiment, write the experiment Report, the experiment report is published in the blog Garden, note that the experimental report focuses onRun Results, encountered theproblem(Tools find, install, make , program editing, commissioning, running, etc.),Solutions(empty methods such as "Check Network", "Ask classmates", "reading" and so on are 0 points) andAnalysis(from which you can get To what revelation, what gains, lessons, etc.). The report can refer to the guidance of Fan Fei Dragon Teacher

3. Plagiarism is strictly forbidden, and the results of the experiment of the perpetrator are zero, and other punitive measures are added.

 

Name

Model

Number

Pc

SONY

1

Virtual machines

Laboratory Building

1

Experiment contents, steps and experiences (attached paper):

(a) Unit test (1) three codes

Programming is intellectual activity, not typing, what to do before programming, how to do to think clearly to write the program, write well. With a lot of students at present, a program to open the editor to write code, I hope that students develop a habit, when you want to solve problems with the procedure, you will write three kinds of code:

    • Pseudo code
    • Product Code
    • Test code

We first write 伪代码 , pseudo-code can be written in Chinese, recommend that you write in English, pseudo-code and specific programming language independent, do not write the specific programming language grammar-related statements (such as 用malloc分配内存 This can only be programmed in C language), 伪代码 from the level of intent to solve the problem, ultimately, 伪代码 is 产品代码 The most natural and best notes. For the above question, we can solve this problem by pseudo code:

Percentile five-point system: If the score is less than 60, turn into "fail"

If the score is between 60 and 70, turn into "pass"

If the score is between 70 and 80, turn into "medium"

If the score is between 80 and 90, turn into "good"

If the score is between 90 and 100, turn into "good"

Other, turn into "error"

There 伪代码 , we translate in a specific programming language, is available, of 产品代码 course, we have to choose Java in this, a piece of cake, translation good MyUtil.java as follows:

Here we have designed a 测试用例(Test Case) 测试用例 set of test inputs, execution conditions, and expected results for a particular goal to test a program path or verify that a particular requirement is met. Here our test input is "50", the expected result is "fail". Running the results in Eclipse is as follows, and the test results match the expected:

Only a set of input tests is not sufficient, we test the general situation, the code is as follows:

We can not just test the normal situation, the following to see how the abnormal situation, such as input is negative or greater than 100, the code is as follows:

Run the program found that negative ticks and expectations are inconsistent, and finally found a bug, the reason is that when the failure of the judgment did not require a score greater than 0. We revise MyUtil.java and increase the judgment of the negative points, the code is as follows:

Have you had enough testing? Not enough, the general code is most prone to error at the boundary, we have not tested the boundary condition, and we have tested the boundary conditions that are entered as "0,60,70,80,90,100" in the following code:

We found a bug when entering 100 in the boundary condition. We have modified the MyUtil.java condition of judging excellent to include the case of input 100, the code is as follows:

At this time the test is in line with expectations, we give MyUtil.java others to use, the heart is more confident. How do you ensure that the unit measure is adequate? Our general requirements are 测试代码 more than that 产品代码 . How to write the test, "the way of unit testing" put forward Right-BICEP the method, we can refer to.
Software is done by the cooperation of many people, and the work of different people is dependent on each other. Many of the errors in the software come from the programmer's misunderstanding of the function of the module, negligence or the failure to understand the module changes. How can I be responsible for the module function definition as clear as possible, the internal changes of the module will not affect the other modules, and the quality of the module can be stable, quantitative guarantee? Unit testing is a very effective solution.

(2) TDD (test driven devlopment, testing-driven development)

The general steps for TDD are as follows:

    • Clear the current functionality to be completed and record it as a test list
    • Quick completion of writing test cases for this feature
    • Test code compilation does not pass (no product code)
    • Write the Product Code
    • Test Pass
    • Refactor the code and ensure the test passes (refactoring the next lab session)
    • Cycle through the development of all functions

In the TDDDemo project, we put the mouse on the project name TDDDemo , right-click, in the pop-up menu selected to New->Source Folder create a new test directorytest。

We put the mouse test on the directory, right-click, in the pop-up menu selected to New->JUnit Test Case create a new test case class MyUtilTest , such as:

We add the first test case testNormal , note that before the test case must have annotations @Test , test case Method name arbitrary, enter the following code:

The test results showed a red bar, indicating that the test failed, the red bar summarized the test situation, ran a test, no errors, a test failed. The following reasons are also very clear: the test code tenth incoming 55 o'clock, the expected result is "failed", the code returned "error", modify MyUtil.Java it, enter the following code:

A green bar was shown, indicating that the test passed. The goal of TDD is "clean Code that Works", the slogan of TDD is "Keep the bar Green, to Keep the Code clean"

The coding rhythm of TDD is:

    • Add test code, JUnit appears red bar
    • Modify the Product Code
    • JUnit appears green bar, Task complete

We add a use case to test for abnormal conditionstestException,我们增加一个测试边界情况的用例testBoundary

II) object-oriented three elements (1) abstract

Abstract the meaning of the word refers to people in the cognitive thinking activities in the object of the appearance of the factors of abandonment and the essence of the extraction of factors . Abstract is a kind of thinking tool that people often use when they know complex things and phenomena, abstract ability of abstract thinking is very important in program design,"refine, simplifying, youbiaojili and seeking the same in different ways" has a great extent to determine the programmer's programming ability.
Abstraction is a matter of extracting the essential features of things and not considering their details for the time being. For complex system problems, we can solve problems by means of hierarchical abstraction. At the highest level of abstraction, the solution of the problem is described in a generalized way, using the language of the problem environment. In the lower layers of abstraction, they are described in a procedural way. When describing a problem solution, use a problem-oriented and implementation-oriented terminology.
In program design, abstraction consists of two aspects, one is process abstraction and the other is data abstraction.

(2) encapsulation, inheritance and polymorphism

The three elements of object-oriented (object-oriented) include: encapsulation, inheritance, polymorphism. Object-oriented thinking involves all aspects of software development, such as object-oriented analysis (OOA), Object-oriented design (OOD), and object-oriented programming (OOP). OOA decomposes The system according to the abstract key problem domain and focuses on what. Ood is an object-oriented implementation of the symbolic design system, which constructs the system into a "real-world" object in a way that is very close to the problem domain terminology, and focuses on howto implement the functional specification through the model. OOP is coded in a programming language (such as Java) on a design basis. The main line that runs through OOA, Ood, and OOP is abstraction.
Ood Modeling uses the graphical Modeling language UML (Unified Modeling Language), UML is a generic modeling language, we experiment with Umbrello for modeling, and Windows recommends that you use STARUML.

The result of a process abstraction is a function, and the result of the data abstraction is an abstract data Type,adt, which can be used as an ADT with inherited and polymorphic mechanisms. Data Abstraction is the core and origin of OOP.

The first element of the OO three elements is encapsulation, which is the encapsulation of data and related behaviors to be wrapped together to implement information hidden.

    • + indicates public
    • #表示 protected
    • -Indicates private
(iii) Preliminary design pattern

(1) S.O.L.I.D principle

Object-oriented three elements are "encapsulation, inheritance, polymorphism", and any object-oriented programming language will support these three elements syntactically. It is very difficult to use the three elements, especially polymorphism, with the help of abstract thinking, and the S.O.L.I.D design principle is a good guide:

    • SRP (Single Responsibility Principle, sole responsibility principle)
    • Never have more than one reason to modify a class
    • OCP (open-closed Principle, open-closed principle):
    • Software entities (classes, modules, functions, etc.) should be open to expansion and closed to modifications.
    • LSP (Liskov substitusion Principle,liskov substitution principle)
    • A subclass must be able to be used by its base class, using a pointer to a base class, or a referenced function, and must be capable of using it without knowing the specific derived class object type
    • ISP (Interface segregation Principle, interface separation principle)
    • Customers should not rely on interfaces that they do not use
    • DIP (Dependency inversion Principle, dependency inversion principle)
    • High-level modules should not be dependent on low-layer modules. Both should be dependent on abstraction, and abstraction should not depend on detail. Details should depend on abstraction
      • (2) mode and design mode

    • A pattern is a customary solution (solution) for a particular problem (problem) under an external environment (context). The pattern must make the problem clear, explain why it is used to solve the problem, and under what circumstances it will not work, and each pattern can be reused because of its repeatability, having its own name, being able to impart it, and transplanting it into different scenarios. Patterns can be seen as expert solutions to a problem that can be reused . There are many patterns in computer science:

      • Grasp mode
      • Analysis mode
      • Software Architecture Patterns
      • Design Patterns: Created, structured, behavioral
      • Management mode: The manager Pool implementation mode
      • Interface Design Interaction Mode
      • ...

      The most important thing here is the design pattern , the position of design pattern in object-oriented can be equal to the position of data structure in process programming.

    • (3) design pattern Real Example

      Design pattern provides a subsystem or component for refining a software system, or a diagram between them, which describes the public rendition structure of a communication component, which can solve a design problem in a particular context.
    • (iv) Practice
      • 1 use TDD to design the complex to implement the plural class.
          • Pseudo code:
          • 1. The real part of the complex number is 0, the imaginary part is 0
          • 2. The real part of the plural is 0, the imaginary part is the corresponding number
          • 3. The real part of the plural is the corresponding number and the real part is 0
          • 4. Two parameters are real and imaginary parts respectively
          • 5. Realize the addition, subtraction and multiplication of two complex numbers.
          • Test code:
          • public class Complextext
            {
            public static void Main (string[] args)
            {
            Complex c=new Complex ();
            Complex c1=new Complex (4,5);
            Complex c2=new Complex (6,7);
            C1. Print ();
            C2. Print ();
            System.out.println ("These two plural and for:");
            System.out.println (C.jia (c1, C2). A + "+" +c.jia (c1, C2). B + "I"). ToString ());
            System.out.println ("These two complex differences are:");
            System.out.println (C.jian (c1, C2). A + "+" +c.jian (c1, C2). B + "I");
            SYSTEM.OUT.PRINTLN ("The product of these two complex numbers is:");
            System.out.println (C.cheng (c1, C2). A + "+" +c.cheng (C1,C2). B + "I");

            }
            }

          • Product Code:
            • public 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 Jia (Complex p1,complex p2)
              {
              Complex P =new Complex (p1.a+p2.a,p1.b+p2.b);
              return p;
              }
              Complex Jian (Complex p1,complex p2)
              {
              Complex P =new Complex (p1.a-p2.a,p1.b-p2.b);
              return p;
              }
              Complex Cheng (Complex p1,complex p2)
              {
              Complex p = new Complex (p1.a*p2.a-p1.b*p2.b,p1.b*p2.a+p1.a*p2.a);
              return p;
              }void Print ()

            • {
              System.out.println ("The value of the plural is:");
              if (this.b!=0)
              System.out.println (this.a+ "+" +this.b+ "I");
              else System.out.println (THIS.A);
              }


              }

            • Test results:
              • 2. Count your PSP (Personal software Process) time
              • Steps

                Take

                Percentage

                Demand analysis

                10 11.1%

                Design

                15 16.7%

                Code implementation

                50 55.6%

                Test

                10 11.1%

                Analysis Summary

                5 5.6%
              • 3. Summarize the benefits of unit testing
              • ①, more explicit development purposes, to avoid duplication or too much lengthy work, improve the productivity of developers.
              • ②, code clearer and more modular. It is easier to maintain and modify the code when there is a problem with it.
              • ③, within the product code, the individual classes are independent from each other, can guarantee the independence of the code and easy to re-add or reduce.
              • ④, separating the product code from the test code, makes it easier for users to understand.
              • 4. Problems encountered
              • ①, their own machine can not run Umbrello, and then from the teacher copied into the Umbrello with the virtual machines.
              • ②, run Umbrello, abstract class can not run, after repeated review of the experiment only to find that forget to remove the public only option.
              • In the environment of ③ and experimental buildings, Chinese input cannot be used, then Chinese input will be converted into corresponding English input.
              • ④, for the complex class exercise, run as can not appear after the corresponding results, found that the test class name is inconsistent with the corresponding file name, the next time you should try to subtract such a low-level error.
              • 5. Experimental experience and Impressions:
              • This is the second Java experiment of the semester, compared to the first experiment, our operation is more mature and the experiment is also more difficult. For this experiment, I understand what a UML diagram is, and understand that it can be converted to code after the experiment. The last experimental solid principle, let me have a further understanding of the principle of TDD, let me develop good programming habits have an important role. And, in the future experiment should pay more attention to their own thinking training and so on.

Java Lab Report II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.