This code demonstrates the use of **inheritance**, **abstraction**, and **interfaces**...

April 4, 2025 at 12:49 AM

// Classe abstrata Figura abstract class Figura { public abstract void exibirDetalhes(); } // Interface FiguraPlana interface FiguraPlana { double calcularArea(); double calcularPerimetro(); } // Interface FiguraEspacial interface FiguraEspacial { double calcularVolume(); } // Classe Circulo class Circulo extends Figura implements FiguraPlana { private double raio; public Circulo(double raio) { this.raio = raio; } @Override public double calcularArea() { return Math.PI * Math.pow(raio, 2); } @Override public double calcularPerimetro() { return 2 * Math.PI * raio; } @Override public void exibirDetalhes() { System.out.println("Círculo:"); System.out.println("Raio: " + raio); System.out.println("Área: " + calcularArea()); System.out.println("Perímetro: " + calcularPerimetro()); } } // Classe Retangulo class Retangulo extends Figura implements FiguraPlana { private double largura; private double altura; public Retangulo(double largura, double altura) { this.largura = largura; this.altura = altura; } @Override public double calcularArea() { return largura * altura; } @Override public double calcularPerimetro() { return 2 * (largura + altura); } @Override public void exibirDetalhes() { System.out.println("Retângulo:"); System.out.println("Largura: " + largura); System.out.println("Altura: " + altura); System.out.println("Área: " + calcularArea()); System.out.println("Perímetro: " + calcularPerimetro()); } } // Classe Triangulo class Triangulo extends Figura implements FiguraPlana { private double lado1; private double lado2; private double lado3; public Triangulo(double lado1, double lado2, double lado3) { this.lado1 = lado1; this.lado2 = lado2; this.lado3 = lado3; } @Override public double calcularArea() { double s = (lado1 + lado2 + lado3) / 2; // semi-perímetro return Math.sqrt(s * (s - lado1) * (s - lado2) * (s - lado3)); // Fórmula de Heron } @Override public double calcularPerimetro() { return lado1 + lado2 + lado3; } @Override public void exibirDetalhes() { System.out.println("Triângulo:"); System.out.println("Lado 1: " + lado1); System.out.println("Lado 2: " + lado2); System.out.println("Lado 3: " + lado3); System.out.println("Área: " + calcularArea()); System.out.println("Perímetro: " + calcularPerimetro()); } } // Classe Esfera class Esfera extends Figura implements FiguraEspacial { private double raio; public Esfera(double raio) { this.raio = raio; } @Override public double calcularVolume() { return (4.0 / 3) * Math.PI * Math.pow(raio, 3); } @Override public void exibirDetalhes() { System.out.println("Esfera:"); System.out.println("Raio: " + raio); System.out.println("Volume: " + calcularVolume()); } } // Classe Cubo class Cubo extends Figura implements FiguraEspacial { private double lado; public Cubo(double lado) { this.lado = lado; } @Override public double calcularVolume() { return Math.pow(lado, 3); } @Override public void exibirDetalhes() { System.out.println("Cubo:"); System.out.println("Lado: " + lado); System.out.println("Volume: " + calcularVolume()); } } // Classe Cone class Cone extends Figura implements FiguraEspacial { private double raio; private double altura; public Cone(double raio, double altura) { this.raio = raio; this.altura = altura; } @Override public double calcularVolume() { return (1.0 / 3) * Math.PI * Math.pow(raio, 2) * altura; } @Override public void exibirDetalhes

This code demonstrates the use of inheritance, abstraction, and interfaces in Java to model various shapes (both two-dimensional and three-dimensional). Here's what each part of the code does:

1. Abstract Class Figura

  • Serves as the base class for all shapes.
  • Contains an abstract method exibirDetalhes() which must be implemented by all subclasses. This is used to display details of the specific shape such as dimensions and calculated properties.

2. Interface FiguraPlana

  • Represents 2D shapes.
  • Defines two methods:
    • calcularArea(): Calculates the area of the shape.
    • calcularPerimetro(): Calculates the perimeter of the shape.
  • Shapes that implement this interface must define these methods.

3. Interface FiguraEspacial

  • Represents 3D shapes.
  • Defines a single method:
    • calcularVolume(): Calculates the volume of the shape.
  • Shapes that implement this interface must define this method.

4. Class Descriptions

(A) 2D Shape Implementations (FiguraPlana)

  • Circulo:

    • Represents a circle, defined by its radius raio.
    • Implements calcularArea() (computes area using π * raio²).
    • Implements calcularPerimetro() (computes perimeter as 2 * π * raio).
    • Implements exibirDetalhes() to print details about the circle.
  • Retangulo:

    • Represents a rectangle, defined by its width largura and height altura.
    • Implements calcularArea() (calculates area as largura * altura).
    • Implements calcularPerimetro() (calculates perimeter as 2 * (largura + altura)).
    • Implements exibirDetalhes() to print details about the rectangle.
  • Triangulo:

    • Represents a triangle, defined by the lengths of its three sides: lado1, lado2, and lado3.
    • Implements calcularArea() using Heron's formula.
    • Implements calcularPerimetro() (sums up all three side lengths).
    • Implements exibirDetalhes() to print details about the triangle.

(B) 3D Shape Implementations (FiguraEspacial)

  • Esfera:

    • Represents a sphere, defined by its radius raio.
    • Implements calcularVolume() (computes volume as (4 / 3) * π * raio³).
    • Implements exibirDetalhes() to print details about the sphere.
  • Cubo:

    • Represents a cube, defined by the length of its side lado.
    • Implements calcularVolume() (computes volume as lado³).
    • Implements exibirDetalhes() to print details about the cube.
  • Cone (incomplete in the provided code):

    • Represents a cone, defined by its radius raio and height altura.
    • Implements calcularVolume() (computes volume as (1 / 3) * π * raio² * altura).
    • Implements exibirDetalhes() (though this method implementation is incomplete here).

5. How It All Works Together

  • By using the Figura abstract class as the base and implementing interfaces (FiguraPlana and FiguraEspacial), the code ensures that specific behaviors for each shape are properly defined.
  • Each specific class provides its own implementation of:
    • Formulas for area, perimeter (for 2D shapes).
    • Formulas for volume (for 3D shapes).
    • The exibirDetalhes() method to display the unique features and calculations of the shape.

This systematic approach illustrates polymorphism and code reusability, where common functionality is defined in interfaces or abstract classes, and specific behavior is implemented in derived classes.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node