Upload files to "OOP/Java/Lab/Week8/SquareTriangle"

This commit is contained in:
Aadit Agrawal 2024-10-26 02:13:47 +05:30
parent b29550a72c
commit 2ae5d58f75
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,42 @@
interface Shape {
double calculateArea();
}
class Square implements Shape {
private double side;
public Square(double side) {
this.side = side;
}
public double calculateArea() {
return side * side;
}
}
class Triangle implements Shape {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
public double calculateArea() {
return 0.5 * base * height;
}
}
public class Main {
public static void main(String[] args) {
Shape square = new Square(5.0);
Shape triangle = new Triangle(4.0, 6.0);
System.out.println("Area of Square: " + square.calculateArea());
System.out.println("Area of Triangle: " + triangle.calculateArea());
}
}

View File

@ -0,0 +1,47 @@
abstract class Shape {
abstract double calculateArea();
}
class Square extends Shape {
private double side;
public Square(double side) {
this.side = side;
}
double calculateArea() {
return side * side;
}
}
class Triangle extends Shape {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
double calculateArea() {
return 0.5 * base * height;
}
}
public class SquareTriangle {
public static void main(String[] args) {
Square square = new Square(5);
Triangle triangle = new Triangle(4, 6);
System.out.println(
"Area of square: " + square.calculateArea() + " square units"
);
System.out.println(
"Area of triangle: " + triangle.calculateArea() + " square units"
);
}
}