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