This lab serves multiple goals:
In this exercise, you will create your own class instead of using and
expanding one that was written for you. The idea is to take inspiration
from the class you have already encountered (Rectangle
) to create a
new class, called PreciseRectangle
, that will manipulate rectangles
whose width and length are floating-point values instead of integers (as
in Rectangle
).
This should be a fairly straightforward exercise that mostly reinforces what you have already encountered, but you will be exposed to creating new classes in your IDE for the first time.
To implement your class in your IDE, you are given two methods below: you can edit the pre-existing project or you can create a new blank class. It is recommended to pick the one that you feel the most comfortable with initially, and then you should try the other technique. You will need to know how to edit existing projects and how to create new ones.
Re-download the “Rectangle” project, extract it in a folder, and open it with your IDE.
Within your IDE, re-name the project to “PreciseRectangle”, and rename the “Rectangle.cs” file to “PreciseRectangle.cs”
It is important that you rename the files within your IDE. If you try to rename your files, or their folders, outside of the IDE then it will break your solution. The solution will still be looking for the original file/folder names, and will not recognize the changed names. If such an error occurs, restore the previous names and then rename your files through the IDE as instructed.
In the “PreciseRectangle.cs” file, replace class Rectangle
with
class PreciseRectangle
.
Comment out the body of the Main
method in “Program.cs”.
Your program should compile as it is, but you need to edit
PreciseRectangle.cs
to now store the width
and the length
attributes as type double
, and then you will need to edit the rest
of the class accordingly. (e.g., What should the return type of the
GetWidth
method be?)
Declare and manipulate precise rectangles (with double
values for
the width and the length) in the Main
method, and make sure they
behave as expected (i.e., Can you compute the area, set attributes,
etc.?).
Add the missing methods ComputePerimeter
, Swap
, and
MultiplyRectangle
, as described in the Rectangle
lab but also below.
Create a new project in your IDE, and name it “PreciseRectangle”.
In the Solution Explorer, right-click on “PreciseRectangle”, then on “Add…” and select “Class”. Then, select “Class” in the dialog box, write “PreciseRectangle.cs” as the name of the file, and click on “Add”.
You should now have two “.cs” files opened and displayed in the Solution Explorer: “Program.cs” and “PreciseRectangle.cs”.
Implement the PreciseRectangle
class according to the following
specifications:
width
and length
, of type
double
ComputePerimeter
to compute the perimeter of a
precise rectangle,Swap
to swap the length and the width of a
precise rectangle, andMultiplyRectangle
to multiply the length and
width of a precise rectangle by a factor given in the argument
as an integer.Declare and manipulate rectangles with floating-point (i.e.,
double
) values for the width and the length in the Main
method,
and make sure they behave as expected (i.e., Can you compute the
area, set attributes, etc.?).
The following is an independent task with the goal of widening your understanding of this class and preparing you for the next labs. Now that you know more about naming conventions, have a look at Microsoft’s naming guidelines, and particularly at: