Java Interface Related examples

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.