Uploaded: 7 years ago
Contributor: Guest
Category: Computer Science
Type: Solutions
Tags: weight, module, program, determine, monthlysales, module
//this, function, monthly, variables, birthmonth, following, student, employees, integer, python
Rating:
(1)
|
Filename: Lab 3 Solutions.doc
(110 kB)
Page Count: 7
Credit Cost: 1
Views: 956
Downloads: 1
Last Download: 4 years ago
|
Description
Starting Out with Programming Logic and Design, 3rd Edition
Transcript
Starting Out with Programming Logic and Design 12
Solutions
Lab 3: Decisions and Boolean Logic
Note to Instructor: This lab accompanies Chapter 4 of Starting Out with Programming Logic & Design, with a focus on if statements and relational operators. Material in the chapter should have been covered prior to lab assignment.
Evaluation: The instructor should be present to answer any questions and observe the student performing the lab. The student should turn in a hard copy (paper) or soft copy (email) of their work.
Learning Objectives for this lab include:
Understand how relational operators function.
Understand how Boolean expressions process.
Learn how to write an if statement in pseudocode, flowcharts, and Python code.
Lab 3.1 walks the student through the process of evaluating Boolean expressions.
Labs 3.2, 3.3, and 3.4 use the following programming problem.
A retail company assigns a $5000 store bonus if monthly sales are $100,000 or more. Additionally, if their sales exceed 125% or more of their monthly goal of $90,000, then all employees will receive a message stating that they will get a day off.
Lab 3.5 uses the following programming problem.
Write a program that will ask the user to enter a person’s age, their weight, and their birth month. Your program will compare the entered values to the following and print the appropriate responses. Be sure to use modules.
The Secret Answers The Comparison The Printed Response
age = 25 If the guessed age is less than or equal to 25 Congratulations, the age is 25 or less
weight = 128 If the guessed weight is greater than or equal to 128 Congratulations, the weight is 128 or more
birthMonth = ‘April’ If the guessed birth month is equal to April Congratulations, the birth month is April
Lab 3.1 – Evaluating Conditions
Step 1: Consider the following values set to variables.
myAge = 32
yourAge = 18
myNumber = 81
yourNumber = 17
votingAge = 18
myName = “Katie”
yourName = “Bob”
Step 2: Based on the values to the variables in Step 1, do the following conditions result in a true or false statement?
The condition True or False
myAge >= yourAge True
yourAge > myAge False
myAge == 45 False
yourAge == votingAge True
votingAge >= yourAge True
myAge <= votingAge False
myName != yourName True
myNumber <= myAge True
yourNumber >= myAge False
yourNumber != 17 False
Step 3: Based on the values to the variables in Step 1, what is the expected output? Hint: The output will either be what is printed to the screen, or "nothing."
The condition Expected Output
If myName == yourName Then print “We have the same name”
End If Nothing
If myAge >= yourAge Then print “I am older or equal to your age”
End If I am older or equal to your age
If myName != “Katie” Then print “That is not my name”
End If Nothing
If myName == “Katie” Then print “That is my name”
End If That is my name
If myNumber == 17 Then print “My number is 17”
End If Nothing
If myNumber >=80 Then print “My number is 80 or more”
End If My number is 80 or more
If yourNumber <= yourAge Then print “Your number is less than or equal to your age”
End If Your number is less than or equal to your age
If myNumber < yourNumber Then print “My number is less”
End If Nothing
If yourAge >= votingAge Then print “You can vote”
End If You can vote
If myAge < yourAge Then print “I am younger”
End If Nothing
Lab 3.2 – Pseudocode and Decisions
Step 1: This program is easiest when solved using just one variable. Declare the variables that you will need in the program, using the proper data type and documenting the purpose. Depending on your programming style, you may find that additional variables are useful. If that is the case, adjust your program as necessary.
Variable Name Purpose
Real monthlySales Stores the monthly sales
Step 2: Given the major task involved in this program, what modules might you consider including? Also describe the purpose of the module.
Module Name Purpose
Module getSales () Allows the user to enter the monthly sales.
Module isBonus () This module will determine if a bonus should be awarded.
Module isDayoff () This module will determine if a day off should be awarded.
**Note: Their module names may be different, but should indicate the main task.
Step 3: Complete the pseudocode by writing the missing lines. 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: Writing a Decision Structure in Pseudocode, page 118).
Module main ()
//Declare local variables
Declare Real monthlySales
//Function calls
Call getSales(monthlySales)
Call isBonus(monthlySales)
Call isDayoff(monthlySales)
End Module
//this module takes in the required user input
Module getSales(Real Ref monthlySales)
Display “Enter the total sales for the month.”
Input monthlySales
End Module
//this module will determine if a bonus is awarded
Module isBonus(Real monthlySales)
If monthlySales >=100000 Then
Print “You get a bonus of $5,000!!!”
End If
End Module
//this module will determine if all employees get a day
//off. If sales are greater than or equal to 112500, then
//they get a day off.
Module isDayoff(Real monthlySales)
If monthlySales >= 112500 Then
Print “All employees get a day off!!!”
End If
End Module
Lab 3.3 – Flowcharts
Note to Instructor:
Steps 1 – 8 walk students through the process of using Raptor and designing a flowchart. Step 9 requires students to show their completed flowchart. Variable names and statements will vary from student to student. Also, the lab can be completed using a different flowcharting application such as Visio. If so, the sample solution below will look a little different, but should still have the same fundamental steps.
Instructors should grade on clear variable names, proper flow of program, the correct calculation, and the right output. Below is a sample solution to the programming problem.
Step 9:
Main Module
getSales Module
isBonus Module
isDayOff Module
Lab 3.4 – Python Code
Note to Instructor:
Steps 1 – 8 in this lab require students to write Python code to create a program. Their final program is required in Step 9.
Step 9:
#Student Name
#Date
#Description: This program determines if a bonus should be awarded
#The main function
def main():
print 'Welcome to the program'
monthlySales = getSales() # gets sales
#Function call to determine bonus
isBonus(monthlySales)
#Function call to determine day off
isDayoff(monthlySales)
#This function gets the monthly sales
def getSales():
monthlySales = input('Enter the monthly sales $')
monthlySales = float(monthlySales)
return monthlySales
#This function determines if there is a bonus
def isBonus(monthlySales):
if monthlySales >= 100000:
print "You have earned a $5,000 bonus!!!"
#This function determines if employees get a day off
def isDayoff(monthlySales):
if monthlySales >= 112500:
print "All employees get a day off!!!"
#calls main
main()
Lab 3.5 – Programming Challenge 1 – Guess the Secrets
The Pseudocode
Module main ()
//Declare local variables
Declare Integer age
Declare Integer weight
Declare String birthMonth
//Function calls
Call getAge(age)
Call getWeight(weight)
Call getMonth(birthMonth)
Call correctAnswers(age, weight, birthMonth)
End Module
//this module takes in the age
Module getAge (Integer Ref age)
Display “Enter your guess for age: ”
Input age
End Module
//this module takes in the weight
Module getWeight (Integer Ref weight)
Display “Enter your guess for weight: ”
Input weight
End Module
//this module takes in the birth month
Module getAge (String Ref birthMonth)
Display “Enter your guess for birth month: ”
Input birthMonth
End Module
//this module will determine if guesses are correct
Module correctAnswers(Integer age, Integer weight, String birthMonth)
If age <= 25 Then
Print “Congratulations, the age is 25 or less.”
End If
If weight >= 128 Then
Print “Congratulations, the weight is 128 or more.”
End If
If birthMonth == ‘April’ Then
Print “Congratulations, the birth month is April.”
End If
End Module
The Flowchart
Main Module
getAge Module
getWeight Module
getMonth Module
correctAnswers Module
The Python Code
#Secret Answers
#the main function
def main():
print #prints a blank line
age = getAge()
weight = getWeight()
birthMonth = getMonth()
print
correctAnswers(age, weight, birthMonth)
#this function will input the age
def getAge():
age = input('Enter your guess for age: ')
return age
#this function will input the weight
def getWeight():
weight = input('Enter your guess for weight: ')
return weight
#this function will input the age
def getMonth():
birthMonth = raw_input('Enter your guess for birth month: ')
return birthMonth
#this function will determine if the values entered are correct
def correctAnswers(age, weight, birthMonth):
if age <= 25:
print 'Congratulations, the age is 25 or less.'
if weight >= 128:
print 'Congratulations, the weight is 128 or more.'
if birthMonth == 'April':
print 'Congratulations, the birth month is April.'
#calls main
main()
|
|