Precise Rectangle and Circle Class
May 21, 2022 (04:25:15 PM)
Writing Your Own PreciseRectangle Class
In this exercise, you will create your own first class instead of using and expanding one that was written for you. The idea is to take inspiration from the class you already know (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 re-enforce what you should know already (except for how to create a class in your IDE).
Implementation
To implement your class in your IDE, you are given two methods below: you can edit the pre-existing project, or start “fresh”. It is recommended to pick the one you feel the most comfortable with (you will get an opportunity to start “fresh” in the next problem in any case).
Edit the Pre-Existing Project
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 re-name 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
withclass PreciseRectangle
.Comment out the body of the
Main
method in “Program.cs”.Your program should compile as it is, but you have to edit
PreciseRectangle.cs
to now store thewidth
and thelength
withdouble
, and to propagate this change accordingly. What should be the return type ofGetWidth
, for instance?Declare and manipulate precise rectangles (with non-integer values for the width and the length) in the
Main
method, and make sure they behave as expected (can you compute the area, for instance?).Add the missing methods
ComputePerimeter
,Swap
,MultiplyRectangle
, described in the section below.
Starting From Scratch
Create a new project in your IDE, 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 specification:- it should have two attributes,
width
andlength
, of typedouble
- it should have eight methods:
- two setters, two getters (i.e., one for each attribute),
- one method to compute the area of a precise rectangle,
- one method
ComputePerimeter
to compute the perimeter of a precise rectangle, - one method
Swap
to swap the length and the width of a precise rectangle, - one method
MultiplyRectangle
to multiply the length and width of a precise rectangle by a factor given in the argument as an integer.
- it should have two attributes,
Declare and manipulate rectangles with floating-point (i.e.
double
) values for the width and the length in theMain
method, and make sure they behave as expected (can you compute the area, for instance?).
Writing A Circle Class
This time, you will have to start your project “from scratch” and shouldn’t try to edit a previous program.
Foundations
- Create a new project in your IDE, name it “Circle”.
- In the Solution Explorer, right-click on “Circle”, then on “Add…” and select “Class”. Then, select “Class”, write “Circle.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
andCircle.cs
. - Declare a single instance variable in
Circle.cs
, of typedouble
and namedradius
. Write aset
and aget
method for this instance variable. - In
Program.cs
, write statements that create a newCircle
object and set its radius to 2.3. Display its radius on the screen using the method you defined previously.
Extending the Class
- In C#, the constant
Math.PI
is adouble
holding an approximation of π. In theMain
method ofProgram.cs
, write a statement that displays its value on the screen. It should be 3.14159265358979. - Now, edit this statement and use the format specifier
N
, to display the value of π rounded to 3.14. - In the
Circle.cs
file, add two methods:- A method that returns the circumference of the circle that calls it (i.e., 2π times the radius),
- A method that returns the area of the circle that calls it (i.e., π times the radius squared).
- Test those two methods in your
Main
program, by displaying on the screen the area and the circumference of the object you created in the previous exercise. - Use the format specifier
N
to round the circumference.
You can find a possible solution to this problem in this archive.
Pushing Further (Optional)
The following is an independent task, to widen your understanding of this class, and to prepare you for the next labs.
- Now that you know more about naming conventions, have a look at Microsoft’s naming guideline, and particularly at