Commit 8fa6ed0
committed
feat: Add Circle class with methods for area, perimeter, and circumference
WHAT the code does:
Defines a Circle class with:
- radius field representing the circle’s size.
- area(): computes area using πr².
- perimeter(): computes perimeter, though currently using 2 * π * r² instead of the standard 2πr.
- circumferenec(): calls perimeter() to return the same value.
Defines AreaandPerimeter with main():
- Creates two Circle objects (radii 7 and 14).
- Prints area, perimeter, and circumference for each.
WHY this matters:
Demonstrates class-based modeling of geometric entities.
Shows how methods encapsulate formulas for properties of a circle.
Illustrates reusability: multiple Circle objects use the same logic with different radii.
Highlights object-oriented principles: data (radius) and behavior (area, perimeter) are bundled.
HOW it works:
c1 and c2 are instantiated with different radii.
Each object calls area(), perimeter(), and circumferenec().
Results are printed for both circles.
Tips and gotchas:
The formula in perimeter() is incorrect: it uses 2πr² instead of 2πr. This makes circumferenec() also return the wrong value.
circumferenec() is redundant if it just mirrors perimeter(); normally circumference is another name for perimeter.
radius is public; in real-world code, prefer private with getters/setters or constructors for better encapsulation.
Overriding toString() would simplify printing all properties at once.
Use-cases:
Educational demo of methods, object instantiation, and encapsulation of mathematical logic.
Foundation for a geometry library handling multiple shapes.
Useful for teaching class design and method reuse in beginner-level Java.
Short key: class-circle area-perimeter circumference-geometry.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 61cf64e commit 8fa6ed0
File tree
1 file changed
+12
-18
lines changed- Section11ObjectOrientedProgramming/src
1 file changed
+12
-18
lines changedLines changed: 12 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
5 | 2 | | |
6 | | - | |
7 | 3 | | |
8 | 4 | | |
9 | | - | |
10 | | - | |
11 | | - | |
| 5 | + | |
| 6 | + | |
12 | 7 | | |
13 | 8 | | |
14 | 9 | | |
| 10 | + | |
15 | 11 | | |
16 | | - | |
17 | | - | |
| 12 | + | |
18 | 13 | | |
19 | 14 | | |
20 | 15 | | |
21 | | - | |
22 | 16 | | |
23 | | - | |
24 | | - | |
| 17 | + | |
| 18 | + | |
25 | 19 | | |
26 | | - | |
27 | 20 | | |
28 | 21 | | |
29 | 22 | | |
30 | 23 | | |
31 | 24 | | |
32 | | - | |
33 | | - | |
| 25 | + | |
34 | 26 | | |
35 | 27 | | |
36 | 28 | | |
37 | 29 | | |
38 | | - | |
| 30 | + | |
39 | 31 | | |
40 | 32 | | |
41 | 33 | | |
42 | 34 | | |
43 | 35 | | |
| 36 | + | |
| 37 | + | |
44 | 38 | | |
45 | 39 | | |
46 | 40 | | |
47 | 41 | | |
48 | | - | |
| 42 | + | |
0 commit comments