Chapter 2, Principles of programming and software Engineering
Specify Each Methods Purposes, Assumptions, Input, and Output
* What data within the objects is utilized by the methods? * What does the method assume? * What actions does the method perform, and is the data stored in the objects changed after the method executes? Thus you should specify in detail the assumptions, input, and output for each method. Example: Provide a method for a shape object that moves it to a new location on the screen, you might write the following Specifications: - The method will receive an (x, y) coordinate. - The method will move the shape to the new location on the screen.
Questions That You Must Answer As You Write The Specifications For The Software:
* What is the input data? * What data is valid and what data is invalid? * Who will use interface should be used? * What error detection and error messages are desirable? * What assumptions are possible? * What error detection and error messages are desirable? * What assumptions are possible? * Are there special cases? * What is the form of the output? * What documentation is necessary? * What enhancements to the program are likely in the future?
The Java assertion statement has two forms
*assert* /booleanExpression;/ *assert* /booleanExpression : valueExpression;/ In the first form, if /booleanExpression is false, an /AssertionError/ is thrown with no further detail information. In the second form, if /booleanExpression/ is false, the /valueExpression/ is evaluated and sent to the /AssertionError/ constructor so as to provide more details information detailed information about the failed assertion. In many instances, the /valueExpression/ is simply a string that describes the problem. Here is a simple example of an *assert* statement in a program: So if a value out of range is entered by the user, a message similar to the following will appear. "Exception in thread "main" java.lang.AssertionError: Score is -23 is not in range 0-100 at AssertionClass.main(AssertionClass.java:9"
Phase 3: Risk Analysis
Building software entails risk. Some risks are the same for all software projects and some are peculiar to a particular project. You can predict and manage some, but not all, risks.
A Solution:
Consist of two components: algorithms and ways to store data. An Algorithm is a step-by-step specification of a method to solve a problem within a finite amount of time.
Phase 4: Verification
Formal, theoretical methods are available for providing that an algorithm is correct.
Phase 1: Specification
Given an initial statement of the software's purpose, you must specify clearly all aspects of the problem. The Specification Phase requires that you bring precision and detail to the original problem statement and that you communicate with both programmers and non-programmers.
Highly Cohesive
Highly cohesive methods each perform one well-defined task. Classes should also be designed so that objects are highly cohesive. Cohesion is the degree to which the data and methods of an object are related. Ideally, each object should represent one component in the solution. Methods within an object should be highly cohesive, each should perform one well-defined task.
Highly Coupled
If every object in a program is connected to every other objects in the program, that is called highly coupled and means that the flow of information between objects is potentially high.
Loosely Coupled
Objects are independent Classes should be designed so that the objects are independents, or loosely coupled.
Data Flow: Methods Calls
Objects interact by sending messages to each other through methods calls, which turn represent the data flow among objects.
Prototype Program
One way to improve communication among people and clarify the software specification is to write a prototype program. For Example: A simple - even inefficient - program could demonstrate the proposed user interface for analysis. It is better to discover any difficulties or to change your mind now than to do so after programming is underway or even complete.
Phase 2: Design
The best way to simplify the problem-solving process is to divide a large program into small, manageable parts. The resulting program will contain modules, which are self-contained units of code.
The Life Cycle of a Software
The development of good software involves a lengthy and continuing process known as the software life cycle. This arrangement suggests that the phases are part of a cycle and are not simply a linear list.
Application Programming Interface
The java application programming interface (API) is an example of one such collection of preexisting software. For example, you know how to use the static method *sqrt* contained in the Java API package /java.lang.Math/, yet you do not have to access its source statement, because it is precompiled. There is so much about /java.lang.Math.sqrt/ that you do not know, yet you can use it in your program without concern, as long /you know its specifications./
Contract
You must view these specifications as the terms of a contract between your method and the code that calls it. After the *move* method has been written and tested, the contract tells the rest of the program how to call the move method properly and lets it know the result of doing so. Specification as a contract
Methods Specifications Include Precise Preconditions And Postconditions
You write a contract when you write a method's *precondition*, which is a statement of the conditions that must exist at the beginning of a method, as well as when you write its *postcondition*, which is a statement of the conditions at the end method.
Postcondition
begin by describing the method's effect on its parameter-or in the case of a valued method, the value returns- and then describe any other action that has occurred.
Precondition
begin by describing the method's formal parameters, mention any class name constants that the method uses.
An Invariant
is a condition that is always true at a particular point in an algorithm.
A Loop Invariant
is a condition that is true before and after each execution of an algorithm's loop. Loop invariants can help you to write correct loops. By using invariants, you can detect errors before you begin coding and thereby reduce your debugging and testing time. Overall invariants can save you time.
An Assertion:
is a statement about a particular condition at a certain point in an algorithm. Preconditions and Postconditions are simply assertions about conditions at the beginning and end of methods.