2.定义接口Shape,包含两个抽象方法getArea()和getCircum(),
分别实现获取图形的面积和周长,抽象方法print()用于输出面积和周长。基于接口Shape定义矩形类Ranctangle和圆类Circle。
圆类包含数据成员有半径,矩形类包含数据成员的有长、宽。
成员通过构造方法进行初始化,每个类都要重写抽象方法getArea()和 getCircum():计算并获取面积和周长,并重写抽象方法print()。
测试类中实例化矩形类Ranctangle和圆类Circle,计算面积和周长,并输出。
接口Shape:
public interface Shape {
double getArea();
double getCircum();
void print();
}
圆类Circle:
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getCircum() {
return 2 * Math.PI * radius;
}
public void print() {
System.out.println("The area of the circle is: " + getArea());
System.out.println("The circum of the circle is: " + getCircum());
}
}
矩形类Ranctangle:
public class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
public double getCircum() {
return 2 * (length + width);
}
public void print() {
System.out.println("The area of the rectangle is: " + getArea());
System.out.println("The circum of the rectangle is: " + getCircum());
}
}
测试类:
public class Test {
public static void main(String[] args) {
Shape circle = new Circle(2.0);
circle.print();
Shape rectangle = new Rectangle(2.0, 3.0);
rectangle.print();
}
}
输出结果:
The area of the circle is: 12.566370614359172
The circum of the circle is: 12.566370614359172
The area of the rectangle is: 6.0
The circum of the rectangle is: 10.0
代码如下,如有帮助望采纳
public interface Shape {
//定义getArea方法
double getArea();
//定义getCircum方法
double getCircum();
//输出周长和面积
void print();
}
import java.math.BigDecimal;
//矩形类
public class Ranctangle implements Shape{
//长
private String length;
//宽
private String width;
//无参构造方法
public Ranctangle() {
}
//有参构造方法
public Ranctangle(String length, String width) {
this.length = length;
this.width = width;
}
@Override
public double getArea() {
//使用bigdecimal防止计算double类型的浮点数精度丢失
BigDecimal length = new BigDecimal(this.length);
BigDecimal width = new BigDecimal(this.width);
return length.multiply(width).doubleValue();
}
@Override
public double getCircum() {
//使用bigdecimal防止计算double类型的浮点数精度丢失
BigDecimal length = new BigDecimal(this.length);
BigDecimal width = new BigDecimal(this.width);
return length.add(width).doubleValue();
}
@Override
public void print() {
System.out.println("矩形周长为:" + getCircum());
System.out.println("矩形面积为:" + getArea());
}
}
import java.math.BigDecimal;
//圆类
public class Circle implements Shape{
//定义成员变量半径radius
private String radius;
//构造方法
public Circle() {
}
//有参构造方法
public Circle(String radius) {
this.radius = radius;
}
@Override
public double getArea() {
BigDecimal radius = new BigDecimal(this.radius);
BigDecimal a = new BigDecimal("3.14");
return a.multiply(radius).multiply(radius).doubleValue();
}
@Override
public double getCircum() {
BigDecimal radius = new BigDecimal(this.radius);
BigDecimal a = new BigDecimal("3.14");
BigDecimal b = new BigDecimal("2");
return a.multiply(radius).multiply(b).doubleValue();
}
@Override
public void print() {
System.out.println("圆形周长为:" + getCircum());
System.out.println("圆形面积为:" + getArea());
}
}
//测试类
public class Test {
public static void main(String[] args) {
//定义矩形和圆形对象
Shape ranctangle = new Ranctangle("2", "3.1");
Shape circle = new Circle("2");
ranctangle.print();
circle.print();
}
}
输出结果如下