January 30, 2018
Dependencies: This lab has only one part.
Feature of the day: I recommend you activate word-wrap in VS. Refer to https://msdn.microsoft.com/en-us/library/ms165339.aspx (VS 2015) or https://docs.microsoft.com/en-us/visualstudio/ide/reference/how-to-manage-word-wrap-in-the-editor (VS 2017), to go from
to
See the difference? The horizontal scrolling disappeared, every line that is too long is “wrapped”, and this is indicated with the sign.
Make sure you understand the grading scale and my annotations. If you want to look at one possible solution, download this project. Make sure you extract it before opening it in VS.
.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.cs
Edit the Main
method of Program.cs
by adding at its end statements that perform the following:
Rectangle
object and set its length to 3 and its width to 3.Rectangle
object, and ask the user to specify its length and width. Print the area of the rectangle whose dimensions were given by the user.Rectangle
object, do not specify its length or width, and print them. What do you observe?In the last exercise, 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.cs
Edit Rectangle.cs
:
lengthParameter
to lengthP
in the SetLength
method (that is, replace the two occurrences). 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 uniformly 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.cs
By taking inspiration from the CalculateAre()
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 succeed, call that method in Program.cs
and see if it has the expected behaviour.
The following are two independent tasks, to widen your understanding of this class, and to prepare you for the next labs.
For instance, a user entering 4 guests and 2 pizzas to be cut in 8 slices should read that every member of the party will have ⌊(2 × 8)/(4 + 1)⌋ = 3 slices, and that (2 × 8)mod(4 + 1)=1 slice will be left.
Account2.sln
in the ch04/Account2/
folder of the source code of the textbook. Properties will not be introduced in this class, but if you feel confident enough to use them, feel free to do so.