Skip to main content

Command Palette

Search for a command to run...

Designing a Simple Geometry Calculator with OOP in Java

Designing a geometry calculator in Java starts with thinking in terms of objects and classes. In an indirect lead-in, imagine a software that needs to compute areas and perimeters of shapes – using object-oriented design makes it clean and extens...

Published
5 min read
Designing a Simple Geometry Calculator with OOP in Java
T

I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.

1. Defining Shape Classes with Inheritance

1.1 In our design

We begin with an abstract Shape class that declares two abstract methods: one for area and one for perimeter. For example:

// Shape.java – an abstract base class
abstract class Shape {
public abstract double getArea();
public abstract double getPerimeter();
}

This code shows an abstract Shape class with getArea() and getPerimeter() as abstract methods. The class itself has no fields; it simply ensures that every subclass must implement these methods. Since the class is abstract, new Shape() is not allowed – only concrete subclasses (like Circle, Rectangle, etc.) can be instantiated. This approach enforces a consistent interface: any subclass of Shape is guaranteed to have methods for calculating area and perimeter, even though each shape will do so differently

1.2 Circle subclass

Consider a Circle that extends Shape. It stores a radius field and implements the abstract methods using circle formulas. For instance:

class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI radius radius; // πr^2
}
@Override
public double getPerimeter() {
return 2 Math.PI radius; // 2πr
}
}

In this code, the Circle class overrides getArea() and getPerimeter(). Its getArea() returns Math.PI radius radius, which implements the formula π . The getPerimeter() returns 2 Math.PI * radius, the circumference formula. Because Circle extends Shape, the compiler enforces that these methods must be provided. This demonstrates inheritance: Circle reuses the structure from Shape and fills in the specific calculations.

1.3 Rectangle subclass

Similarly, a Rectangle class holds length and width and provides its own formulas. Example code:

class Rectangle extends Shape {
private double length, width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double getArea() {
return length width; // length × width
}
@Override
public double getPerimeter() {
return 2
(length + width); // 2*(length + width)
}
}

Here the getArea() method multiplies length and width, and getPerimeter() computes 2*(length + width). These match the standard rectangle. The code clearly separates the data (length, width) and behavior (area, perimeter). The object carries its dimensions and knows how to calculate results. This encapsulation means we don’t mix shape data with other logic. The Rectangle class inherits the abstract contract from Shape and fulfills it with its own logic.

1.4 Triangle subclass

A Triangle class can store three sides and use Heron’s formula to compute area. For example:

class Triangle extends Shape {
private double a, b, c;
public Triangle(double a, double b, double c) {
this.a = a; this.b = b; this.c = c;
}
@Override
public double getArea() {
double s = (a + b + c) / 2; // semi-perimeter
return Math.sqrt(s (s - a) (s - b) * (s - c));
}
@Override
public double getPerimeter() {
return a + b + c;
}
}

In this snippet, getArea() applies Heron’s formula: compute the semi-perimeter s, then take the square root of s(s-a)(s-b)*(s-c). The getPerimeter() simply sums the sides. As with the other classes, Triangle fulfills the abstract methods from Shape. Each shape class is self-contained: it holds its parameters and knows how to compute area and perimeter using formulas specific to that shape.

2. Implementing the Geometry Calculator Logic

With these classes defined, a simple geometry calculator can create and use shape objects. For example, a main method might look like this:

public class GeometryCalculator {
public static void main(String[] args) {
Shape[] shapes = {
new Circle(5),
new Rectangle(10, 12),
new Triangle(7, 8, 6)
};
for (Shape s : shapes) {
System.out.println("Area: " + s.getArea());
System.out.println("Perimeter: " + s.getPerimeter());
}
}
}

In this main method, we create an array of Shape references holding a Circle, a Rectangle, and a Triangle. We then loop through each shape and call getArea() and getPerimeter() on it. This is polymorphism in action: the code doesn’t need to know what kind of shape s is at compile time. Instead, at runtime the correct method (circle’s area formula or rectangle’s, etc.) is invoked for each object. A classic note explains that calls to area() are late-bound: the actual method invoked is determined by the object’s class at runtime. The same interface works for all shapes thanks to our abstract base class.

Benefits of the OOP design

This approach has several advantages. First, adding a new shape later (say a Square class) doesn’t require changing the loop in main; you simply make a new subclass of Shape and it will work automatically. As one reference notes, new subclasses can be written later and the main program will still work without modification. This shows the power of abstraction and polymorphism: the GeometryCalculator treats all shapes generically, which simplifies the code and makes it extensible. Second, encapsulating shape logic inside each class means the formulas are not scattered all over the code. Each shape knows how to compute itself, improving clarity and maintainability. In short, by using inheritance, abstract methods, and polymorphism, our backend logic remains organized and flexible – exactly what a robust geometry calculator needs.

If you have questions about this design or want to discuss other geometry calculator techniques, leave a comment below!

Read more at : Designing a Simple Geometry Calculator with OOP in Java

More from this blog

T

tuanh.net

540 posts

Are you ready to elevate your Java, OOP, Spring, and DevOps skills? Look no further!