This lab serves multiple goals:
.cs
files can interact in a single solution,This last part is challenging; therefore, we provide a possible solution at the end of the page, but make sure you try to solve it by yourself beforehand.
.cs
Files at a TimeDownload the Rectangle project, extract it, and open it with your IDE.
Note that in the “Solution Explorer”, there are two .cs
files
listed: Program.cs
and Rectangle.cs
:
In the Solution Explorer, double-click on Rectangle.cs
and note
how close it is to what is presented during in the lecture
notes.
In the Solution Explorer, double-click on Program.cs
and observe
it.
Compile and execute the code.
Now, do the following:
Program.cs
(e.g., remove a ;
) and
try to build the solution. What do you observe? Undo the modification.Rectangle.cs
(e.g., remove a ;
)
and try to build the solution. What do you observe? Undo the
modification.length = 12;
in the Main
method of Program.cs
and try to
build the solution. What do you observe? Undo the modification.Program.cs
Edit Program.cs
by adding a few statements at the end of its Main
method. The statements should 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 without specifying 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 the documentation on default values of C## types.
Rectangle.cs
Edit Rectangle.cs
:
Rename every instance of lengthParameter
to lengthP
in the
SetLength
method (i.e., replace both occurrences).
You can use your IDE’s rename feature to perform this operation. If you are having trouble finding or using it, see the rename guide for your IDE: Visual Studio, MonoDevelop, Rider
Compile and execute your program. What do you observe? What happens
if you change one instance to lengthP
while leaving the other as
lengthParameter
? Try it out by manually editing one of these
instances and compiling the program. Be sure to change it back
after.
Some people use the convention of prefixing instance variables with
_
(the underscore character), m
(for “member”), or even m_
.
You can always find someone furiously advocating for one particular
convention, but unless someone like an employer gives you specific
guidance, 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 execute your program.
What do you observe? Either undo this modification or rename
length
into m_length
to be consistent.
Change the name of one of the accessor methods in Rectangle.cs
without changing it in Program.cs
. Compile and execute your
program. What do you observe? Undo your modification.
What has this section taught you about variable and method names
within .cs
files and across .cs
files within the same project?
What about naming is important to the compiler, and what is only
important to the programmer?
Rectangle.cs
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
successfully compiling your program, call that method in Program.cs
and see if it has the expected behavior.
This is more challenging than the rest of the lab, so if you are unable to finish this part during the lab session, do not worry, but take the time to study a possible solution to this problem.