Main Class:
package pkgabstract;
public class Test
{
public static void main(String[] args)
{
Circle c = new Circle(3.5,"Red",true);
Rectangle r = new Rectangle(5.2,4.2,"Black",true);
Square s = new Square();
System.out.println("Circle: ");
System.out.println("Circle Area: "+c.getArea());
System.out.println("Circle Perimeter: "+c.getPerimeter());
System.out.println("Circle Color: "+c.toString());
System.out.println("Rectangle: ");
System.out.println("Rectangle Area: "+r.getArea());
System.out.println("Rectabgle Perimeter: "+r.getPerimeter());
System.out.println("Rectangle Color: "+r.toString());
///for square
s.setLength(3.1);
s.setWidth(3.1);
s.setColor("White");
System.out.println("Square:");
System.out.println("Square Area: "+s.getArea());
System.out.println("Square Perimeter: "+s.getPerimeter());
System.out.println("Square Color: "+s.toString());
}
}
Interface Class:
package pkgabstract;
public interface NewInterface
{
public double getArea();
public double getPerimeter();
public String toString();
}
Shape Class:
package pkgabstract;
public class Shape
{
private String color;
private boolean filled;
Shape()
{
color = null;
filled = false;
}
Shape(String color,boolean filled)
{
this.color = color;
this.filled = filled;
}
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
}
public boolean isFilled()
{
return filled;
}
public void setBoolean(boolean filled)
{
this.filled = filled;
}
}
Circle Class:
package pkgabstract;
/**
*
* @author Sabit
*/
public class Circle extends Shape
{
private double radius;
Circle()
{
super();
radius = 0.0;
}
Circle(double radius)
{
super();
this.radius = radius;
}
Circle(double radius,String color,boolean filled)
{
super(color,filled);
this.radius = radius;
}
public double getRadius()
{
return radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
public double getArea()
{
return (Math.PI*Math.pow(radius, 2));
}
public double getPerimeter()
{
return (2*Math.PI*radius);
}
public String toString()
{
return super.getColor();
}
}
Rectangle Class:
package pkgabstract;
/**
*
* @author Sabit
*/
public class Rectangle extends Shape implements NewInterface
{
private double width,length;
Rectangle()
{
super();
this.width =0;
this.length=0;
}
Rectangle(double width,double length)
{
super();
this.width =width;
this.length=length;
}
Rectangle(double width,double length,String color,boolean filled)
{
super(color,filled);
this.width =width;
this.length=length;
}
public double getWidth()
{
return width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getLength()
{
return length;
}
public void setLength(double length)
{
this.length = length;
}
@Override
public double getArea()
{
return (width*length);
}
@Override
public double getPerimeter()
{
return (2*(width+length));
}
@Override
public String toString()
{
return super.getColor();
}
}
Square Class:
package pkgabstract;
/**
*
* @author Sabit
*/
public class Square extends Rectangle implements NewInterface
{
Square()
{
super();
}
Square(double side)
{
super(side,side);
}
Square(double side,String color,boolean filled)
{
super(side,side,color,filled);
}
public double getSide()
{
return super.getLength();
}
public void setSide(double side)
{
super.setLength(side);
super.setWidth(side);
}
@Override
public void setWidth(double side)
{
super.setWidth(side);
}
@Override
public void setLength(double side)
{
super.setLength(side);
}
@Override
public String toString()
{
return super.toString();
}
}
Output
Circle:
Circle Area:38.48451000647496
Circle Perimeter:21.991148575128552
Circle Color:Red
Rectangle:
Rectangle Area: 21.8400000000003
Rectabgle Perimeter:18.8
Rectangle Color:Black
Square:
Square Area:9.6100000000001
Square Perimeter:12.4
Square Color:White
Add your comment