September 13, 2018
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, 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.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. Display the area of this rectangle at the screen.Rectangle
object, do not specify its length or width, and display them at 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.cs
Edit 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 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.