CS101-CS102
Java Lab - Design &
Implementation
Details & examples of what we expect in Java (structured programming) lab assignments...
Structured Programming Designs
- Top-down design: ensure that a main algorithm of at most seven steps is
written & checked before any attempt to solve these steps is made. Given
independent implementations of the pieces, would the design be guaranteed to work
correctly?
- Fully specify steps: Each step in an algorithm should be complete, such
that it could be given to someone (who knows nothing of the original task) and they
could correctly implement it without any additional information. This
requires information that will be input to and output from the step, to be
clearly specified.
- Data/Memory requirements: Information flowing from one step in the
algorithm to another should be identified. In the algorithm, the word/phrases
involved should be underlined. These are the memory locations needed by your
program. Their types and whether they are variables or
constants (and if constants, their values) should be clearly specified.
- Rewriting/expanding steps is simply a rewording of the step and must not
do something else or include anything not mentioned in the original step!
- Beginners should develop algorithms until one step corresponds to
one Java statement.
Basic Algorithm Steps
Ultimately, you should aim to have
an algorithm comprising only sequence/decision/repetitions of
input/output/assignment steps. These correspond to the following common
English sentence forms.
- sequence (
"do this and then do this and then do this..." )
- decision (
"if condition then statement else statement")
- repetition
( "while condition do this" or "do
this while condition" or "for each/every/all/no-of-times do this"
)
- input ( "ask for and get
x" or "read/input/get x" )
- output(
"print/report/output x" )
- assignment ( "x is 0" or
"x is square root of y" ... )
Important: note that
there is no statement/sentence of the form "go to step x". Such forms are a
remnant of the older go-to (spaghetti code) paradigm and must be rewritten using
one of the structured programming paradigm forms below.
The correct form of the various
control algorithm forms is:
(notice how they correspond one-to-one with the Java statements!)
| sequence: |
1. do this
2 do this
3. do this
|
| decision: |
1. do this
2 if condition then
2T.1 do this
2T.2 and this
else
2F.1 do that
2F.2 and that
3. do this
|
| while: |
1. do this
2 while condition do
2.1 something
2.2 and something more
2.3 and yet more
3. do this
|
| do-while: |
1. do this
2 do
2.1 something
2.2 and something more
2.3 and yet more
while condition
3. do this
|
| for: |
1. do this
2 for each value of i from 0 to number of repetitions do
2.1 something
2.2 and something more
2.3 and yet more
3. do this
|
Algorithm development examples:
Also, be sure you are not making any of these
Common Algorithm Development Mistakes
Methods
Any step in your algorithm may be implemented as a method.
You should already be identifying the data/memory/variable locations used in
each step as being I(nput), O(utput) or L(ocal) to the step, so the only
additional consideration is what name you want to give it! Write the algorithm
for the method separate from the other parts of your design and then simply
"call" -use/refer- to it as necessary (in exactly the same way you already
call/use/refer to Java methods such as those for input, output and square roots!)
Implementation
- All programs should have a header comment, including a description,
author name, date, etc.
- Comments should be used to aid understanding major sections of the
code & to clarify non-obvious algorithms.
- Blank lines should separate all logical sections, ie. major steps,
method definitions, etc.
- Correct indention must be maintained everywhere.
- All variables, constants, methods, and classes must be meaningfully named
& follow Java naming conventions.
- Variables and named constants should be declared
before the program code begins, not as needed in the program!
- Each variable or constant should be declared on a separate line.
- Named constants should be defined before variables so they are easy to
find and change.
- Variables should not be initialised when they are
declared, but only later given a value as necessitated by the algorithm.
When implementing your program in Java you MUST follow our
Java Style Guidelines!
Last updated:
02 Oct 2010
Please report any errors or omissions to the course instructor.