Previous | Next | Table of Contents | Index | Program List | Copyright

2.6 Problem Solving: Software Development Illustrated

In this textbook, we will provide solutions to a number of case studies of programming problems. We obtain the solutions by following the software development method outlined in Section 1.4. Let's go through a case study, step by step.


Case Study: Converting Units of Measurement

PROBLEM SPECIFICATION

You work in a store that imports fabric. Most of the fabric you receive is measured in square meters; however, the store's customers want to know the equivalent amount in square yards. You need to write a program that performs this conversion.

ANALYSIS

The first step in understanding this problem is to determine what you are beingasked to do. It should be clear that you must convert from one system of measurement to another, but are you supposed to convert from square meters to square yards, or vice versa? The problem states that you receive fabric measured in square meters, so the problem input is fabric size in square meters. Your customers want to know the equivalent amount in square yards, which must be your problem output.

To solve this problem, with or without a computer, we need to know the relationship between square meters and square yards. By examining a metric table, we find that 1 square meter equals 1.196 square yards.

We summarize the data requirements and relevant formulas below. As shown below, we will use the name SquareMeters to identify the memory cell that will contain the problem input and the name SquareYards to identify the memory cell that will contain the program result, or the problem output.

Data Requirements and Formulas

Problem Inputs:

SquareMeters -- the fabric dimensions in square meters

Problem Outputs:

SquareYards -- the fabric dimensions in square yards

Formulas or Relations:

1 square meter equals 1.196 square yards

DESIGN

Next, we try to formulate the algorithm that we must follow to solve theproblem. We begin by listing the three major steps, or subproblems, of the algorithm.

Initial Algorithm

1. Read the fabric size in square meters.

2. Convert the fabric size to square yards.

3. Display the fabric size in square yards.

In using the term read we mean: "find out the value of this quantity from the user of the program"; because this quantity will change from run to run, we need to ask the user for its value each time. Generally this is done by instructing the computer to ask the user to enter the value on the computer keyboard; sometimes it is done by reading it from an external disk file (secondary storage). Similarly, in using the term display we usually mean "instruct the computer to show the value on the computer monitor."

Next, we decide whether any steps of the algorithm need further refinement or whether they are perfectly clear as stated. Step 1 (reading data) and step 3 (displaying a value) are basic steps and require no further refinement. Step 2 is fairly straightforward, but it might help to add some detail. The refinement of step 2 follows.

Step 2 Refinement

2.1 Multiply the fabric size in square meters by 1.196; the result is the fabric size in square yards.

The complete algorithm with refinements is shown below. The algorithm resembles an outline for a paper. The refinement of step 2, numbered as step 2.1, is indented under step 2. We list the complete algorithm with refinements below to show you how it all fits together.

Algorithm with Refinements

1. Read the fabric size in square meters.

2. Convert the fabric size to square yards.

2.1 Multiply the fabric size in square meters by 1.196; the result is the fabric size in square yards.

3. Display the fabric size in square yards.

TEST PLAN

We need to test three cases: a normal case of a positive floating-point value, a zero value, and a negative value. In the last case, the program will compute a negative number of square yards. Since this doesn't make physical sense, we will need a way of ensuring that it does not happen. Section 2.10 will offer some first solutions to this.

IMPLEMENTATION

To implement the solution, we must write the algorithm as an Ada program thatis acceptable to the compiler. Ada's syntax or grammatical rules require that we first list the problem data requirements--that is, what memory cell names we are using and what kind of data will be stored in each memory cell. Next, we convert each algorithm step into one or more Ada statements. If an algorithm step has been refined, we convert its refinements into Ada statements. You will be able to do this yourself as you learn more about Ada.

Program 2.6 shows the program along with a sample execution (the last two lines of the figure). We show the test run for a normal positive value; we leave it to you to run the program for the other test cases.

Program 2.6
Converting Square Meters to Square Yards

WITH Ada.Text_IO;
WITH Ada.Float_Text_IO;
PROCEDURE Metric_Conversion IS
------------------------------------------------------------------------------
--| Converts square meters to square yards
--| Author: Michael B. Feldman, The George Washington University 
--| Last Modified: July 1995                                     
------------------------------------------------------------------------
 
  MetersToYards : CONSTANT Float := 1.196; -- conversion constant
  SquareMeters  : Float;                   -- input - metric fabric size
  SquareYards   : Float;                   -- output - US fabric size
 
BEGIN -- Metric_Conversion

  -- Read the fabric size in square meters
  Ada.Text_IO.Put (Item => "Enter the fabric size in square meters > ");
  Ada.Float_Text_IO.Get (Item => SquareMeters);
 
  -- Convert the fabric size to square yards
  SquareYards := MetersToYards * SquareMeters;
 
  -- Display the fabric size in square yards 
  Ada.Text_IO.Put (Item => "The fabric size in square yards is ");
  Ada.Float_Text_IO.Put (Item => SquareYards); 
  Ada.Text_IO.New_Line;

END Metric_Conversion;
Sample Run
Enter the fabric size in square meters > 45.00
The fabric size in square yards is  5.38200E+01

The program consists, as before, of two parts: the declaration part and the program body. The declaration part is based on the data requirements identified in the problem analysis and tells the compiler what memory cells are needed in the program. Memory cells are needed for storing the variables SquareMeters and SquareYards and for storing the conversion constant MetersToYards (whose value is 1.196).

The program body begins, as always, with the line

    BEGIN
and contains the Ada statements that are translated into machine language and later executed. In the program body, we find the statements that express the algorithm steps as Ada statements. The statement
    Ada.Float_Text_IO.Get (Item => SquareMeters);
reads the data value typed by the program user (in this case, 4.56) into the memory cell named SquareMeters. The statement
    SquareYards := MetersToYards * SquareMeters;
computes the equivalent fabric size in square yards by multiplying the size in square meters by 1.196; the product is stored in memory cell SquareYards. Finally, the Put statements display a message string, the value of SquareYards, and a second message string. The instruction displays the value of SquareYards as a real number in Ada scientific notation (5.38200E+01). The value printed is equivalent to 5.382 x 101 or 53.82, as will be explained later.

TESTING

The sample run shows the result for a positive input. As discussed in the testplan section, you should run the program for zero and negative input values.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.