Skip to content

Commit 104f008

Browse files
committed
feat: Add Cylindertest1 with basic geometry methods for cylinders
WHAT the code does: Defines Cylindertest1 with public fields radius and height. Implements geometry methods: - lidArea(): returns area of the circular lid (πr²). - circumference(): returns circumference of the base circle (2πr). - totalSurfaceArea(): computes total surface area (2πr² + 2πrh). - volume(): currently returns only lidArea() instead of πr²h. Defines CylinderTest with main(): - Creates a cylinder object with radius = 7 and height = 10. - Prints lid area, total surface area, and volume. WHY this matters: Demonstrates how to model a cylinder with fields and methods in Java. Highlights the bundling of state (radius, height) and behavior (area, surface, volume). Shows how mathematical formulas can be encapsulated in methods for reusability. Provides an accessible educational example of class and object usage. HOW it works: lidArea() computes πr² = 153.938... circumference() computes 2πr = 43.982... totalSurfaceArea() adds two lid areas + lateral surface = 2πr² + 2πrh. main() sets radius and height, then prints computed results. Tips and gotchas: The volume() method is incorrect; it should be πr²h, not just lidArea(). radius and height are public, which breaks encapsulation. Prefer private fields with getters and setters. Method naming consistency matters: totalSurfaceArea is clear, but lidArea could be renamed baseArea for symmetry. Using double precision is fine for small examples, but rounding/formatting may be needed for real-world output. Use-cases: Educational demo of geometry modeling in Java. Foundation for math/physics simulation applications. Can evolve into an encapsulated, validated design with constructors and private fields. Short key: class-cylindertest1 cylinder-geometry area-surface-volume. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent dec4f0e commit 104f008

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

Section11ObjectOrientedProgramming/src/CylinderTest.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
class Cylindertest1
2-
{
1+
class Cylindertest1 {
32
public double radius;
43
public double height;
54

6-
public double lidArea()
7-
{
5+
public double lidArea() {
86
return Math.PI*radius*radius;
97
}
10-
public double totalSurfaceArea()
11-
{
8+
9+
public double totalSurfaceArea() {
1210
return 2*lidArea()+circumference()*height;
1311
}
14-
public double circumference()
15-
{
12+
13+
public double circumference() {
1614
return 2*Math.PI*radius;
1715
}
18-
public double volume()
19-
{
16+
17+
public double volume() {
2018
return lidArea();
2119
}
2220
}
2321

2422
public class CylinderTest {
2523
public static void main(String[] args) {
2624
Cylindertest1 c = new Cylindertest1();
27-
2825
c.radius=7;
2926
c.height=10;
3027

0 commit comments

Comments
 (0)