You are on page 1of 7

Chapter 5: Program Design and

Analysis
The Waterfall Method

Analysis of the Specification


The specification is a written description of the project. It is based on the
customer's requirements.

Program Design
A fairly detailed plan for solving the problem as described in the specification.
The program design takes into account all of the objects, classes, and the
hierarchy that will be used in the solution. It will also include the data
structures that will implement those objects.

It does not include the minutiae (small, trivial details) of Java code.

Program Implementation

Testing and Debugging


Test Data:
The programmer should choose a representative set of test data in order to test
the program or debug the program. This test data should include:

Typical Values that are part of the input domain.

Chapter 5 Program Design and Analysis 1


Endpoint Values that are the ends of the input domain.

Out - Of - Range Values that are not part of the input domain.

Types of Errors (Bugs):


Problems in the code

 Compile - Time Error: This type of error occurs while the program is
compiling. This means that the compiler is unable to translate the program
into bytecode.

Syntax Error: Syntax errors are compile - time errors, occur due to
breaking programming rules.

 Run - Time Error: An error that occurs during the execution of the program,
when the Java run - time environment throws an exception, meaning it stops
execution and prints an error message.

Examples: Dividing an integer by zero, using an array index that is out of


bounds, attempting to open a file that cannot be found, and an infinite
loop.

 Intent or Logic Error: Error in which the program fails to carry out the
specification of the program. The program compiles and runs but does not do
the job.

Robustness
Robust: A program that will work even if the user of the program does not
follow the directions of the program. A robust program is able to deal with
input data that is not in the input domain.

It won't give inaccurate data for some input data.

It won't crash if the input data are invalid.

It won't allow execution to proceed if invalid data are entered.

Chapter 5 Program Design and Analysis 2


The program should include code that catches the error, allows the error to
be fixed, and allows program execution to resume.

Program Maintainence
This involves upgrading the code as circumstances change, making the
program easy to follow in the code for new coders and programmers, good
documentation, and allowing for new features to be added easily.

Object - Oriented Programming Design


Steps in object - oriented design:

Identify classes to be written

Identify behaviors (i.e. methods) for each class

Determine the relationships between classes

Write the interface (public method headers) for each class

Implement the methods

Identifying Classes
When identifying classes, the programmer must decide names that accurately
describe parts of the code. These parts of the code include basic objects,
collections, controllers, and display(s).

Example: Write a program that maintains an inventory of stock items for a


small store.

Basic Object: StockItem

Collection: Inventory A list of StockItems)

Controllers: Store (has an Inventory, uses a StoreDisplay)

Display: StoreDisplay (could be a GUI, but does not have to be)

Chapter 5 Program Design and Analysis 3


Identifying Behaviors
Encapsulation: The process of bundling a group of methods and data fields
into a class.

Remember, a basic object should not be required to manage actions for the
whole group, only itself.

Also, consider which data fields each class will need and which data
structures should store them.

Determining Relationships Between Classes


Inheritance Relationships: These are relationships between classes in which
one class is another class.

Composition Relationships: These are relationships that are defined by the


has - a relationships. If two classes have a composition relationship, one of
them typically contains an instance variable whose type is the other class.

A wrapper class always has a has - a relationship with any objects that it
wraps.

Relationships Between Classes Using UML Diagrams


This is a good way to keep track of the relationships between classes.

UML Unified Modeling Language) diagrams show the inheritance hierarchy


in a program.

Classes are rectangles

Angle brackets with the word "abstract" or "interface" indicate either an


abstract class or interference.

An open up arrow show the is - a relationship.

An open up dotted arrow shows the is - a relationship with an interface.

Chapter 5 Program Design and Analysis 4


The has - a relationship is shown with a down arrow or sideways arrow
(indicates composition).

Implementing Classes
Bottom - Up Development:

In bottom - up development, the developer first starts by, for each method,
listing all of the other classes needed to implement that particular method.
These classes are called collaborators. A class that has no collaborators is
independent.

Then, independent classes are fully implemented and tested before being
incorporated into the overall project. This means that unrelated classes in a
programming project can be implemented by programmers.

There is also a driver class that contains a main method that can be used to
test the program as it is being developed.

Top - Down Development:

The programmer starts with an overview of the program, selecting the


highest - level controlling object and the tasks needed, creating subsidiary
classes as existing classes get too complicated.

Implementing Methods
Procedural Abstraction:

A good programmer attempts to avoid chucks of repeated code. In order to


avoid this, helper methods can be used.

Procedural abstraction is the use of helper methods within a class, and


breaks long methods into smaller tasks. It is an example of top - down
development within a class.

Stepwise Refinement: The process of breaking a long method into as


sequence of smaller tasks.

Information Hiding:

Chapter 5 Program Design and Analysis 5


Information Hiding: The strategy in which instance variables and helper
methods are generally declared as private to prevent client classes from
accessing them.

Stub Method:

Stub: A dummy method that stands in for a method until the actual method
has been written and tested. It usually has an output statement to show that it
was called in the correct place, or it may return some reasonable values if
necessary.

Algorithm:

Algorithm: A precise step - by - step procedure that solves a problem or


achieves a goal. Don't write code for an algorithm until the steps are
completely clear to you (you being the developer in this instance).

Program Analysis

Program Correctness
Although it is not possible to test every single possible input (in many cases),
scientists have developed mathematical techniques to prove correctness in
certain cases.

Assertions
Assertion: A precise statement about a program at any given point. If an
assertion is proved to be true, then the program is working correctly at that
point in time. Assertions are useful for knowing if your code works.

Precondition: A statement of what is true immediately before execution of a


piece of code. Helps debug.

Postcondition: A statement of what is true immediately after execution of a


piece of code. Helps debug.

Chapter 5 Program Design and Analysis 6


Efficiency
An efficient algorithm is one that conserves these two characteristics:

CPU Time: Number of machine operations required to carry out the


algorithm.

Memory: The number and complexity of the variables used.

Run time efficiency is affected by these factors:

Unnecessary tests, excessive movement of data elements, and redundant


computations, especially in loops.

Try to aim for early detection of output conditions. For example, in a sorting
algorithm, once the element or value that the program has been looking for is
found, the sorting algorithm should be stopped, and halted.

Best Case: The configuration of the data that causes the algorithm to run in
the least possible time amount.

Worst Case: A configuration of the data that causes the algorithm to run in
the most possible time amount.

Average Case: Typical configurations, or the time amount for the algorithm
to run under normal conditions.

Chapter 5 Program Design and Analysis 7

You might also like