Skip to content

Commit 8fa6ed0

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

1 file changed

+12
-18
lines changed
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,42 @@
1-
//Area of circle and perimeter of circe.
2-
3-
class Circle
4-
{
1+
class Circle {
52
//All these are the properties of the circle class
6-
73
public double radius;
84

9-
public double area()
10-
{
11-
//Formula for area. Math.PI Built in constant givem in class.
5+
public double area() {
6+
//Formula for area. Math.PI Built in constant given in class.
127
//Predefine final here.
138
return Math.PI * radius * radius;
149
}
10+
1511
//Perimeter method
16-
public double perimeter()
17-
{
12+
public double perimeter() {
1813
//Formula for perimeter
1914
return 2 * Math.PI*radius*radius;
2015
}
21-
//One more method
2216

23-
public double circumferenec()
24-
{
17+
//One more method
18+
public double circumferenec() {
2519
//Calling the Perimeter method or you write the formula instead.
26-
2720
return perimeter();
2821
}
2922
}
3023

3124
class AreaandPerimeter{
32-
public static void main(String[] args)
33-
{
25+
public static void main(String[] args) {
3426
Circle c1 = new Circle();
3527
Circle c2 = new Circle();
3628

3729
c1.radius = 7;
38-
c2.radius=14;
30+
c2.radius = 14;
3931

4032
System.out.println("Area: "+c1.area());
4133
System.out.println("Perimeter: "+c1.perimeter());
4234
System.out.println("Circumference: "+c1.circumferenec());
4335

36+
System.out.println("\n");
37+
4438
System.out.println("Area: "+c2.area());
4539
System.out.println("Perimeter: "+c2.perimeter());
4640
System.out.println("Circumference: "+c2.circumferenec());
4741
}
48-
}
42+
}

0 commit comments

Comments
 (0)