This lab serves multiple goals:
- To reinforce your understanding of boolean values,
- To practice conditional statements, and
- (Optional) To solve and implement solutions to simple, concrete problems.
The second part may seem
repetitive, but you need to practice if
statements a lot to
understand their mechanics and to be able to write them properly.
Basic Conditional Statements
Testing and Improving Conditional Statements
Consider the following code1:
- Without executing it, write down what you expect to be displayed
if the user enters
- 29339
- -15
- “That’s confidential”
- 10
- 18
- 19
- 22
- Download a solution containing this code as its
Main
method. - Execute it, providing the values written above. Were your expectations correct? If not, revise it and make sure you understand the logic of the program.
- There is at least one issue with this code as “You have reached the age of majority in all US states.” will never be displayed. Can you understand why?
- Fix the program so that all the messages can be displayed when relevant. Feel free to reorder statements or to use conjunction, disjunction, etc. to alter the conditions.
Writing Simple Conditional Statements
Read all the instructions in this part before starting to type code. Create a new project, and write portions of code that perform the following:
- Ask the user for an integer, and display on the screen “You were born after me” if the number is strictly greater than your year of birth.
- Ask the user for an integer, and display on the screen “Between and ” if the number is greater than or equal to and less than or equal to .
- Ask the user for an integer, and display on the screen “Not between and ” if the number is greater than or less than .
- Ask the user for an integer, and display on the screen “Odd” or “Even”, depending if the number is odd or even.
- Ask the user for an integer, and display on the screen “Negative” if the integer is negative, “Positive” if the integer is positive, and nothing if the integer is .
- Ask the user for an integer, and display on the screen “positive and odd” if the number is positive and odd, “positive and even” if the number is positive and even, “negative and odd” if the number is negative and odd, “negative and even” if the number is negative and even, and “You picked ” if the number is .
For each of those questions, write on paper whenever you should use
if
, if-else
, if-else-if
, and what the condition(s) should be. Once
you feel confident, write the code in your IDE, and then test it
intensively; enter all kinds of values (positive and odd, negative and
even, , and remember that is even, etc.) and make sure that what
is displayed on the screen is always correct.
Observation: How to Construct a Value Progressively
Please, read this part only once you have solved the last question of the previous exercise. You were asked the following:
Ask the user for an integer, and display on the screen “positive and odd” if the number is positive and odd, “positive and even” if the number is positive and even, “negative and odd” if the number is negative and odd, “negative and even” if the number is negative and even, and “You picked 0” if the number is 0.
A possible answer is:
That is a lot of repetition! And, as you know, it is not good practice to copy-and-paste the very same code, as it requires twice the editing every time you make an update!
An alternative approach is to “progressively” construct the message we will be displaying:
This is arguably much better, for the following reasons:
- There is less repetition (e.g., we do not have to repeat
answer >= 0
), - The conditions are simpler (e.g., no conjunction), and
- Since the two conditions (being odd / even, and being positive / negative) are actually independent, it seems more logical to test them separately.
Pushing Further (Optional)
This part asks you to read and understand a simple problem and to design, implement, and test a solution to it. You are asked to write a simple program that computes the total price for a group of people to enter a park.
Your program should:
- Ask the user how many adults and how many children want to enter the park,
- If the group comprises persons or more, offer to sell a group pass for $ (that allows everyone in the group to enter the park), and
- Compute and display the total price on the screen, knowing that:
- Adults pay $,
- Children pay $, and
- If purchasing the group pass allowed the group to save money (which isn’t always the case!), you should display on the screen the amount saved.
Some tips:
- When asking “yes” / “no” questions, treat “y” and “Y” as a “Yes”, and any other string as a “No”.
- Note that we will sell the pass even if the user is not saving any money by doing so (e.g., if 6 children want to enter, \4 \times 6 = $24 < $ 30$, but we would still sell them the pass).
Here is an example of execution, where the user input is u͟n͟d͟e͟r͟l͟i͟n͟e͟d͟, and hitting “enter” is represented by “⏎͟”:
Footnotes
-
The information about the age of majority comes from wikipedia. ↩