Search This Blog

Loading...
If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Wednesday, 19 October 2011

Java program to find area of a square and rectangle using abstract class (by noor diana fauzi)

import java.util.*;
class area
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
double base,height;
System.out.print ("\n\t Enter base of the Rectangle = ");
base=in.nextDouble();
System.out.print ("\n\t Enter height of the Rectangle = ");
height=in.nextDouble();

double side1,side2;
System.out.print ("\n\t Enter length of side1 of the Square = ");
side1=in.nextDouble();
System.out.print ("\n\t Enter length of side2 of the Square = ");
side2=in.nextDouble();

Rectangle ob1 = new Rectangle (base,height);
Square ob2 = new Square (side1,side2);
ob1.area();
ob2.area();
}
}


abstract class Shape
{
abstract void area();
}

class Rectangle extends Shape
{
double base,height,a;
Rectangle(double b, double l)
{
base=b;
height=l;
}
void area()
{
a=base*height;
System.out.println ("\n\t Area of Rectangle = "+a);
}
}

class Square extends Shape
{
double side1,side2,a;
Square (double s1, double s2)
{
side1=s1;
side2=s2;
}
void area()
{
a=side1*side2;
System.out.println ("\n\t Area of Square = "+a);
}
}

No comments:

Post a Comment