Java Interface related exercises
Interface Graphics
{//interface only with abstract method
public abstract double area ();
}
Set up a plane class
Class PLANEGRAPHICS1
{
Private String shape; Shape
Construction method, with parameters
Public PlaneGraphics1 (String shape)
{
This.shape=shape;
}
No parameters
Public PLANEGRAPHICS1 ()
{
This ("Unknown figure 1");
}
Print
public void print ()
{
}
}
Rectangle, inheriting the interface necessary to implement an abstract method of the interface, otherwise it will be an error.
Class Rectangle extends PLANEGRAPHICS1 implements Graphics
{
protected double length;
protected double width;
Construction method
Public Rectangle (double length,double width)
{
This.length=length;
This.width=width;
}
Square Construction Method
Public Rectangle (double width)
{
This.length=width;
This.width=width;
}
Non-parametric construction method
Public Rectangle ()
{
}
Public double Area ()
{
return width*length;
}
public void print ()
{
System.out.println ("Rectangular area:" +this.area ());
}
}
Elliptic
Class Eclipse extends PLANEGRAPHICS1 implements Graphics
{
protected double radius_a;
protected double radius_b;
Ellipse construction Method
Public Eclipse (double radius_a,double radius_b)
{
Super ("Ellipse");
This.radius_a=radius_a;
This.radius_b=radius_b;
}
Round
Public Eclipse (double radius_a)
{
Super ("Circle");
This.radius_a=radius_a;
This.radius_b=radius_a;
}
Public Eclipse ()
{
}
Public double Area ()
{
return math.pi*radius_a*radius_b;
}
public void print ()
{
System.out.println ("The area of the circle is" +this.area ());
}
}
Class Triangle extends PLANEGRAPHICS1 implements Graphics
{
private double bottom;
private double height;
Public Triangle (double bottom, double height)
{
This.bottom=bottom;
This.height=height;
}
Public double Area ()
{
return BOTTOM*HEIGHT/2;
}
public void print ()
{
System.out.println ("Triangle area:" +this.area ());
}
}
public class Planegraphics_ex1 {
public static void Main (String[]args)
{
PLANEGRAPHICS1 g=new Rectangle (10.0,20.0);
G.print ();
G=new Eclipse (10.0,12.0);
G.print ();
G=new Triangle (10.0,6.0);
G.print ();
}
}
Java Interface Related examples