Search
Thursday, August 28, 2008 ..:: Articles » FoxPro Summary » Worksheet 5 ::.. Register  Login
 FoxPro Summary: Worksheet 5 Minimize

FoxPro Summary

FoxPro Worksheet 5

D.Brettell 21 October 1999
Modified by Jutiphan (23-01-2002, 03-09-2005)

This worksheet introduces you to Modular Programming using the Top Down Design method. You will build a simple program that calculates the area and circumference of a circle. Modules will be written and tested before they are used, in a step by step process. The goal here is for you to understand the process of building a large program in units or modules. In this example we are not particularly interested in what the program does, but rather how it is put together.

We will follow the software lifecycle design process.

 

The Problem

We wish to write a program that finds the area and circumference of a circle.

At this point you would write a detailed specification of what the program will do and how the screens would look. As well as evaluate all other existing and potential solutions.

 

Program Structure

I have decided to divide the program into the following modules:

                                                                Main
                                                                    |
                    |                               |                                 |                              |                          
                  Input                      Area                       Circum                    Output

 

Main:      Displays the menu and calls the other modules
Input:     Gets the user input, the radius
Area:      Calculates the circle area
Circum:   Calculates the circle circumference
Output:  Displays the results

 

Main Module (main.prg) Design

It’s best to do the design in simple steps. Adding more detail to the design at each step.

Step 1

initialise the variables
clear the screen
display the menu
get the users menu choice
call appropriate module depending upon users choice
return to start of main

Step 2

initialise the variables
clear the screen
display the menu
get the users menu choice
if choice is input then
      call input module
if choice is area then
      call area module
if choice is circumference then
      call circum module
if choice is output then
      call output module
return to start of main

 

Step 3

initialise the variables
do while user doesn’t want to quit
clear the screen   
display the menu
get the users menu choice
if choice is input then
      call input module
if choice is area then
      call area module
if choice is circumference then
      call circum module
if choice is output then
      call output module
end of while loop

 

Implement the Main Program

Type in the following main program, save with the file name main.prg.

 

**************************************************************
* MAIN PROGRAM (main.prg)
* Modular Programming Example
* Finding the area and circumference of a circle
*
* Displays the main menu
*
* Calls:
*                              input.prg               -               Asks and gets radius from user
*                              area.prg                -                               Calculates the area
*                              circum.prg            -               Calculates the circumference          
*                              output.prg             -               Displays the results
*
* Created on by
**************************************************************

* initialise the variables
choice = 1                                                             && Users menu choice
radius=0.00                                                         && Radius of the circle
area = 0.00                                                           && Area to be calculated
circumference = 0.00                                          && Circumference to be calculated

do while choice <> 5                                         && do while user doesn't want to quit
                clear                                                       && clear the screen           
                @ 5,15 say "Modular Circle Program"                          && display the menu
                @ 8,10 say "Input radius"
                @ 8,40 say "1"
                @ 10,10 say "Area calculation"
                @ 10,40 say "2"
                @ 12,10 say "Circumference calculation"
                @ 12,40 say "3"
                @ 14,10 say "Display answers"
                @ 14,40 say "4"
                @ 16,10 say "Quit the program"
                @ 16,40 say "5"
                
                @ 20,10 say "Please enter choice"
                @ 20,40 get choice                                                             && get the users menu choice
                read

                do case
                                case choice = 1                                                    && if choice is input then
                                                do input.prg                                         && call input module
                                case choice = 2                                                    && if choice is area then
                                                do area.prg                                           && call area module
                                case choice = 3                                                    && if choice is circumference then
                                                do circum.prg                                       && call circum module
                                case choice = 4                                                    && if choice is output then
                                                do output.prg                                       && call output module
                                case choice = 5                                                    && if choice is quit the program
                                                clear                                                       && clear screen
                                                @ 20,10 say "Thank you for using the circle program."
                                otherwise                                                              && user presses a number that is not vaild
                                                @ 18,15 say "Please enter a choice between 1 and 5. Thank you."
                                                @ 25,10 say ""
                                                wait
                endcase
enddo                                                                                                     && end of while loop

 

Well, that’s all of the hard work done. The main program is the framework on to which we can hang the rest of the modules. Run the program and test it with input values greater than 5.

The case statement is used in the program instead of using lots of if statements.

 

Question 1: What happens when you chose one of the valid choices (1 to 5)?

You should get an error message that the program does not exist. We need to create some small stub programs so that our main program can be fully tested.

 

Stubs

These very simple and little programs will help us test our main program. Each stub belongs in it’s own file. Type in the following stub programs:

Save the following stub with the file name input.prg

 

***************
* input.prg stub
***************

clear
? "Input prg"
wait

 

Save the following stub with the file name area.prg

 

***************
* area..prg stub
***************

clear
? "Area  prg"
wait

 

Write new stub program files for the remaining two stubs. When you have written and saved the four stub programs test every choice of your main program.

 

Question 2: Why didn’t the program work? What error message did you get? Why?

Despite having created the stubs (input.prg, area.prg, circum.prg and output.prg) for each module FoxPro is having some trouble finding the files. We have to tell FoxPro where to look. We do this by using the following command:

set default to "P:\FoxPro\Vfp5"

You must add this line to your main.prg, just before the loop is a good place to put it. In between the quotes “” you must type the location of where your files are kept (E.g. p:\foxpro). Run the main program again and test each choice to check that the stubs are called and run.

 

Input Module (input.prg)

Now that the main program works we will create each module.

Step 1

clear the screen
ask user for the radius
read in the radius

One design step is enough for this module. Now change your input.prg stub to look the same as the module below.

 

******************************
* Input Module
* Gets the radius from the user
*
* Called from:
*                              main.prg
*
*  Created on by
******************************

clear
@ 5,15 say "Input Screen"                                                                                && clear the screen
@ 10,10 say "Please enter the circle radius:"                              && ask user for the radius
@ 10,50 get radius                                                                             && read in the radius
read

* FoxPro now returns back to the main program

 

Area Module (area.prg)

This module calculates the are of the circle given the radius. Read the design and then change your area stub to the code given below.

Step 1

clear the screen
calculate the circle area
display a message saying area has been calculated
wait for user to press a key

 

*************************************
* Area Module (area.prg)
* Calculates the area of the circle
*
* Called from:
*                              main.prg
*
*  Created on by
*************************************

clear
area=pi()* radius^2                                                                          && calculate the circle area
@ 10,10 say "The circle area has been calculated"   
@ 15,10 say ""
wait                                                                                                        && wait for user to press a
key

* FoxPro now returns back to the main program

 

Test the stub from your main program but also test the other parts of the program to ensure everything is still working.

Task :  You are to write the circumference (circum.prg) and display (output.prg) modules. Follow the exact same format as given above. You must write the design before you type the FoxPro code in.  Each of your modules should contain comments. Call me when your program is finished.

 

Summary

Well done, you have built a modular program and are probably thinking to yourself  “Well, sure I have, but it took about ten times more work than the simple little circle program that I wrote before.” This is true, for this small programming example. On the other hand, you have to admit that the final program is neater, logical and essentially easier to understand. The benefits of modular program, as outlined in class, are legendary and when it comes to writing larger programs, such as your project, it is the only way your program is going to work.

You are now going to use the same principles for your project.

 


 Print   

 Usefulness Survey Minimize
Content on this page is:



Submit Survey  View Results

 Print   

Copyright 2005-2008 by Jutiphan Mongkolsuthree   Terms Of Use  Privacy Statement