Friday, July 3, 2015

Shape class with Rect and Circle as Subclasses and area calculation facility

class Shape{
double area;
int perimeter;

}
class Rect extends Shape{
int l;
int w;

Rect(int length , int w){
this.l = length;
this.w = w;
}
Rect(){
this.l = 0;
this.w= 0;
}

double getArea(){
return this.l * this.w;
}


}
class Circle extends Shape{
int radius;
Circle(int r){
this.radius = r;
}
Circle(){
this.radius = 0;
}
double getArea(){
return 3.1428 * this.radius* this.radius;
}
}
class TestShapes{
public static void main(String args[]){
Rect r1 = new Rect(5,5);
Circle c = new Circle(5);
double area = r1.getArea();
System.out.println("Area of R1 is: "+ area);
System.out.println("Area of c is: "+ c.getArea());
}
}






No comments:

Post a Comment