Lab 2: Modules: Step 1: Examine The Following Algorithm
Lab 2: Modules: Step 1: Examine The Following Algorithm
Lab 2: Modules: Step 1: Examine The Following Algorithm
Lab 2: Modules
This lab accompanies Chapter 3 of Starting Out with Programming Logic & Design.
Name: ___________________________
This lab requires you to think about the steps that take place in a program by writing
algorithms. Read the following program description prior to completing the lab.
Critical Review
A Module is a group of statements that exists within a program for the purpose of performing a
specific task.
Modules are commonly called procedures, subroutines, subprograms, methods, and functions.
The code for a module is known as a module definition. To execute the module, you write a
statement that calls it.
Module name( )
Statement
Statement
Etc.
End Module
Calling a module is normally done from the Main ( ) module such as:
Call name( )
Generally, local variables should be used and arguments should be passed by reference when the
value of the variable is changed in the module and needs to be retained. For example:
Module main( )
Real Integer number
Call inputData(number)
Call printData(number)
End Module
lab2-2.wmv
Starting Out with Programming Logic and Design 3
This lab requires you to think about the steps that take place in a program by writing
pseudocode. Read the following program prior to completing the lab.
External Design
Step 1: This program is most easily solved using just four variables. Declare the
variables that you will need in the program, using the proper data type and documenting
the purpose.
Step 2: Given the major task involved in this program, what modules might you consider
including? Describe the purpose of each module. (Reference: Defining and Calling a
Module, page 90).
Step 3: Complete the pseudocode by writing the missing lines. (Reference: Defining
and Calling a Module, page 90). Also, when writing your modules and making calls, be
sure to pass necessary variables as arguments and accept them as reference parameters if
they need to be modified in the module. (Reference: Passing Arguments to Modules,
page 104).
Module main ()
//Declare local variables
Declare Real totalSales
______________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
//Method calls
Call inputData(totalSales)
Call calcCounty(totalSales, countyTax)
______________________________________________________
______________________________________________________
______________________________________________________
End Module
______________________________________________________
End Module
Critical Review
The flowchart symbol used for a function call is a rectangle with vertical bars on each
side:
Main ( ) Method ( )
End
This lab requires you to think about the steps that take place in a program by designing a
flowchart. Use an application such as Flowgorithm. Read the following program
description prior to completing the lab.
Because Flowgorithm does not support reference variables, we will have to deviate from
the pseudocode design. Instead of the main module calling each module and passing the
required data, a “daisy chain” will be created with each module calling the next
sequential module and passing the required data.
Step 1: Start Flowgorithm and save your flowchart as Lab2_3. The .fprg file extension
will be added automatically. Start by adding a Comment box that specifies your name,
the date and a brief description of the program function. Then add the main method
comments from the pseudocode.
Starting Out with Programming Logic and Design 7
Step 2: Add a Declare rectangle for the totalSales variable after the correct comment and
then an Assign rectangle that initializes totalSales to 0.
Step 3: The next step in the flowchart should be to call the inputData method. Add a
Call symbol after the correct comment. Double click on the Call symbol and type the
name of the first method. For example, type inputData in the text box and specify
totalSales in between the ( ). Click the OK button. The flowchart should look like the
following:
Step 4: We now need to add the 4 other methods. Click the Add Function icon and
specify inputData as the function name and totalSales as the parameter as follows.
Starting Out with Programming Logic and Design 8
Click the OK button and the new function should appear as follows:
(To display other modules simply click the dropdown arrow to the right of the module
name to display a drop down list with all the module names then select one.)
Starting Out with Programming Logic and Design 9
Step 5: Continue this process and add the calcCounty() method. Make sure the
parameters for this method matches the pseudocode. For example, calcCounty should
look like the following:
Step 6: Each additional method needs to have any previously calculated data passed to
it. So define calcState() to accept totalSales, countyTax, and stateTax and define
calcTotal() and printData() to accept countyTax, stateTax, and totalTax.
Step 7: inputData needs to prompt the user for the total sales, assign the user specified
value to totalSales, declare and initialize the countyTax variable to 0 and call calcCounty
passing totalSales and countyTax. Create the flowchart to look like the following:
Starting Out with Programming Logic and Design 10
Step 9: Define calcState () to declare the totalTax variable and initialize it to 0, calculate
the state tax and assign it to stateTax. Then have it call calcTotal and pass the three
required variables.
Step 10: Define calcTotal() to calculate the total tax amount and assign it to totalTax.
Then have it call printData and pass the three tax variables.
Step 11: Define printData() to display the data and text as follows (if the total sales
amount was 12567):
Starting Out with Programming Logic and Design 11
This lab requires you to translate your work in the pseudocode and flowchart from Lab
2.2 and Lab 2.3 to actual code using Java. Read the following program description prior
to completing the lab.
If your program is correct, the input and output will look as follows:
Because java does not support reference variables as described in the text, you will have
to use global variables to store the information.
Step 1: Start Notepad++. Prior to entering code, save your file by clicking on File and
then Save. Select your location and save this file as Lab2_4.java. Be sure to include
the .java extension.
Step 2: Document the first few lines of your program to include the file name, your
name, the date, and a brief description of what the program does.
Step 3: Start your program with the following code for the various modules and that
creates global variables to hold the values:
Starting Out with Programming Logic and Design 13
import java.util.Scanner;
}
// This method calculates the county tax
public static void calcCounty(){
}
}
Step 4: In the inputData module, add the following statement to create a Scanner that
will read the input:
Step 5: In the inputData module, prompt the user to enter the total sales for the month by
adding this statement:
Step 6: In the inputData module, read the total sales for the month and assign it to the
variable totalSales by adding the following statement:
totalSales = keyboard.nextDouble();
Step 7: In the calcCount module, add the following statement to calculate the county tax
on the sales as totalSales * .0 2 and assign the result to countyTax:
Step 8: In the calcState module, add a statement to calculate the tip on the meal as
totalSales *.04 and assign the result to stateTax.
Step 9: In the calcTotal module, add a statement to calculate the totalTax as the sum of
countyTax and stateTax.
Step 10: In the printData module, add a println statement to display a blank line, then 3
println statements to display the following:
Step 11: In the main module, add five statements to call inputData(), calcCounty(),
calcState(), calcTotal(), and printData().
Write the Algorithm, Pseudocode, Flowchart, and Java code for the following
programming problem.
Meal Cost
Document the first few lines of your program to include your name, the date, and a brief
description of what the program does. The description of the program is:
External Design
The input prompts and output of the program should look like the following:
The Algorithm
Type algorithm here (and send this document) or create a separate document
for the algorithm and send it.
The Pseudocode
The Flowchart