(1)定义一个Shape接口,该接口中有一个常量PI,两个方法:getPerimeter()和getArea(),方法均无参数,返回值类型为double型;
package cn.hygzs.demo6;
/**
*
* @author 湖云工作室
*
*/
public interface Shape {
final double PI = 3.14159267;
double getPerimeter();
double getArea();
}
(2)定义一个圆类Circle,满足以下条件:
package cn.hygzs.demo6;
/**
*
* @author 湖云工作室
*
*/
public class Circle implements Shape {
double r ;
public double getR() {
return r;
}
public Circle(double r) {
this();
this.r = r;
}
public Circle() {
super();
System.out.println("--------接口测试程序------");
}
@Override
public double getPerimeter() {
double p = 2*r*PI;
return p;
}
@Override
public double getArea() {
double s = r*r*PI;
return s;
}
}
(3)编写一个测试类,类名为TestCircle,让用户输入圆的半径,利用Circle类计算该圆的周长和面积,
package cn.hygzs.demo6;
import java.util.Scanner;
/**
*
* @author 湖云工作室
*
*/
public class TestCircle {
public static void main(String[] args) {
System.out.println("请输入圆的半径:");
Scanner input = new Scanner(System.in);
double r = input.nextDouble();
Circle c = new Circle(r);
System.out.println("半径为"+r+"的圆的周长为"+c.getPerimeter());
System.out.println("半径为"+r+"的圆的面积为"+c.getArea());
}
}
Shapes.java
package cn.hygzs.demo6;
/**
*
* @author 湖云工作室
*
*/
public interface Shapes {
void getInfo();
void getInfo(Line l);
double size();
double size(Line l);
}
Line.java
package cn.hygzs.demo6;
/**
*
* @author 湖云工作室
* 直线
*/
public class Line implements Shapes{
double x;
double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Line(double x, double y) {
super();
this.x = x;
this.y = y;
}
@Override
public double size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public double size(Line l) {
double sum =Math.sqrt((x-l.x) * (x-l.x) + (y-l.y) * (y-l.y) );
return sum;
}
@Override
public void getInfo() {
// TODO Auto-generated method stub
}
@Override
public void getInfo(Line l) {
System.out.println("点("+x+","+y+ ")到点("+l.getX()+","+l.getY()+")长度为:"+size(l));
}
}
Circles.java
package cn.hygzs.demo6;
/**
*
* @author 湖云工作室
* 圆形
*/
public class Circles implements Shapes {
private double r;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public Circles(double r) {
super();
this.r = r;
}
@Override
public double size() {
return 3.14*getR()*getR();
}
@Override
public void getInfo() {
System.out.println("半径为:"+getR()+"的圆面积为:"+size());
}
@Override
public double size(Line l) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void getInfo(Line l) {
// TODO Auto-generated method stub
}
}
Rectangles.java
package cn.hygzs.demo6;
/**
*
* @author 湖云工作室
* 三角形
*/
public class Rectangles implements Shapes {
double sideA;
double sideB;
double sideC;
public Rectangles(double sideA, double sideB, double sideC) {
super();
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
@Override
public double size() {
double p=(sideA+sideB+sideC)/2.0;
double area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)) ;
return area;
}
@Override
public void getInfo() {
System.out.println("边长为:"+sideA+","+sideB+","+sideC+"的三角形面积为:"+size());
}
@Override
public double size(Line l) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void getInfo(Line l) {
// TODO Auto-generated method stub
}
}
Triangles.java
package cn.hygzs.demo6;
/**
*
* @author 湖云工作室
* 矩形
*/
public class Triangles implements Shapes {
private double a;
private double b;
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public void setB(double b) {
this.b = b;
}
public double getB() {
return b;
}
public Triangles(double a, double b) {
super();
this.a = a;
this.b = b;
}
@Override
public double size() {
return getA()*getB();
}
@Override
public void getInfo() {
System.out.println("长为:"+getA()+"高为:"+getB()+"的矩形面积为:"+size());
}
@Override
public double size(Line l) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void getInfo(Line l) {
// TODO Auto-generated method stub
}
}
Test.java
package cn.hygzs.demo6;
/**
*
* @author 湖云工作室
*
*/
public class Test {
public static void main(String[] args) {
//定义对象数组
Shapes s[] = new Shapes[4];
s[0] = new Circles(47.0);
s[1] = new Triangles(3,4);
s[2] = new Rectangles(3,4,5);
s[3] = new Line(22.0,56.0);
Line l = new Line(41.0,57.0);
//引用方法
s[3].getInfo(l);
s[0].getInfo();
s[2].getInfo();
s[1].getInfo();
}
}