CS 139 Algorithm Development
Lab10B: Program Design

Objectives

Students will be able to:

Key Terms

Submission

  1. 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:
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
Structure Diagram 1
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
  1. import java.util.Scanner;
  2. public class CostPerSqFt {
  3.   public static void main(String[] args) {
  4.     final double SENTINEL = 0.0;
  5.     double houseSize;
  6.     int houseCost;
  7.     double costPerSqFt;
  8.     double totalCostPerSqFt;
  9.     double averageCostPerSqFt;
  10.     int houseCount;
  11.     Scanner keyboard = new Scanner(System.in);
  12.  
  13.     totalCostPerSqFt = 0.0;
  14.     houseCount = 0;
  15.    
  16.     System.out.println("Enter the square feet and price for each house.");
  17.     System.out.println("Enter 0 for house size to quit.");
  18.    
  19.     do {
  20.       System.out.print("Size in square feet: ");
  21.       houseSize = keyboard.nextDouble();
  22.       System.out.print("Cost in whole dollars: ");
  23.       houseCost = keyboard.nextInt();
  24.      
  25.       if (houseSize > 0.0) {
  26.         costPerSqFt = houseCost / houseSize;
  27.         totalCostPerSqFt = totalCostPerSqFt = costPerSqFt;
  28.         houseCount = houseCount + 1;
  29.       }
  30.     } while (houseSize != SENTINEL);
  31.    
  32.     if (houseCount > 0) {
  33.       averageCostPerSqFt = totalCostPerSqFt / houseCount;
  34.     }
  35.     else {
  36.       averageCostPerSqFt = 0.0;
  37.     }
  38.     System.out.println("Number of Houses: " + houseCount);
  39.     System.out.println("Average cost per square foot: " + averageCostPerSqFt);
  40.    
  41.   }
  42.   
  43. }
Structure Diagram 2




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.

Structure Diagram 3

 


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.