diff --git a/OOP/Java/Lab/Week8/SquareTriangle/SqTriInt.java b/OOP/Java/Lab/Week8/SquareTriangle/SqTriInt.java new file mode 100644 index 0000000..ee71f5c --- /dev/null +++ b/OOP/Java/Lab/Week8/SquareTriangle/SqTriInt.java @@ -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()); + } +} diff --git a/OOP/Java/Lab/Week8/SquareTriangle/SquareTriangle.java b/OOP/Java/Lab/Week8/SquareTriangle/SquareTriangle.java new file mode 100644 index 0000000..64ab467 --- /dev/null +++ b/OOP/Java/Lab/Week8/SquareTriangle/SquareTriangle.java @@ -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" + ); + } +}