-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab9.java
More file actions
113 lines (91 loc) · 2.61 KB
/
Copy pathLab9.java
File metadata and controls
113 lines (91 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import static java.lang.Math.sqrt;
abstract class Shape{
public abstract double area();
public abstract double perimeter();
}
class Circle extends Shape{
double radius;
public Circle(int radius){
this.radius = radius;
}
public double area(){
return radius*radius*Math.PI;
}
public double perimeter(){
return 2*Math.PI*radius;
}
}
class Rectangle extends Shape {
double length;
double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double area() {
return length * width;
}
public double perimeter() {
return 2 * (length + width);
}
}
class Triangle extends Shape {
double sideA, sideB, sideC;
public Triangle(double sideA, double sideB, double sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
public double area() {
double s = (sideA + sideB + sideC) / 2;
return sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
}
public double perimeter() {
return sideA + sideB + sideC;
}
}
class Square{
double side;
public Square(double side) {
this.side = side;
}
public double area() {
return side * side;
}
public double perimeter() {
return 4*side;
}
}
class Ellipse{
double a;
double b;
public Ellipse(double a, double b) {
this.a = a;
this.b = b;
}
public double area() {
return a * b * Math.PI;
}
public double perimeter() {
return 2 * Math.PI * sqrt((a*a+b*b)/2);
}
}
public class Lab9 {
public static void main(String[] args) {
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(20, 20);
Triangle triangle = new Triangle(20, 20, 20);
Square square = new Square(20);
Ellipse ellipse = new Ellipse(20, 20);
System.out.println("Circle Area: " + circle.area());
System.out.println("Circle Perimeter: " + circle.perimeter());
System.out.println("Rectangle Area: " + rectangle.area());
System.out.println("Rectangle Perimeter: " + rectangle.perimeter());
System.out.println("Triangle Area: " + triangle.area());
System.out.println("Triangle Perimeter: " + triangle.perimeter());
System.out.println("Square Area: " + square.area());
System.out.println("Square Perimeter: " + square.perimeter());
System.out.println("Ellipse Area: " + ellipse.area());
System.out.println("Ellipse Perimeter: " + ellipse.perimeter());
}
}