Lab 2: Modules: Step 1: Examine The Following Algorithm

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 16

Starting Out with Programming Logic and Design 1

Lab 2: Modules
This lab accompanies Chapter 3 of Starting Out with Programming Logic & Design.

Name: ___________________________

Lab 2.1 – Algorithms

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.

A retail company must file a monthly sales tax report


listing the total sales for the month and the amount of
state and county sales tax collected. The state sales tax
rate is 4 percent and the county sales tax rate is 2
percent. Write a program that asks the user to enter the
total sales for the month. The application should calculate
and display the following:
 The amount of county sales tax
 The amount of state sales tax
 The total sales tax (county plus state)

Step 1: Examine the following algorithm.

1. Get the total sales for the month.


2. Multiply the total sales by .04 to calculate the state sales
tax.
3. Multiply the total sales by .02 to calculate the county
sales tax.
4. Add the state tax and county tax to calculate the total
sales tax.
5. Display the calculated county tax, state tax, and total
sales tax.

Step 2: Given a total sales of $27,097, answer the following:


What is the calculated state tax? _______________________________________
What is the calculated county tax? _____________________________________
What is the calculated total tax? _______________________________________
Starting Out with Programming Logic and Design 2

Lab 2.2 – Pseudocode and Modules

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.

The format for a module definition is as follows:

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

//accepts number as a reference so the changed value


//will be retained
Module inputData(Real Ref number)
number = 20
End Module

//number does not to be sent as a reference because


//number is not going to be modified
Module printData(number)
Display “The number is “, number
End Module

Help Video: Double click the file to view video

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.

A retail company must file a monthly sales tax report


listing the total sales for the month and the amount of
state and county sales tax collected. The state sales tax
rate is 4 percent and the county sales tax rate is 2
percent. Write a program that asks the user to enter the
total sales for the month. The application should calculate
and display the following:
 The amount of county sales tax
 The amount of state sales tax
 The total sales tax (county plus state)

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.

Variable Name Purpose


Declare Real totalSales Stores total sales the user
inputs

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).

Module Name Purpose


Module inputData () Allows the user to enter required
user input
Starting Out with Programming Logic and Design 4

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

//This module takes in the required user input


Module inputData(Real Ref totalSales)
Display ______________________________________________
Input totalSales
End Module

//This module calculates county tax


//totalSales can be a value parameter because it is not
//changed in the module.
//countyTax must be a reference parameter because it is
//changed in the module
Module calcCounty(Real totalSales, Real Ref countyTax)
countyTax = totalSales * .02
End Module

//This module calculates state tax


Module ____________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
End Module

//This module calculates total tax


Module ____________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
Starting Out with Programming Logic and Design 5

______________________________________________________
End Module

//This module prints the total, county, and state tax


Module ____________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
______________________________________________________
End Module
Starting Out with Programming Logic and Design 6

Lab 2.3 – Flowcharts

Critical Review

The flowchart symbol used for a function call is a rectangle with vertical bars on each
side:

Main ( ) Method ( )

//Used in main to //do something


represent a
function call
Method( )
Return

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.

A retail company must file a monthly sales tax report


listing the total sales for the month and the amount of
state and county sales tax collected. The state sales tax
rate is 4 percent and the county sales tax rate is 2
percent. Write a program that asks the user to enter the
total sales for the month. The application should calculate
and display the following:
 The amount of county sales tax
 The amount of state sales tax
 The total sales tax (county plus state)

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 8: Define calcCounty() to declare the stateTax variable and initialize it to 0,


calculate the county tax and assign it to countyTax. Then have it call calcState and pass
the three required variables.

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

Step 12: The final step is to send the finished flowchart.

Send the .fprg file to [email protected]


Starting Out with Programming Logic and Design 12

Lab 2.4 – Programming Challenge 1 – Retail Tax

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.

A retail company must file a monthly sales tax report


listing the total sales for the month and the amount of
state and county sales tax collected. The state sales tax
rate is 4 percent and the county sales tax rate is 2
percent. Write a program that asks the user to enter the
total sales for the month. The application should calculate
and display the following:
 The amount of county sales tax
 The amount of state sales tax
 The total sales tax (county plus state)

Your program will consist of the following modules:


 main calls the other module
 inputData will ask for the monthly sales
 calcCounty will calculate the county tax
 calcState will calculate the state tax
 calcTotal will calculate the total tax
 printData will display the county tax, the state tax, and the total tax

If your program is correct, the input and output will look as follows:

Please enter total sales for the month 12567

The county tax is $251.34


The state tax is $502.68
The total tax is $754.02

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;

//Lab 2_4 The Retail Tax Program

public class Lab2_4{


//Declare global variables
static double totalSales, countyTax, stateTax,
totalTax;

// This module takes in the required user input


public static void inputData(){

}
// This method calculates the county tax
public static void calcCounty(){

// This method calculates the state tax


public static void calcState(){

// This method calculates the total tax


public static void calcTotal(){

// This module prints the total, county, and state tax


public static void printData(){

public static void main(String[] args){


//Method calls

}
}

Step 4: In the inputData module, add the following statement to create a Scanner that
will read the input:

Scanner keyboard = new Scanner(System.in);

Step 5: In the inputData module, prompt the user to enter the total sales for the month by
adding this statement:

System.out.print("Please enter total sales for the month ");


Starting Out with Programming Logic and Design 14

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:

countyTax = totalSales * .02;

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:

The county tax is $251.34


The state tax is $502.68
The total tax is $754.02

Step 11: In the main module, add five statements to call inputData(), calcCounty(),
calcState(), calcTotal(), and printData().

The Java Code

Send java code to [email protected]


Starting Out with Programming Logic and Design 15

Lab 2.5 – Graded Assg - Meal Costs

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:

Write a program that will calculate a 20% tip and a 6% tax


on a meal price. The user will enter the meal price and the
program will calculate tip, tax, and the total. The total
is the meal price plus the tip plus the tax.

Save source code as Lab2_5.java.

In addition to main, there should be 5 modules called inputMeal( ), calcTip( ), calcTax( ),


calcTotal(), and printInfo( ). For the pseudocode use and pass local variables and
reference variables. For flowgorithm, create a daisy chain between the modules passing
local variables. For the java portion of this assignment, use global variables.

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

Type pseudocode here (and send this document) or create a separate


document for the pseudocode and send it.
Starting Out with Programming Logic and Design 16

The Flowchart

Send the .fprg file

The Java Code

Send the .java file

You might also like