-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 4
More file actions
50 lines (50 loc) · 1.43 KB
/
Assignment 4
File metadata and controls
50 lines (50 loc) · 1.43 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
import java.util.Scanner;
abstract class Shape{
double dimension1;
double dimension2;
public void inputDimension(double dim1,double dim2){
this.dimension1=dim1;
this.dimension2=dim2;
}
abstract double computeArea();
}
class Triangle extends Shape {
double computeArea(){
return 0.5 * dimension1 * dimension2 ;
}
}
class Rectangle extends Shape{
double computeArea(){
return dimension1*dimension2;
}
}
class Square extends Shape{
double computeArea(){
return dimension1*dimension1;
}
}
public class Ass_4{
public static void main (String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter base of Triangle:");
double base=sc.nextDouble();
System.out.println("Enter height of Triangle:");
double height=sc.nextDouble();
Shape triangle=new Triangle();
triangle.inputDimension(base,height);
System.out.println("Area of Triangle:"+ triangle.computeArea());
System.out.println("Enter width of Rectangle:");
double width=sc.nextDouble();
System.out.println("Enter length of Rectangle:");
double length=sc.nextDouble();
Shape rectangle= new Rectangle();
rectangle.inputDimension(width,length);
System.out.println("Area of Rectangle:"+ rectangle.computeArea());
System.out.println("Enter side of square:");
double side=sc.nextDouble();
Shape square= new Square();
square.inputDimension(side,side);
System.out.println("Area of square:"+ square.computeArea());
sc.close();
}
}