Commit dec4f0e
committed
feat: Add CylinderTestDemo with constructors, setters, getters, and geometry methods
WHAT the code does:
Defines a CylinderTestDemo class with private fields radius and height.
Implements multiple constructors:
- Default constructor: initializes radius and height to 1.
- Parameterized constructor: initializes radius and height with given values.
Provides getter methods getRadius() and getHeight().
Provides setter methods setRadius(int r), setHeight(int h), and setDimensions(int h, int r) with validation to prevent negative values.
Adds geometry methods:
- lidArea(): area of the circular lid (πr²).
- perimeter(): circumference of the circle (2πr).
- drumArea(): total surface area of the cylinder (2πr² + 2πrh).
- volume(): volume of the cylinder (πr²h).
Defines Cylinder with main():
- Creates a CylinderTestDemo object.
- Sets radius and height using setters and setDimensions().
- Prints all computed values and validates getters.
- Uses printf with %.2f for formatted numerical output.
WHY this matters:
Demonstrates encapsulation with private fields and controlled access via getters and setters.
Shows constructor overloading for flexible initialization of objects.
Encodes validation logic in setters, preventing invalid states (negative radius or height).
Illustrates real-world object modeling by combining data (radius, height) with relevant operations (area, volume).
Highlights string concatenation vs arithmetic addition nuances in Java when mixing with System.out.printf.
HOW it works:
Default constructor initializes radius = height = 1.
Setters enforce non-negative values (if negative, assign 0).
lidArea computes πr², perimeter computes 2πr, drumArea adds lid areas and lateral surface, volume multiplies base area by height.
Main method demonstrates usage and outputs calculated geometry values.
Tips and gotchas:
The method setDimensions(int h, int r) flips the conventional (r,h) order, which may cause confusion.
Surface area formula in drumArea is correct but could be made clearer with explicit terms.
Public setters allow changing state freely; in immutable designs, prefer constructor-only initialization.
Naming consistency (e.g., drumArea vs surfaceArea) would improve clarity.
Add constructor chaining (this(...)) to reduce duplication across constructors.
Use-cases:
Educational demonstration of constructor overloading, encapsulation, and object behavior.
Practical geometric utility for calculating cylinder properties.
Foundation for extending into engineering or graphics applications.
Short key: class-cylindertestdemo constructors encapsulation geometry-methods.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 3bc15ec commit dec4f0e
1 file changed
+29
-30
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | | - | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
21 | | - | |
22 | | - | |
| 20 | + | |
23 | 21 | | |
24 | 22 | | |
25 | 23 | | |
26 | | - | |
27 | | - | |
| 24 | + | |
| 25 | + | |
28 | 26 | | |
29 | 27 | | |
30 | | - | |
31 | | - | |
| 28 | + | |
| 29 | + | |
32 | 30 | | |
33 | 31 | | |
34 | | - | |
35 | | - | |
| 32 | + | |
| 33 | + | |
36 | 34 | | |
37 | 35 | | |
38 | 36 | | |
39 | 37 | | |
40 | 38 | | |
41 | | - | |
42 | | - | |
| 39 | + | |
| 40 | + | |
43 | 41 | | |
44 | 42 | | |
45 | 43 | | |
46 | 44 | | |
47 | 45 | | |
48 | | - | |
49 | | - | |
| 46 | + | |
| 47 | + | |
50 | 48 | | |
51 | 49 | | |
52 | 50 | | |
53 | | - | |
54 | | - | |
| 51 | + | |
| 52 | + | |
55 | 53 | | |
56 | 54 | | |
57 | | - | |
58 | | - | |
| 55 | + | |
| 56 | + | |
59 | 57 | | |
60 | 58 | | |
61 | | - | |
62 | | - | |
| 59 | + | |
| 60 | + | |
63 | 61 | | |
64 | 62 | | |
65 | | - | |
66 | | - | |
| 63 | + | |
| 64 | + | |
67 | 65 | | |
68 | 66 | | |
69 | 67 | | |
| 68 | + | |
70 | 69 | | |
71 | | - | |
72 | | - | |
| 70 | + | |
73 | 71 | | |
74 | 72 | | |
75 | 73 | | |
| |||
85 | 83 | | |
86 | 84 | | |
87 | 85 | | |
88 | | - | |
| 86 | + | |
| 87 | + | |
89 | 88 | | |
90 | 89 | | |
91 | 90 | | |
| |||
111 | 110 | | |
112 | 111 | | |
113 | 112 | | |
114 | | - | |
| 113 | + | |
0 commit comments