<%! double area(double r) { return Math.PI*r*r; } double perimeter(double r) { } %>
时间: 2024-06-05 08:06:45 浏览: 87
As an AI language model, I do not have personal opinions or feelings, but "null" is a term used in computer programming to represent an empty or non-existent value. It is often used to indicate that a variable or data field has not been assigned a value or is uninitialized.
相关问题
用java 1定义接口IShape,该接口有2个方法,方法一(求周长):double perimeter(),方法二(求面积):double area();2自行定义圆类(Circle),实现IShape接口;3自行定义矩形类(Rectagle),实现IShape接口;然后进行测试
好的,以下是实现的Java代码:
```java
interface IShape {
double perimeter(); // 求周长
double area(); // 求面积
}
class Circle implements IShape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double perimeter() {
return 2 * Math.PI * radius;
}
@Override
public double area() {
return Math.PI * Math.pow(radius, 2);
}
}
class Rectangle implements IShape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double perimeter() {
return 2 * (length + width);
}
@Override
public double area() {
return length * width;
}
}
public class Test {
public static void main(String[] args) {
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(3, 4);
System.out.println("圆的周长:" + circle.perimeter());
System.out.println("圆的面积:" + circle.area());
System.out.println("矩形的周长:" + rectangle.perimeter());
System.out.println("矩形的面积:" + rectangle.area());
}
}
```
输出结果为:
```
圆的周长:31.41592653589793
圆的面积:78.53981633974483
矩形的周长:14.0
矩形的面积:12.0
```
编写Point、Circle、 Cylinder三个类之间具有层次结构的类。 Point类中有xy属性,定义构造函数,及计算面积的方法double area0和toString0方 法; Circle类中增加一个半径double radius属性,定义构造函数,及计算面积的方法double area0(重写父类的方法)、计算周长的方法double perimeterQ和toString0方法; Cylinder类增加一个高height属性,定义构 造函数,及计算表面积的方法double area0( 重写父类的方法)和计算体积的方法double volume0和toString方法。 要求:所有的属性都是私有的。 提示:圆继承的点作为圆心,圆柱体继承 圆作为底面和顶面。 最终定义测试类。
class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double area() {
return 0;
}
@Override
public String toString() {
return "Point(" + x + ", " + y + ")";
}
}
class Circle extends Point {
private double radius;
public Circle(double x, double y, double radius) {
super(x, y);
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
public double perimeter() {
return 2 * Math.PI * radius;
}
@Override
public String toString() {
return "Circle(" + super.toString() + ", " + radius + ")";
}
}
class Cylinder extends Circle {
private double height;
public Cylinder(double x, double y, double radius, double height) {
super(x, y, radius);
this.height = height;
}
@Override
public double area() {
return 2 * super.area() + 2 * Math.PI * radius * height;
}
public double volume() {
return super.area() * height;
}
@Override
public String toString() {
return "Cylinder(" + super.toString() + ", " + height + ")";
}
}
public class Test {
public static void main(String[] args) {
Point p = new Point(0, 0);
System.out.println(p + " Area: " + p.area());
Circle c = new Circle(0, 0, 5);
System.out.println(c + " Area: " + c.area() + " Perimeter: " + c.perimeter());
Cylinder cy = new Cylinder(0, 0, 5, 10);
System.out.println(cy + " Area: " + cy.area() + " Volume: " + cy.volume());
}
}
阅读全文
相关推荐

















