Saturday, July 11, 2015

Shape class with Rect & Cirlce as subclasses

class Shape{
double area;
}
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);
System.out.println("Area of r1 is: "+ r1.getArea());
System.out.println("Area of c is: "+ c.getArea());

}
}

No comments:

Post a Comment