WRITE A PROGRAM TO CALCULATE THE AREA OF TRIANGLE,CIRCLE,SQUARE,AND RECTANGLE. HOWEVER,YOU NEED TO ENSURE THAT THE PRECEDING SET OF FUNCTIONALITIES MUST BE ARCHIEVED BY IMPLEMENTING ABSTRACTION.
ANSWER
abstract class Area {
double dim1, dim2, result;
abstract void calArea();
double disArea() {
return result;
}
}
class Circle extends Area {
double pie = 3.14;
public Circle(double rad) {
dim1 = rad;
}
void calArea() {
result= pie*dim1*dim1;
}
}
class Rectangle extends Area {
public Rectangle(double ln, double br) {
dim1 = ln;
dim2 = br;
}
void calArea() {
result = dim1 * dim2;
}
}
claSS Square extand Area {
public Square(double side) {
dim1 = side;
}
void calArea() {
result =dim1* dim1;
}
}
class Triangle extends Area {
public Triangle (double bas, double high) {
dim1 = bas;
dim2 = high;
}
void calArea() {
result = (dim1 * dim2 ) /2;
}
}
public class Area calculator {
public static void main (String[] args) {
Square sobj = new Square (44.5);
sobj.calArea();
System.out.println("The area of square is " + sobj.disArea());
Rectangle robj = new Rectangle (23.4, 12);
robj.calArea();
System.out.println("The area of rectangle is " + robj.disArea());
Circle cobj = new Circle (5.7);
cobj.calArea();
System.out.println("The area of circle is " + cobj.disArea());
Triangle tobj = new Triangle (20, 20);
tobj.calArea();
System.out.println("The area of triangle is " + tobj.disArea());
}
}
ANSWER
abstract class Area {
double dim1, dim2, result;
abstract void calArea();
double disArea() {
return result;
}
}
class Circle extends Area {
double pie = 3.14;
public Circle(double rad) {
dim1 = rad;
}
void calArea() {
result= pie*dim1*dim1;
}
}
class Rectangle extends Area {
public Rectangle(double ln, double br) {
dim1 = ln;
dim2 = br;
}
void calArea() {
result = dim1 * dim2;
}
}
claSS Square extand Area {
public Square(double side) {
dim1 = side;
}
void calArea() {
result =dim1* dim1;
}
}
class Triangle extends Area {
public Triangle (double bas, double high) {
dim1 = bas;
dim2 = high;
}
void calArea() {
result = (dim1 * dim2 ) /2;
}
}
public class Area calculator {
public static void main (String[] args) {
Square sobj = new Square (44.5);
sobj.calArea();
System.out.println("The area of square is " + sobj.disArea());
Rectangle robj = new Rectangle (23.4, 12);
robj.calArea();
System.out.println("The area of rectangle is " + robj.disArea());
Circle cobj = new Circle (5.7);
cobj.calArea();
System.out.println("The area of circle is " + cobj.disArea());
Triangle tobj = new Triangle (20, 20);
tobj.calArea();
System.out.println("The area of triangle is " + tobj.disArea());
}
}
No comments:
Post a Comment