CS 139
Algorithm Development
Lab10B: Program Design
Objectives
Students will be able to:
- interpret a structure chart
- write a program skeleton based upon a structure chart
- create a program structure chart
Key Terms
- modularity
- cohesion
- coupling
- structure diagram
Submission
- Write the answers in the worksheet that is provided
(Lab10B.rtf) and submit the worksheet via Blackboard.
Background
Modern computer software is very complex. A typical application that
you use might consist of tens of millions of lines of computer code.
Applications of this complexity cannot be successfully written
without first creating a detailed design for the software that
identifies modules and their interactions.
At the individual program level, we also employ design techniques to
help us decompose a program into efficient subprogram modules (which
we call methods in the case of Java programs). The goals of
design are:
- modularity: isolate different functions (such as
input, calculation, output) from one another by putting them in
separate methods
- high cohesion: ensure that each method has a
single purpose that it can be focused on
- low coupling: minimize the amount of data that
must be transferred between methods
The structure diagram is a simple design tool that helps us to
visualize program design. It shows the modules (methods) that make
up a program, and the arguments and return values by which data is
transferred between them. Take a look at the following program and
structure diagram. Each method of the program appears as a module
(box) in the diagram. Each argument appears as a data-flow from the
main module, and each return value appears as a data flow to the
main module.
Structure Diagram
|
Program
|

|
import java.util.*;
public class Interest {
public static void main(String[] args) {
double balance;
double rate;
double monthlyInterest;
balance = getInput("Enter the
loan balance: ");
rate = getInput("Enter the
annual interest rate % (0-100): ");
monthlyInterest =
calculateInterest(balance, rate);
showResult(monthlyInterest);
}
public static double getInput(String
prompt) {
double valueIn;
Scanner keyboard = new
Scanner(System.in);
System.out.println(prompt);
valueIn =
keyboard.nextDouble();
return valueIn;
}
public static double
calculateInterest(double balance, double rate) {
double interest;
interest = (rate / 100.0 /
12.0) * balance;
return interest;
}
public static void showResult(double
interest) {
System.out.println("Monthly
interest: " + interest);
}
}
|
Part 1: Refactoring
Examine the following program. It is a monolithic program, with
no structure.
The structure diagram alongside the program shows an improved
design, with several modules.
1. For each line of code in the program, decide which module that
line of code should belong to in the improved program, and then
label the line with the module letter (A, B, C, or D).
Original Code
|
Improved Design
|
- import java.util.Scanner;
- public class CostPerSqFt {
- public static void main(String[] args) {
- final double SENTINEL = 0.0;
- double houseSize;
- int houseCost;
- double costPerSqFt;
- double totalCostPerSqFt;
- double averageCostPerSqFt;
- int houseCount;
- Scanner keyboard = new
Scanner(System.in);
-
- totalCostPerSqFt = 0.0;
- houseCount = 0;
-
- System.out.println("Enter the
square feet and price for each house.");
- System.out.println("Enter 0 for
house size to quit.");
-
- do {
-
System.out.print("Size in square feet: ");
- houseSize =
keyboard.nextDouble();
-
System.out.print("Cost in whole dollars: ");
- houseCost =
keyboard.nextInt();
-
- if (houseSize >
0.0) {
-
costPerSqFt = houseCost / houseSize;
-
totalCostPerSqFt = totalCostPerSqFt = costPerSqFt;
-
houseCount = houseCount + 1;
- }
- } while (houseSize !=
SENTINEL);
-
- if (houseCount > 0) {
- averageCostPerSqFt
= totalCostPerSqFt / houseCount;
- }
- else {
- averageCostPerSqFt
= 0.0;
- }
- System.out.println("Number of
Houses: " + houseCount);
- System.out.println("Average
cost per square foot: " + averageCostPerSqFt);
-
- }
-
- }
|

|
Part 2: Creating a Program Skeleton from
a Design
1. Given this structure diagram, write all of the method headers
(using the argument and parameter names shown in the diagram).
Assume that names ending in _s are string values, otherwise they are
int's.
2. Begin the coding for the main method by adding one instance of a
call to each method.

Part 3:
Create Your Own Design
1. Develop a structure diagram for a program to solve the following
problem. Then write the program skeleton (method headings) as in
Part 2, and the complete main method.
The Password Generator program will generate a unique password using
random characters. The user will input a password length and a
string of characters from which the password can be composed. The
program will select the required number of characters at random to
form a password, and then display the password.