Java Experiment II

Source: Internet
Author: User

Course: Mobile Platform Application development Practice class: 9217

Name: Cai Binsi No.: 20159217

Score: Instructor: Lou Jia Peng Experimental Date: 2015.10.11

Experiment level: Preview degree: Experiment time:

Instrument Group: Compulsory/Elective: Elective experiment number: 1

Experiment Name: Java Object-oriented programming

Experimental purposes and requirements:

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

Experimental requirements:

1. Students who do not have a Linux base are advised to start with the Linux Basic Primer (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 experiment report focuses on the results of the operation, the problems encountered (tool find, installation, use, program editing, commissioning, operation, etc.), solutions (empty methods such as "Check network "," Ask classmates "," read books "and so on all get 0 points) and analysis (from which can get what revelation, what harvest, lessons, etc.). The report can refer to Fan Fei's guidance.

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

4. please first in the laboratory building in the ~/code directory with their own school number to create a directory, code and UML diagram to put in this directory, no study number will be required to redo, and then follow the following steps to practice.

Experimental instrument:

Name

Model

Number

Pc

1

Virtual machines

Laboratory Building

1

Experimental steps:

(a) Unit testing

(1) Three kinds of code: pseudo Code, product code, test code

Take "percentile to five cent system" as an example, introduce three kinds of code:

Pseudo-code (the purpose of using pseudo-code is to make the described algorithm can be easily implemented in any programming language, using pseudo-code, do not rigidly adhere to the specific implementation)

/*

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"

*/

Product Code (pseudo-code in the form of Java statements)

Test code (a 测试 set of test inputs, execution conditions, and expected results for a particular goal to test a program path or verify that a specific requirement is met)

In the specific test, found two bugs, one is in the judgment failed to require greater than or equal to zero, the other is not considered 100 of the situation. So add grade<0 error and modify grade<100 to grade<=100. Therefore, in general, the test code will be longer than the product code.

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

Test-driven Development (TDD): Write first 测试代码 , then write, and then write 产品代码 the code that is correct

The steps 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

Refactor the code and ensure the test passes (refactoring the next lab session)

Cycle through the development of all functions

EclipseFile->New->Java Project-A new TDDDemo Java project

Tdddemo-> Right-click to New->Source Folder Create a new test directorytest

TestRight-click New->JUnit Test Case New test Case classMyUtilTest并输入代码(由于没有建立MyUtil类以及percentage2fivegrade方法,所以有红字出现):

在TDDDemosrcnew class in the directory MyUtil , and implement the Percentage2fivegrade method:

We put the mouse on the Myutiltest.java, right-click, select Run As->junit Test. The test results showed a red bar indicating that the test failed:

The problem appears in line tenth 55 hope results are not passed, but no specific code, so can only report errors, so to modify the Myutil.java, once again to test, got a green bar, indicating that the test has passed:

On this basis we extend the test to optimize, increase the exception case and the boundary use case, after the error has added a statement appears green bar:

The coding rhythm of TDD is:

• Add test code, JUnit appears red bar

• Modify the Product code

· JUnit appears green bar, Task complete

(ii) Object-oriented three elements

(1) Abstract

Abstract refers to the abandonment of the appearance of things and the extraction of essential factors in the cognitive thinking activities. includes two aspects, one is the process abstraction, the other is the data abstraction. The result of the process abstraction is the function, and the result of the data abstraction is the abstract data type (Type,adt)

(2) encapsulation, inheritance and polymorphism

Object-oriented three elements: 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.

Encapsulation actually uses methods to hide the data of the class, controlling the user's modifications to the class and the extent to which the data is accessed, resulting in modularity (modularity) and information hiding ( Information hiding) , interface (interface) is an accurate description of the package.

First try the package:

We can use the class diagram in UML to describe the class dog, first we open the shell in the environment of the experimental building, enter Umbrello in the command line, open the UML modeling software Umbrello.

The model after UML modeling is as follows:

The corresponding test code and running results are as follows:

The practice found that there are a lot of redundancies in Dog.java and Cat.java, so redefine the inheritance representation in UML:

(iii) Preliminary design pattern

(1) S.o.l.i.d

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, in the abstract, s.o.l.i.d design principle is a good guide

Based on the OCP use of object-oriented polymorphism (polymorphic), more flexibility in handling change-embracing changes OCP can be achieved by: (1) abstraction and inheritance, (2) interface-oriented programming

LSPThe core idea is that the parent type object can be overridden by the quilt type Object

(2) mode and design mode

Pattern is the usual solution to a particular problem in an external environment, the most important of which is the design pattern

(3) design pattern Real Example

Design patterns provide 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

Design and implement the plural class complex using TDD method

Pseudo code:

A complex class is designed to represent the real and imaginary parts of a complex number as variables.

The addition and subtraction of complex numbers is realized by adding and decreasing the real and imaginary parts respectively.

The plural is printed in the plural format.

Product Code:

Steps

Take

Percentage

Demand analysis

0.5h

10%

Design

1h

20%

Code implementation

1.5h

30%

Test

1h

20%

Analysis Summary

1h

20%

The benefits of unit testing:

The quality of the unit-tested code can be guaranteed.

The problems found by unit tests are easy to locate.

Modify the code to make mistakes, after unit testing easy to find.

(v) Summary

Experiment Harvest:

Although the experiment took a lot of time, but I also gained a lot. First of all, through this experiment, I am more familiar with the use of virtual machines, but also more adapted to the experimental mode, for the virtual machine code coding more familiar and rapid. Learn to unit test this convenient and high-quality test method, while the experiment step-by-Step guide me to learn to deal with the possible problems, but also taught me later in the process of writing to consider the various possibilities to improve the security of the code.

The problems and solutions that arise:

First, because of the intranet reasons, the experimental building of the UML is not installed, by asking teachers and their own practice, with the UPDATE statement and--fix-missing statement successfully installed.

Secondly, because the network is very slow, the experimental building is always card, so wasted a long time, the solution is I under Windows under a eclipse, and then with their own eclipse code, saving unnecessary time wasted, greatly accelerated speed.

Java Experiment 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.