September 3, 2020
Download the PersonalizedWelcomeMessage project.
Extract it and open it in VS.
(If you are using VS on MAC, follow the instructions at https://stackoverflow.com/a/49056993/ to have your project “Run on external console”. You will need to do that for every solution where the user is supposed to enter values.)
Compile and execute it.
You will be prompted with the message:
Please, enter your first name, followed by "Enter":
Enter your first name, followed by Enter ↵. You just witnessed an interaction between a program and the user!
Read the source code carefully and make sure you understand all of it.
Change the code so that the program would also ask for the user’s last name and print both their first and last names.
For this part, it is recommended to have the cheatsheet we discussed last time. Note that it contains numerous references at its end, you are encouraged to open those links if you have not already, to have a look at the official documentation, which should not scare you.
This part should be first carried out without using VS.
Assume we have the following statements:
Answer the following:
| Operation | Legal? | Result | Datatype |
|---|---|---|---|
a + d |
Yes | 19.7 | double |
m + f |
No | _ | _ |
a / b |
_ | _ | _ |
b * f |
_ | _ | _ |
d + f |
_ | _ | _ |
d + b |
_ | _ | _ |
a + m |
_ | _ | _ |
f / m |
_ | _ | _ |
d * m |
_ | _ | _ |
You can check your answers using VS: create a new project, copy the variable declarations and assignments, and write your own statements to perform the calculations in the Main method. For instance, if you want to check that the result of a + d is of type double, write something like:
Create a new project, and then do the following.
Add in your program the following:
You will get an error that reads
Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)Can you explain it?
VS is suggesting that we use a “cast” to “force” C# to store the value of the variable floatVar into the variable intVar. To do so, replace the previous statement with the following:
Using a Console.WriteLine statement, observe the value stored in intVar. Can you tell if the value stored in floatVar was rounded or truncated before being stored in the variable intVar? Conduct further experiments if needed to answer this question.