Skip to content

Commit dec4f0e

Browse files
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

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,73 @@
11
/*
22
Properties: Radius and Height.(get and set methods)
3-
What are the methods you write getRadius() and getHight().
4-
What are the set methods we Required. First is setRadius(Parameters and make sure it is not a negative. double r) and setHeight(double h).
3+
What are the methods you write getRadius() and getHeight().
4+
What are the set methods we Required.
5+
First is setRadius(Parameters and make sure it is not a negative. double r) and setHeight(double h).
56
Read and writable properties.
67
setDimensions(double r, double h)
78
now lets write constructors name is same as a class name
89
Cylinder() first constructors and no parameters and second is Cylinder(int r) h=1 and Cylinder(int r, int h)
910
*/
1011

11-
class CylinderTestDemo
12-
{
12+
class CylinderTestDemo {
1313
private int radius;
1414
private int height;
1515

16-
public CylinderTestDemo()
17-
{
16+
public CylinderTestDemo() {
1817
radius=height=1;
1918
}
2019

21-
public CylinderTestDemo(int r, int h)
22-
{
20+
public CylinderTestDemo(int r, int h) {
2321
radius=r;
2422
height=h;
2523
}
26-
public int getHeight()
27-
{
24+
25+
public int getHeight() {
2826
return height;
2927
}
30-
public int getRadius()
31-
{
28+
29+
public int getRadius() {
3230
return radius;
3331
}
34-
public void setHeight(int h)
35-
{
32+
33+
public void setHeight(int h) {
3634
if(h>=0)
3735
height=h;
3836
else
3937
height=0;
4038
}
41-
public void setRadius(int r)
42-
{
39+
40+
public void setRadius(int r) {
4341
if(r>=0)
4442
radius=r;
4543
else
4644
radius=0;
4745
}
48-
public void setDimensions(int h, int r)
49-
{
46+
47+
public void setDimensions(int h, int r) {
5048
height=h;
5149
radius=r;
5250
}
53-
public double lidArea()
54-
{
51+
52+
public double lidArea() {
5553
return Math.PI*radius*radius;
5654
}
57-
public double perimeter()
58-
{
55+
56+
public double perimeter() {
5957
return 2*Math.PI*radius;
6058
}
61-
public double drumArea()
62-
{
59+
60+
public double drumArea() {
6361
return 2*lidArea()+perimeter()*height;
6462
}
65-
public double volume()
66-
{
63+
64+
public double volume() {
6765
return lidArea()*height;
6866
}
6967
}
68+
7069
public class Cylinder {
71-
public static void main(String[] args)
72-
{
70+
public static void main(String[] args) {
7371
CylinderTestDemo c = new CylinderTestDemo();
7472
c.setHeight(10);
7573
c.setRadius(7);
@@ -85,7 +83,8 @@ public static void main(String[] args)
8583
System.out.printf("Total Above are: %.2f%n",
8684
c.lidArea() + c.perimeter() + c.drumArea() + c.volume() + c.getHeight() + c.getRadius());
8785
/*
88-
Java evaluates expressions from left to right. Since "Total Above are " is a String, all subsequent additions (+) are treated as string concatenation rather than arithmetic addition.
86+
Java evaluates expressions from left to right. Since "Total Above are " is a String,
87+
all subsequent additions (+) are treated as string concatenation rather than arithmetic addition.
8988
Instead of adding the numerical values together, Java concatenates them as strings.
9089
Java first evaluates the arithmetic sum inside the parentheses and then concatenates the result with the string.
9190
%.2f ensures two decimal places.
@@ -111,4 +110,4 @@ public static void main(String[] args)
111110
height = 1
112111
113112
This ensures that the object has meaningful default values before any setter methods are called.
114-
*/
113+
*/

0 commit comments

Comments
 (0)