August 26, 2019
Create a new project.
Write two statements, one that declares a variable of type int named intVar, one that declares a variable of type string named stringVar.
Assign the value 3 to intVar, and "4" to stringVar.
Display the values of intVar and stringVar.
Write a statement that assign the value of stringVar to intVar. Why is the compiler complaining? Comment out the statement you just added (that is, add a // in front of it, so that the compiler won’t try to execute it).
Copy the following statement, to “convert” the string value of stringVar into an integer value, and assign it to intVar:
Using https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number, try to understand what just happened.
Change the value of stringVar to be "Train" and assign it to intVar using int.Parse as previously shown. What happened?
Create a new project.
Declare three variables: one of type int, one of type float, and one of type double.
We can use the following to read an int from the user (assuming your int variable is named intVar):
Similarly, we can read float using float.Parse(Console.ReadLine()), and double using double.Parse(Console.ReadLine()).
Write statements that ask the user to enter an int, a float and a double, store those values in the appropriate variables, and then display them at the screen.
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.
You accomplished a lot since you started this class. Let’s take a moment to look back at what you learned, and to make sure you understand all those notions and skills. If you have a doubt, feel free to look back at the corresponding lab.
Write and WriteLine,Maybe you decided what your major was going to be. Maybe you changed your mind. Maybe you’re not sure. Being confused and uncertain is sometimes part of the process of taking decisions and learning, and that’s all right. It is normal to hesitate. This page by a colleague in Computer Science may be a good read for students hesitating between IT, CS and MIS, or wanting to have more information about those majors.
Here is a problem that involves almost all the previously mentioned notions and skills. It is phrased in a more abstract way, closer to the kind of problem you will be facing if you were a software-developer. Think about what you need to do before starting to type your code, and when you start writing your code, make sure you compile it frequently.
Write a program that asks the user for their name, their number of guests, and the number of pizza slices they have. Your program should then display the name of the user, the number of people at the party, the number of slices, and how many slices will each guest have, assuming the slices will be shared equitably.
A couple of additional precisions: