Problem overview
A class bundles values with the questions you can ask about them. Rectangle remembers a width and a height, and answers three questions: its Area, its Perimeter and whether it IsSquare. Each object keeps its own sizes, so two rectangles never interfere with each other.
For a 3 by 4 rectangle the area is 3 * 4 = 12, the perimeter is 3 + 4 + 3 + 4 = 14, and it is not a square. For 5 by 5 the area is 25, the perimeter 20, and it is a square.
Approach
Each question needs only the two sizes, so the class stores them once and every method works from them. That makes each method O(1): a multiplication, an addition or a comparison, whatever the sizes are.
Two parts of a class matter here. Fields are the variables each object carries. The constructor has the same name as the class, no return type, and runs when you write new Rectangle(3, 4). Its job is to copy the parameters into the fields.
Step by step
- Declare the fields. Two
public intfields,widthandheight, one line each. Every object gets its own copy. - Store the parameters. The constructor's parameters have the same names as the fields, so inside it
widthmeans the parameter andthis.widthmeans the field of the object being created. - Area: return
width * height. Inside a method, plainwidthis the field of the object the method was called on. - Perimeter: return
2 * (width + height). The parentheses make the addition happen before the multiplication. - IsSquare: return
width == height. A comparison is already abool, so noifis needed.
The solution in C#
class Rectangle {
public int width;
public int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public int Area() {
return width * height;
}
public int Perimeter() {
return 2 * (width + height);
}
public bool IsSquare() {
return width == height;
}
}
Using it:
var r = new Rectangle(3, 4);
Console.WriteLine(r.Area() + " " + r.Perimeter() + " " + r.IsSquare());
var q = new Rectangle(5, 5);
Console.WriteLine(q.Area() + " " + q.Perimeter() + " " + q.IsSquare());
r.width = 10;
Console.WriteLine(r.Area() + " " + q.Area());
This prints:
12 14 False
25 20 True
40 25
C# prints a bool as True or False when it is joined to a string; the lesson's tests print the answers the same way. The last two lines show that each object has its own sizes: changing r.width changes r.Area() and leaves q alone. Because the fields are public, any code can do that.
Edge cases and common mistakes
- Forgetting
this.in the constructor. Writingwidth = width;assigns the parameter to itself and leaves the field at 0. The compiler warns about it, and every area would then be 0. - Perimeter without parentheses.
2 * width + heightis not the perimeter. Use2 * (width + height). - Squares. A square is a rectangle with equal sides, so
Rectangle(5, 5).IsSquare()is true. A 2 by 10 and a 10 by 2 rectangle have the same area and perimeter (20 and 24) and neither is a square. - Method names. C# is case-sensitive, and the methods are
Area,PerimeterandIsSquarewith a capital first letter, as in the starter code.
How the class works
A class is a blueprint, and new Rectangle(3, 4) builds one object from it. The class says that every rectangle has a width and a height. Each object then holds its own two numbers. When you call r.Area(), the method runs on r, so width and height inside it are r's values. The same method called on another object uses that object's values.
Notice that the methods take no parameters. The sizes were handed over once, in the constructor, and the object remembers them. This is the difference between a class and a plain function such as Area(width, height), which would need the sizes again on every call.
The size limits are small: each side is at most 10,000, so the area is at most 100,000,000, and that fits in an int.
Testing
Try these rectangles and compare:
Rectangle(3, 4)gives area 12, perimeter 14,False.Rectangle(5, 5)gives 25, 20,True.Rectangle(2, 10)andRectangle(10, 2)both give 20, 24,False: turning a rectangle on its side changes neither number.Rectangle(1, 1)gives 1, 4,True.Rectangle(10000, 10000)gives 100000000, 40000,True.
Other ways to write it
C# lets a method with a single return use the shorter expression-bodied form. These three methods behave exactly like the ones above, and for a 3 by 4 rectangle they give 12, 14 and False:
public int Area() => width * height;
public int Perimeter() => 2 * (width + height);
public bool IsSquare() => width == height;
The lesson's fields are public to keep the first class simple. Larger C# programs usually make fields private and expose them through properties, so that the class decides how its values can change. For a small class like this one, public fields are fine.
One more detail: int arithmetic wraps silently on overflow, so a very large width and height would give a wrong area without any error. The lesson's limits keep every answer well inside the range of int.
Key takeaways
- Fields hold what each object remembers; methods answer questions about it.
- The constructor copies its parameters into the fields, using
this.when the names match. - A comparison such as
width == heightis already abool.
