Introduction to Classes – Rectangle Class
May 21, 2022 (04:25:15 PM)
Using a Pre-Defined Class
This lab will guide you in your first manipulation of a programmer-defined class. The 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.
Manipulating Two .cs
Files at a Time
- Download 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
andRectangle.cs
. - In the Solution Explorer, double-click on
Rectangle.cs
and note how close it is to what was presented during the lecture. - In the Solution Explorer, double-click on
Program.cs
and observe it. - Compile and execute the code.
- Now, do the following:
- Introduce a syntactical error in
Program.cs
(e.g., remove a;
), and try to build the solution: what do you observe? Restore the program to its previous state. - Introduce a syntactical error in
Rectangle.cs
(e.g., remove a;
), and try to build the solution: what do you observe? Undo the modification. - Add
length = 12;
in the main method ofProgram.cs
and try to build the solution: what do you observe? Undo the modification.
Enriching Program.cs
Edit the Main
method of Program.cs
by adding at its end statements that perform the following:
- Create a second
Rectangle
object and set its length and width to 3. - Create a third
Rectangle
object and ask the user to specify its length and width. Display the area of this rectangle on the screen. - Create a fourth
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 the documentation on default values of C# types.
Editing Rectangle.cs
Edit Rectangle.cs
:
Rename every instance of
lengthParameter
tolengthP
in theSetLength
method (that is, replace both occurrences).You can use 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 run your program. What do you observe? What happens if you change one instance to
lengthP
while leaving the other aslengthParameter
? 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 evenm_
. You can always find someone furiously advocating for one particular convention, but the truth is that if you’re not forced to use one (for example, by the rules of a software company you work for), you should pick whichever suits you best. Still, just to use it at least once, rename every instance ofwidth
intom_width
and see how it feels. Compile and run your program. What do you observe? Either undo this modification or renamelength
intom_length
(you have to be consistent!).Change the name of one of the accessor methods in
Rectangle.cs
without changing it inProgram.cs
. Compile and run 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?
Enriching Rectangle.cs
Taking inspiration from the ComputeArea()
method, write three new methods:
- A method that returns the perimeter of the calling object.
- A method that doubles the length and the width of the calling object.
- A method that swaps the length and the width of the calling object.
For each method: pick a (valid) name, think about the return type and the parameters, and write the body of the method carefully. After compilation succeeds, 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.