September 7, 2020
Create a new project.
Write two statements, one that declares a variable of type int named intVar and 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 assigns the value of stringVar to intVar. Why is the compiler complaining? Comment out the statement you just added (that is, add // 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 on the screen.
This lab will guide you in your first manipulation of a programmer-defined class. We will use the example shown in class. The last part is challenging; therefore, we will give a possible solution to it in class, but make sure you try to solve it by yourslef beforehand.
.cs Files at a Timecs files listed: Program.cs and Rectangle.cs.Rectangle.cs and note how close it is from what was presented during the lecture.Program.cs and observe it.Program.cs (e.g., remove a ;), and try to build the solution: what do you observe? Restore the program to its previous state, using CTRL + z to “undo” your operation.Rectangle.cs (e.g., remove a ;), and try to build the solution: what do you observe? Undo the modification using CTRL + z.length = 12; in the main method of Program.cs and try to build the solution: what do you observe? Undo the modification using CTRL + z.Program.csEdit the Main method of Program.cs by adding at its end statements that perform the following:
Rectangle object and set its length and width to 3.Rectangle object and ask the user to specify its length and width. Display the area of this rectangle on the screen.Rectangle object, do not specify its length or width, and display them on the screen. What do you observe?In the last part, you may notice that the length and the width of the newly created object were assigned default values. To know more about this, refer to https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/default-values-table.
Rectangle.csEdit Rectangle.cs:
lengthParameter to lengthP in the SetLength method (that is, replace the two occurrences). You can use the symbol refactoring of C# to do so. Compile and run your program. What do you observe?_ (the underscore character), m (for “member”), or even m_. You will always find someone furiously advocating for one particular convention, the truth is that if you’re not forced to use one, you should pick whichever suits you best. Still, just to use it at least once, rename every instance of width into m_width and see how it feels. Compile and run your program. What do you observe? Either undo this modification or rename length into m_length (you have to be consistent!).Rectangle.cs without changing it in Program.cs. Compile and run your program. What do you observe? Undo your modification.Rectangle.csBy taking inspiration from the ComputeArea() method, write three new methods:
For each method: pick a (valid) name, think about the return type and the parameters, and write the body of the method carefully. After compliation succeeds, call that method in Program.cs and see if it has the expected behavior.
You can compare your answer with this proposition: Rectangle_Sol.zip.