Shapes - Class diagram
Friday, November 10, 2023
⁃
Difficulty:
Intermediate
Objective
Create a Java program that models different geometric shapes from the following UML class diagram.
The diagram represents public, private, and protected attributes, as well as class dependency and inheritance.
Start by designing a base class called GeometricShape
that contains properties common to all shapes, such as color. Implement a constructor that allows initializing these properties.
Next, create specific classes that inherit from GeometricShape
to represent different shapes, such as circles, squares, and triangles. In these classes, implement methods that calculate the area and perimeter of each shape.
Conclude the program by instantiating objects of the different shapes and displaying relevant information, such as area and perimeter, in the console.
Additional Instructions:
- Use the main class of the program to instantiate objects of the created geometric shapes.
- Make sure to print detailed information for each shape in the console, including its area and perimeter.
- Observe how inheritance and encapsulation enable a more organized and reusable code implementation.
Input
No input required
Output
Circle Information:
Color: Red
Area: 78.53981633974483
Perimeter: 31.41592653589793
Square Information:
Color: Blue
Area: 16.0
Perimeter: 16.0
Triangle Information:
Color: Green
Area: 24.0
Perimeter: 18.0
Solution
import java.util.Scanner;
// Base class GeometricShape
class GeometricShape {
// Properties common to all shapes
private String color;
// Constructor to initialize properties
public GeometricShape(String color) {
this.color = color;
}
// Method to get the color
public String getColor() {
return color;
}
// Abstract method to calculate the area (to be implemented by subclasses)
public double calculateArea() {
return 0;
}
// Abstract method to calculate the perimeter (to be implemented by subclasses)
public double calculatePerimeter() {
return 0;
}
}
// Class to represent a circle
class Circle extends GeometricShape {
private double radius;
// Constructor to initialize properties
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
// Implementation of the method to calculate the area
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
// Implementation of the method to calculate the perimeter
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
// Class to represent a square
class Square extends GeometricShape {
private double side;
// Constructor to initialize properties
public Square(String color, double side) {
super(color);
this.side = side;
}
// Implementation of the method to calculate the area
@Override
public double calculateArea() {
return side * side;
}
// Implementation of the method to calculate the perimeter
@Override
public double calculatePerimeter() {
return 4 * side;
}
}
// Class to represent a triangle
class Triangle extends GeometricShape {
private double base;
private double height;
// Constructor to initialize properties
public Triangle(String color, double base, double height) {
super(color);
this.base = base;
this.height = height;
}
// Implementation of the method to calculate the area
@Override
public double calculateArea() {
return 0.5 * base * height;
}
// Implementation of the method to calculate the perimeter (approximate)
@Override
public double calculatePerimeter() {
return 3 * base; // Approximation for an equilateral triangle
}
}
// Main class
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create objects of different shapes
Circle circle = new Circle("Red", 5);
Square square = new Square("Blue", 4);
Triangle triangle = new Triangle("Green", 6, 8);
// Display information of the shapes
System.out.println("Circle Information:");
displayShapeInformation(circle);
System.out.println("\nSquare Information:");
displayShapeInformation(square);
System.out.println("\nTriangle Information:");
displayShapeInformation(triangle);
scanner.close();
}
// Method to display common information of the shapes
private static void displayShapeInformation(GeometricShape shape) {
System.out.println("Color: " + shape.getColor());
System.out.println("Area: " + shape.calculateArea());
System.out.println("Perimeter: " + shape.calculatePerimeter());
}
}
import java.util.Scanner;
// Base class GeometricShape
class GeometricShape {
// Properties common to all shapes
private String color;
// Constructor to initialize properties
public GeometricShape(String color) {
this.color = color;
}
// Method to get the color
public String getColor() {
return color;
}
// Abstract method to calculate the area (to be implemented by subclasses)
public double calculateArea() {
return 0;
}
// Abstract method to calculate the perimeter (to be implemented by subclasses)
public double calculatePerimeter() {
return 0;
}
}
// Class to represent a circle
class Circle extends GeometricShape {
private double radius;
// Constructor to initialize properties
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
// Implementation of the method to calculate the area
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
// Implementation of the method to calculate the perimeter
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
// Class to represent a square
class Square extends GeometricShape {
private double side;
// Constructor to initialize properties
public Square(String color, double side) {
super(color);
this.side = side;
}
// Implementation of the method to calculate the area
@Override
public double calculateArea() {
return side * side;
}
// Implementation of the method to calculate the perimeter
@Override
public double calculatePerimeter() {
return 4 * side;
}
}
// Class to represent a triangle
class Triangle extends GeometricShape {
private double base;
private double height;
// Constructor to initialize properties
public Triangle(String color, double base, double height) {
super(color);
this.base = base;
this.height = height;
}
// Implementation of the method to calculate the area
@Override
public double calculateArea() {
return 0.5 * base * height;
}
// Implementation of the method to calculate the perimeter (approximate)
@Override
public double calculatePerimeter() {
return 3 * base; // Approximation for an equilateral triangle
}
}
// Main class
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create objects of different shapes
Circle circle = new Circle("Red", 5);
Square square = new Square("Blue", 4);
Triangle triangle = new Triangle("Green", 6, 8);
// Display information of the shapes
System.out.println("Circle Information:");
displayShapeInformation(circle);
System.out.println("\nSquare Information:");
displayShapeInformation(square);
System.out.println("\nTriangle Information:");
displayShapeInformation(triangle);
scanner.close();
}
// Method to display common information of the shapes
private static void displayShapeInformation(GeometricShape shape) {
System.out.println("Color: " + shape.getColor());
System.out.println("Area: " + shape.calculateArea());
System.out.println("Perimeter: " + shape.calculatePerimeter());
}
}
Click here to view the exercise solution
Share it
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
Copied
The code has been successfully copied to the clipboard.