0% found this document useful (0 votes)
4 views1 page

Java Shape Class Area and Perimeter

The document defines an abstract class 'Shape' with methods for calculating area and perimeter, which are implemented by the 'Circle' and 'Triangle' classes. The 'Circle' class calculates area and perimeter based on its radius, while the 'Triangle' class does so using its three sides. The main method demonstrates the creation of a Circle and Triangle object, and prints their respective area and perimeter values.

Uploaded by

VEENA
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Java Shape Class Area and Perimeter

The document defines an abstract class 'Shape' with methods for calculating area and perimeter, which are implemented by the 'Circle' and 'Triangle' classes. The 'Circle' class calculates area and perimeter based on its radius, while the 'Triangle' class does so using its three sides. The main method demonstrates the creation of a Circle and Triangle object, and prints their respective area and perimeter values.

Uploaded by

VEENA
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

abstract class Shape

{
abstract double calculateArea();
abstract double calculatePerimeter();
}
class Circle extends Shape
{
private double radius;
public Circle(double radius)
{
[Link] = radius;
}
@Overdrive
double calculateArea()
{
return [Link] * radius * radius;
}
@Overdrive
double calculatePerimeter()
{
return 2 * [Link] * radius;
}
}
class Triangle extends Shape
{
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Overdrive
double calculateArea()
{
double s = (side1 + side2 + side3) / 2;
return [Link](s * (s - side1) * (s - side2) * (s - side3));
}
@Overdrive
double calculatePerimeter()
{
return side1 + side2 + side3;
}
}
public class ShapeAbstract {
public static void main(String[] args) {
Circle circle = new Circle(10.0);
Triangle triangle = new Triangle(4.0, 5.0, 6.0);
[Link]("Circle - Area: " + [Link]());
[Link]("Circle - Perimeter: " +[Link]());
[Link]("Triangle Area: " +[Link]());
[Link]("Triangle Perimeter: " +[Link]());
}
}

Common questions

Powered by AI

A potential improvement to the Shape class hierarchy could include adding an interface that specifies additional methods like calculateVolume and implement it in new subclasses representing three-dimensional shapes like Sphere or Cuboid. Additionally, implementing a factory pattern to create Shape objects based on input parameters could streamline the creation process and add flexibility in how shapes are instantiated, enhancing usability and supporting more dynamic use cases .

Polymorphism allows for flexibility by enabling classes such as Circle and Triangle to provide specific implementations of methods like calculateArea and calculatePerimeter, while adhering to a common interface defined by an abstract class like Shape. This abstraction allows for the method calls to be dynamically resolved at runtime, enabling code that deals with shape operations to work uniformly across different shape types without needing to know the specific details of each shape. This improves code maintainability and extendability .

Inheritance in the code example allows Circle and Triangle to reuse the structure defined in the abstract Shape class. Method overriding enables these subclasses to specialize the calculateArea and calculatePerimeter methods to suit their specific geometric calculations. This facilitates code reuse by avoiding redundant code in every shape type, as the method signatures remain consistent and specializing logic is encapsulated within each subclass, promoting enhanced maintainability and separation of concerns within the object-oriented paradigm .

For the Circle class, the calculateArea method uses the mathematical formula for the area of a circle, πr², where r is the radius. The calculatePerimeter method uses the formula for the circumference, 2πr. For the Triangle class, the calculateArea method utilizes Heron's formula, which is derived from the semi-perimeter s = (side1 + side2 + side3) / 2 and then calculates the area as √[s(s-side1)(s-side2)(s-side3)]. The calculatePerimeter simply sums the lengths of its three sides .

The use of abstract classes, like Shape, provides benefits such as the ability to implement shared code and a clear hierarchy of classes that share common base functionality. Abstract classes can hold abstract and concrete methods, allowing for partial implementation which can be shared among subclasses. However, abstract classes limit the extensibility compared to interfaces because Java allows single inheritance for classes, which may restrict subclassing in complex systems. Interfaces, on the other hand, support multiple inheritance but do not support fields or shared code directly, requiring implementation of all methods. In the provided code, the abstract class ensures a common structure for shapes while facilitating method reuse .

Incorrect implementation of the calculateArea method in subclasses of Shape can lead to inaccurate computations of areas, which may propagate incorrect results throughout any systems relying on these calculations. This can be mitigated by implementing unit tests that validate the outputs against known results, using assert statements to symbolically check for expected values, and conducting code reviews to ensure mathematical accuracy in the formulas. Ensuring method visibility and transparency can also assist in tracing and debugging errors .

Encapsulation is achieved in the Circle and Triangle classes by declaring the class attributes as private, such as radius in Circle and side1, side2, and side3 in Triangle. This restricts direct access to these fields from outside of their respective classes. Access to these fields is controlled via the constructor and the methods calculateArea and calculatePerimeter, which operate on the private fields and provide specific behaviors related to the geometric properties .

The structure of the Shape class hierarchy supports the open-closed principle, which advocates for software entities to be open for extension but closed for modification, by defining a base abstract class Shape with abstract methods for area and perimeter calculation. New shapes can be introduced by extending the Shape class, providing specific implementations for these methods without altering the existing code of Shape or other subclasses. This facilitates enhancement and evolution of the codebase without changing the behavior of the existing implementation .

The main method in the ShapeAbstract class serves as the entry point of the Java program, where instances of Circle and Triangle are created with specific dimensions. It demonstrates the use of the defined classes by invoking the calculateArea and calculatePerimeter methods on these objects, which prints the area and perimeter of both shapes to the console. This highlights the practical use of polymorphism and encapsulation in the program .

The abstract class Shape contains two abstract methods, calculateArea and calculatePerimeter, which do not have an implementation in the abstract class itself. By declaring these methods as abstract, Java enforces that any concrete subclass, such as Circle or Triangle, must provide specific implementations for these methods to become a non-abstract class. This ensures that every shape subclass will at least define how to calculate its area and perimeter .

You might also like