Monday, January 20, 2014

Java Program to illustrate Abstract Class

with 0 Comment
import java.text.*;
import java.io.*;
abstract class figure
{
double dim1,dim2;
figure(double d1,double d2)
{
dim1=d1;
dim2=d2;
}
abstract double area();
}
class Rectangle extends figure
{
Rectangle(double d1,double d2)
{
super(d1,d2);
}
double area()
{
return dim1*dim2;
}
}
class triangle extends figure
{
triangle(double d1,double d2)
{
super(d1,d2);
}
double area()
{
return (dim1*dim2)/2;
}
}
class absdemo
{
public static void main(String args[])throws IOException,ParseException
{
double l,b,br,h,rec,tri;
DecimalFormat df=new DecimalFormat("###.##");
DataInputStream x=new DataInputStream(System.in);
System.out.println("Rectangle & Triangle using Abstract");
System.out.println("-------------------------------");
System.out.println("Area of Rectangle");
System.out.println("---------------");
System.out.print("Enter the l value:");
l=Double.parseDouble(x.readLine());
System.out.print("Enter the b value:");
b=Double.parseDouble(x.readLine());
Rectangle r=new Rectangle(l,b);
System.out.println("Area of Triangle");
System.out.println("--------------");

System.out.print("Enter the br value:");
br=Double.parseDouble(x.readLine());
System.out.print("Enter the h value:");
h=Double.parseDouble(x.readLine());
triangle t=new triangle(br,h);
figure figref;
figref=r;
rec=figref.area();
System.out.println("Result");
System.out.println("------");
rec=df.parse(df.format(rec)).doubleValue();
System.out.println("Area of Rectangle:"+rec);
figref=t;
tri=figref.area();
tri=df.parse(df.format(tri)).doubleValue();
System.out.println("Area of Triangle:"+tri);
}
}

-------------
OUTPUT:
-------------
Rectangle & Triangle using Abstract
--------------------------------------------
Area of Rectangle
---------------------
Enter the 1 value
9
Enter the b value
5

Area of Triangle
--------------------
Enter the br value
10
Enter the h value
8

Result:
--------
Area of Rectangle : 45.0
Area of Triangle   :40.0

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive