├── README.md └── que 2 └── que2.java /README.md: -------------------------------------------------------------------------------- 1 | # Mock-test-2-java-PPT-PW-skills-Second-Question 2 | 3 | 👋 Welcome to my Java project! 4 | 5 | ## Overview 6 | This project contains three classes that demonstrate how to calculate the area of different shapes. The `Shape` class is an abstract class that defines a method for calculating the area of a shape. The `Rectangle`, `Circle`, and `Triangle` classes are concrete classes that extend the `Shape` class and implement the `calculateArea()` method. 7 | 8 | The `ShapeCalculator` class is a utility class that provides a method for printing the area of a shape. 9 | 10 | ## Usage 11 | To use this project, simply clone the repository and run the `Main` class. 12 | 13 | ## Example Output 14 | - The area is 50.0 15 | - The area is 153.93804002589985 16 | - The area is 12.0 17 | -------------------------------------------------------------------------------- /que 2/que2.java: -------------------------------------------------------------------------------- 1 | abstract class Shape { 2 | public abstract double calculateArea(); 3 | } 4 | 5 | class Rectangle extends Shape { 6 | private double length; 7 | private double width; 8 | 9 | public Rectangle(double length, double width) { 10 | this.length = length; 11 | this.width = width; 12 | } 13 | 14 | @Override 15 | public double calculateArea() { 16 | return length * width; 17 | } 18 | } 19 | 20 | class Circle extends Shape { 21 | private double radius; 22 | 23 | public Circle(double radius) { 24 | this.radius = radius; 25 | } 26 | 27 | @Override 28 | public double calculateArea() { 29 | return Math.PI * radius * radius; 30 | } 31 | } 32 | 33 | class Triangle extends Shape { 34 | private double base; 35 | private double height; 36 | 37 | public Triangle(double base, double height) { 38 | this.base = base; 39 | this.height = height; 40 | } 41 | 42 | @Override 43 | public double calculateArea() { 44 | return 0.5 * base * height; 45 | } 46 | } 47 | 48 | class ShapeCalculator { 49 | public void printArea(Shape shape) { 50 | System.out.println("The area is " + shape.calculateArea()); 51 | } 52 | } 53 | 54 | public class que2 { 55 | public static void main(String[] args) { 56 | ShapeCalculator calculator = new ShapeCalculator(); 57 | 58 | Rectangle rectangle = new Rectangle(5, 10); 59 | Circle circle = new Circle(7); 60 | Triangle triangle = new Triangle(4, 6); 61 | 62 | calculator.printArea(rectangle); 63 | calculator.printArea(circle); 64 | calculator.printArea(triangle); 65 | } 66 | } --------------------------------------------------------------------------------