This lab serves multiple goals:
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.
Consider the following code1:
int yourAge; // This variable will contain the user's age.
Console.WriteLine("Please, enter your age"); // We ask the user.
// We read from the user and convert their answer into an int.
yourAge = int.Parse(Console.ReadLine());
// The rest of the code tests the value of yourAge and
// displays a message based on its value. You are asked
// to explain this code next.
if (yourAge < 0)
{
Console.WriteLine(
"I believe you made a mistake, an age cannot be negative!");
}
else if (yourAge > 2000)
{
Console.WriteLine(
"I believe you made a mistake, nobody can live that long!");
}
else if (yourAge >= 18)
{
Console.WriteLine(
"In all States but Alabama, Nebraska, Mississippi"
+ " and Puerto Rico, you have reached the age of majority.");
}
else if (yourAge >= 19)
{
Console.WriteLine(
"In all States but Mississippi and Puerto Rico,"
+ " you have reached the age of majority.");
}
else if (yourAge >= 21)
{
Console.WriteLine(
"You have reached the age of majority in all US states.");
}
Main
method.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:
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, $0$, and remember that $0$ is even, etc.) and make sure that what
is displayed on the screen is always correct.
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:
// First, we obtain the number from the user:
int answer;
Console.WriteLine("Enter an integer");
answer = int.Parse(Console.ReadLine());
// Then we test and display accordingly:
if (answer >= 0 && answer % 2 == 0){
Console.WriteLine("Positive and even");
}
else if (answer >= 0 && answer % 2 != 0) {
Console.WriteLine("Positive and odd");
}
else if (answer < 0 && answer % 2 == 0){
Console.WriteLine("Negative and even");
}
else if (answer < 0 && answer % 2 != 0){
Console.WriteLine("Negative and odd");
}
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:
// First, we obtain the number from the user:
// (This part is unchanged)
int answer;
Console.WriteLine("Enter an integer");
answer = int.Parse(Console.ReadLine());
// Then, we declare the string that will
// contain the message we will be displaying:
string msg;
// Then, we test the value, constructing
// the string to display as we go:
if (answer >= 0)
{
msg = "Positive";
}
else // This is the same as "if (answer < 0)".
{
msg = "Negative";
}
if (answer % 2 == 0)
{
msg += " and even"; // The "+=" operator adds to the end of the string
}
else // This is the same as "if (answer % 2 != 0)".
{
msg += " and odd";
}
// And *finally* we display the message:
Console.WriteLine(msg);
This is arguably much better, for the following reasons:
answer >= 0
),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:
Some tips:
Here is an example of execution, where the user input is underlined, and hitting “enter” is represented by ↵:
How many adults?
2͟↵
How many children?
4͟↵
Since you are more than 6, you can buy a group pass for $30. Are you interested (please enter "Y" for "yes" or anything else for "no")?
Y͟↵
Your total is $30.00 (you saved $0.00).
The information about the age of majority comes from wikipedia. ↩