Java-Interface Exercise 1

Source: Internet
Author: User

1. (1) write an interface Shapepara: The method in the interface: int Getarea (): Gets the area of the graph. int getcircumference (): Get the perimeter of the graph

(2) Write a circle-like circle, which requires that the Circle class circle implement the interface Shapepara.

The class contains a member variable:

Radius:public the decorated double type radius, which represents the radius of the circle.

x : The private decorated double variable x, which represents the horizontal axis of the center.

y : Protected modified double variable y, which represents the ordinate of the center point.

The methods that are included are:

Circle (double radius) a method for constructing a parameter. Initializes the radius with the parameters in the formal parameter list, and the center point is the coordinate origin. Double Getradius (): Gets the return value of the radius to the method. void SetCenter (double x, double y): Sets the center coordinate of the class circle using the parameters in the formal parameter list. void Setradius (double radius): Sets the radius domain of the class circle using the parameters in the formal parameter list.

Interface:

 Package Com.lianxi6;  Public Interface Shapepara {            // constant    double pi=3.14;         // abstract Methods    Double Getarea ();         Double getcircumference ();        }
 PackageCom.lianxi6; Public classCircleImplementsShapepara {//Properties    Private Doublex; Private Doubley; Private Doubleradius;  Public DoubleGetX () {returnx; }     Public voidSetchenter (DoubleXDoubley) { This. x =x;  This. y =y; }     Public DoubleGetY () {returny; }     Public DoubleGetradius () {returnradius; }     Public voidSetradius (Doubleradius) {         This. Radius =radius; }        //Construction Method     PublicCircle (Doubleradius) {        Super();  This. x = 0;  This. y = 0;  This. Radius =radius; } @Override Public DoubleGetarea () {returnPi*math.pow (RADIUS, 2); } @Override Public Doublegetcircumference () {return2*pi*radius; }}
 Package Com.lianxi6;  Public class Test {    publicstaticvoid  main (string[] args) {        Circle c=  New Circle (ten);        C.setchenter (3, 4);        System.out.println (C.getarea ());            }      Public void Han (Shapepara s)    {        s.getarea ();    }}

Results:

Java-Interface Exercise 1

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.