BUAA_OO_2020_UNIT1_Summary
The first task for the whole unit is given a string of a string, it is determined whether normative, and according to certain rules to parse a string expression, and outputs the result of the expression is evaluated guide
First, program analysis
The first job
The first operation is relatively simple, containing only an expression with a power factor function term is composed, and does not involve determination WF
1. Design ideas
Overall structure
- The power function with coefficients abstract class Item, containing two variable items and management classes BigInteger coefficients
- The expression is an abstract class Polynomial, with a number of container management HashMap
- Factory mode analysis using a simple string and generates an object
Input processing
- Because they do not involve WF, all blank characters removed
- The +, -, + -, - + are replaced +, +, -, -, ease of handling
Output processing : print method Polynomial object method call print output of each Item object container
Simplification
- Item print method, the coefficients are 1 or -1, the index is 1 or 0 special handling, etc.
- Polynomial print method, a priority term to find the maximum output coefficient, so that the first positive output positive entry items, to avoid increasing the length of the output minus sign at the beginning of the expression
UML 图
2. The code quality analysis
Metrics: Simplification when the output section, and therefore the print method of Item high complexity
code lines: total number of lines of code 167
DesigniteJava analysis tool
By means of the above analysis result, it can be seen, because the first operation is relatively simple, code complexity is not high, it is preferred degree of coupling, the complexity of the method are also low, only the output method thus involves high complexity simplification
Second job
The second part of the job and add a trigonometric judgment, taking into account backwards compatibility, they reconstructed the architecture, making the overall architecture has good scalability. In addition, Jane second part of the job is also a major difficulty
1. Design ideas
Overall structure
- Hierarchical design six entity class, Expression, Term, Constant, Power, Sine, Cosine, six categories are implemented Function interface to be achieved by the method of derivation takeDerivative (); in addition Power, Sine, Cosine needed to achieve Multiply interfaces to objects can be managed Term
- Expression containers used HashMap class management objects Term
- Term used to achieve the management class HashMap factor Multiply interface objects, contains a class variable storage modulus BigInteger: when put constant term, it is multiplied by the coefficient which is not put into the Map
- Abstract factory parse the string and generates an object
- Use the search deep pruning in-depth simplification
Input processing
- In accordance with the instructions hierarchical structure of regular expressions, the judge legally remove whitespace characters, replace the continuous sign
- The treated plants to generate a character string object, WF is determined (e.g., the absolute value of the index exceeds the range, etc.) when generating an object, an exception is thrown if WF, otherwise the resulting object.
Output processing :
- Each instance of a class replication method toString
- Power and other factors in accordance with the output request class for replication, and reduces to 0 for other special circumstances Index
- Term and then the HashMap toString toString each combination coefficient as a factor toString Term object, while coefficients for special situation simplifies to 1 or -1, etc.
- Expression will HashMap each item as a combination thereof toString toString, special cases of simplification
Simplification related
Generating object part: when put into the Map Term factor, if the same key value is detected, Multiply is implemented call () method combined simplification; similarly be combined to simplify when put items in Expression
Search simplification deep part:
The triple well structure each expression, in accordance with the same indices x are classified into (factor, sin index, COS index)
X indices for the same two triplets, prune deep search profile according to six kinds of conversions below, the new search expression complete length shorter than the original expression obtained is replaced.
① (a, m, n) + (b, m, n+2) --> (a+b, m, n) + (-b, m+2, n)
② (a, m, n) + (b, m, n+2) --> (a, m+2, n) + (a+b, m, n+2)
③ (a, m, n) + (b, m+2, n) --> (a+b, m, n) + (-b, m, n+2)
④ (a, m, n) + (b, m+2, n) --> (a, m, n+2) + (a+b, m+2, n)
⑤ (a, m, n+2) + (b, m+2, n) --> (a, m, n) + (b-a, m+2, n)
⑥ (a, m, n+2) + (b, m+2, n) --> (a-b, m, n+2) + (b, m, n)
Note the set time threshold fuse: program runtime exception is thrown when more than a specified value
Simplification output section: toString in each class, a special case for simplification
UML 图:
- Data Management section
- Plant sections
- Simplification deep search section
2. The code quality analysis
Metrics:
code lines: a total of 807 line
DesigniteJava analysis tool:
As can be seen by the above analysis tool, thanks to the better division of labor was planned and classes, methods and processes in the data storage portion complexity and complexity classes are preferred. However, in the deep part of the optimization search, since a recursive process complexity is greater. Coupling between the overall class of preferred
The third operation
The third job was added and the nesting factor expression factor, and full WF determination. Therefore, the focus of this work is that the architecture design, data processing, for optimization, only a relatively simple, whatever optimization
1. Design ideas
Overall architecture design continues second job design, only some extensions and optimization; parse the string part due to the presence of nested factor and factor expression, more Extended
Overall structure
- Design of
Function
the interface, comprising aFunction takeDerivative()
derivation method; designedFactor
interface extendsFunction
the interface, comprising avoid multiply(Factor another)
multiplication combining method andString keyOfMul()
a method (the obtained key value of the Map Term) - Entity class aspect, on the basis of the job 2 is increased
Nested
nesting factors, members comprising two variables:private Factor outer
andprivate Factor core
, respectively, the management of nested outer and inner layers factor factor. While theExpression、Constant、Nested
class implements theFactor
interface. FurtherExpression
class adds the index variable for the case of managing a plurality of input multiplied by the same factor expression - Continuation of the second operation, the abstract factory mode analysis using input and generating object to increase
Format
the class, the string that some memory built-in static variables, and static methods with strings - To ensure safety, so that all entities class implements
Serializable
the interface, in order to achieve the object by cloning a sequence of streaming and deserialization. While increasing theCloneUtil
class, the entity class object built Some clones Static method
- Design of
Input processing
- Increasing the pretreatment step, the replacement string is the outermost brackets "$ x", and stores the contents to be replaced with a
StringList
passed down. Alternatively while checking the matching of brackets - Second positive continuation of operations of a string expression, increasing the expression factor
($x)
, the original triangle regular changesin($x)
of form, after treatment to determine the legality of expression: if not legal, throws an exception; otherwise de-blank character and continuous positive and negative number processing, and said processing resultStringList
is transmitted to the object generation plant - Generating object factory recursive mode, and exceeds the defined instructions to check whether each layer is legal, and the corresponding analytical data: If no, the resulting object is returned; otherwise thrown
- Increasing the pretreatment step, the replacement string is the outermost brackets "$ x", and stores the contents to be replaced with a
Output processing : the continuation of work two ways, be extended to the following points
Expression
As a factor which needs to add the outer parentheses toString; index when it is not 1, since the instruction requires a power of expression can not contain factor, and therefore need toString becomes "() * () * () ... "form- ToString factor is nested,
outer.toString().replaceAll("x", core.toString())
Simplification related
- Parsing the input section: stripping the outer nested extra brackets, as will be "((((((x))))))" reduces to "(x)"
- Generating portion of the object: the continuation of operations of similar items merged two (addition or multiplication) simplification, while expanding at two points, to reduce unnecessary nesting
- Term class object to put in a factor exp term expression, contains only one if exp t, t and the term will be combined, rather than put into the exp term factor as to reduce nesting
- When put an item in the Expression class object term exp, when a term contains only factor E expression, and the coefficient term is 1 (or -1), then e (or -e) exp combined with, rather than as the term exp items put into the reduced nesting
- Output section: II continued operation mode, the processing of the special cases
UML图 :
- Data Management section
- Plant sections
2. The code quality analysis
Metrics
code lines: a total of 1007 line
DesigniteJava analysis tool:
Seen by the above results, as the complexity of the operation increases, the complexity of the classes and methods also increased. Wherein PowerMachine, TermMachine, TriMachine large number of cycles and the need to generate objects is determined, the average complexity is high. Format Because of the many built static variables and static methods complex, complexity and coupling draw higher. Since the class Term Expression and core data processing, higher overall complexity class. In addition, the complexity of the coupling with other classes were good
Overall, the job due to the expansion of the second job aside, almost no part of the reconstruction, mainly for expansion. Class and because better planning, the overall complexity and coupling are good.
Second, testing and bug
1. beta:
This means no job bug is found in the sensing and in the intensity measurement. Wherein the first and the third due to the optimized better performance is obtained points out; the second operation since the optimization is not thorough enough(Testing methods in question)Obtain 97.7583 points.
2. Mutual Testing:
It is hack :
Using the first operation TreeSet sort the items according to the size factor, since the container and the interface Comparable TreeSet deep understanding, resulting in the same output of a coefficient term only, thereby hack the bug
The second and third times were not to be a bug hack
hack:
The first hack work out of the bug are:
Coefficient is 1 or -1 when it is omitted, the coefficient multiplication sign is omitted, resulting in the output WF
The part of the data output bit exponent symbol ^, does not comply with instructions, specifications, output WF
Continuous sign handling problems, the - - x -x identified as
Details of the process are issues
The second hack work out of the bug are:
- The x * x ** 10000 identified as WF, on the instructions of said absolute value of the index over 10,000 deep understanding
- Comprising an input (sin (x) ** 2 + cos (x) ** 2) ** n when expanded, an error occurs, the output processing of the problem
- Part of the data will be thrown
java.util.ConcurrentModificationException
exception, the check code is found to be in the inner loop of the double loop iterations in HashMap, due Remove elements
Third hack work out of the bug are:
- Multiplying the plurality of processing the same expressions (e.g., (x + 1) * (x + 1) * (x + 1)), will be treated as the first power (i.e., as (x + 1) Processing)
- The COS (0) reduces to 0
The main idea of hack are:
- Record their encounter in the process of writing their own code and bug in the bug data possible
- Black box, get the wrong result after gradually resolved
- Read the code, precision hack
Third, the design mode
The main use of the factory model
In the first operation, using a simple factory mode to parse the input string, since relatively simple, backward compatibility is not considered
In the second operation, due to the complexity of parsing the string, and considering the backward compatibility, using the abstract factory model: interface design Machine
, built-in method Function getFunc(String str) throws WrongFormat
, and then various specific design implemented Machine Machine
Interface, captured through a regular generating a character string corresponding object. A design Factory
class, HashMap manage these through Machine
the object is instantiated, the object is obtained by way of the need to produce an input command
In the third operation, the continuation of the second operation mode of the plant, for polynomial nesting factor and corresponding expansion factors
Transparent mode so that the creation of the factory to a user, the business logic code does not need too much attention to how the object is generated by the input coupling reduces the complexity of business logic and program code
While the factory model is conducive to the expansion, when there is a new product object corresponding production design and implement an interface to the class
Fourth, feelings and experiences
We need to think more about the problem from the design level
Needs hierarchically structured, the program design to object-oriented architecture, in order to leave a better scalability
Need to make good use of discussion forums, more than exchange students. I have three jobs that huge gains in student exchanges, including:
- The idea to build a black-box testing machine
- The second job trigonometric deep search optimization ideas and tips x ** 2 reduces to x * x's
- Understand the various concepts
- Some bug inspired more difficult to find
All in all, this makes my job three times for java inheritance, interfaces, polymorphism, have a further understanding of the containers; in the design, how to make the degree of coupling and complexity of the code is better, how to set aside more scalability, It has also been further thought