CSCI 1301 – Lab 06

Clément Aubert

September 12, 2020

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 the UML part, and for how to create a class in Visual Studio).

Conception

Draw the UML diagram of this class: it should have two attributes, of type double, and eight methods:

Implementation

To implement your method in VS, 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

  1. Re-download the Rectangle project, extract it in a folder, open it with VS.
  2. Within VS, re-name the project to “PreciseRectangle”, rename the “Rectangle.cs” file to “PreciseRectangle.cs”
  3. In the “PreciseRectangle.cs” file, replace class Rectangle with class PreciseRectangle.
  4. Comment out the body of the Main method in “Program.cs”.
  5. Your program should compile as it is, but you have to edit PreciseRectangle.cs to now store the width and the length with double, and to propagate this change accordingly. What should be the return type of GetWidth, for instance?
  6. Declare and manipulate precise rectangles (i.e., with floating-point 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?).
  7. Add the missing methods (ComputePerimeter, Swap, MultiplyRectangle).

Starting From Scratch

  1. Create a new project in VS, name it “PreciseRectangle”.
  2. In the Solution Explorer, right-click on “PreciseRectangle”, then on “Add…” and select “Class”. Then, select “Class”, write “PreciseRectangle.cs” as the name of the file, and click on “Add”.
  3. You are now suppose to have two “.cs” files opened and displayed in the Solution Explorer: “Program.cs” and “PreciseRectangle.cs”.
  4. Implement the PreciseRectangle class according to your UML diagram. Don’t forget about the ComputePerimeter, Swap, and MultiplyRectangle methods.
  5. Declare and manipulate rectangles with floating-point 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?).

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

  1. Create a new project in VS, name it “Circle”.
  2. 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”.
  3. You are now suppose to have two .cs files opened and displayed in the Solution Explorer: Program.cs and Circle.cs.
  4. Declare one single instance variable in Circle.cs, of type double and named radius. Write a set and a get method for this instance variable.
  5. In Program.cs, write statements that create a new Circle object and set its radius to 2.3. Display its radius at the screen using the method you defined previously.

Extending the Class

  1. In C#, Math.PI is a double holding an approximation of π. In the Main method of Program.cs, write a statement that displays its value at the screen. It should be 3.14159265358979.
  2. Now, edit this statement and use the format specifier N, to display the value of π rounded to 3.14.
  3. In the Circle.cs file, add two methods:
    1. A method that returns the circumference of the circle that calls it (i.e., 2π times the radius),
    2. A method that returns the area of the circle that calls it (i.e., π times the radius squared).
  4. Test those two methods in your Main program, by displaying at the screen the area and the circumference of the object you created at the previous exercise.
  5. Use the format specifier N to round the circumference.

Pushing Further (Optional)

The following are two independent tasks, to widen your understanding of this class, and to prepare you for the next labs.

  1. Class diagram (the one we will be using) are just a special case of UML diagram. Have a look at https://en.wikipedia.org/wiki/Unified_Modeling_Language#Diagrams. In which category are class diagram: behaviour, or structure diagram? Have a look at https://en.wikipedia.org/wiki/Activity_diagram and try to read the example of activity diagram for a guided brainstorming process.

  2. Now that you know more about naming convention, have a look at https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines, and particularly at