Upload files to "OOP/Java/Lab/Week8/SquareTriangle"
This commit is contained in:
parent
b29550a72c
commit
2ae5d58f75
42
OOP/Java/Lab/Week8/SquareTriangle/SqTriInt.java
Normal file
42
OOP/Java/Lab/Week8/SquareTriangle/SqTriInt.java
Normal 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());
|
||||
}
|
||||
}
|
47
OOP/Java/Lab/Week8/SquareTriangle/SquareTriangle.java
Normal file
47
OOP/Java/Lab/Week8/SquareTriangle/SquareTriangle.java
Normal 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"
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user