在Main类中定义方法showArea(),其含义见输入输出示例
时间: 2023-06-12 22:02:37 浏览: 49
好的,以下是定义的方法showArea():
```java
public static void showArea(double r) {
double area = Math.PI * r * r;
System.out.println("半径为" + r + "的圆的面积为:" + area);
}
```
该方法接收一个double类型的参数r,计算出以r为半径的圆的面积,并输出结果。具体的输入输出示例见下:
示例输入:
```
5.0
```
示例输出:
```
半径为5.0的圆的面积为:78.53981633974483
```
相关问题
2.定义抽象类Shape,抽象方法为showArea(),求出面积并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。程序
```java
// Shape.java
public abstract class Shape {
public abstract void showArea();
}
// Rectangle.java
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public void showArea() {
double area = length * width;
System.out.println("The area of the rectangle is " + area);
}
}
// Square.java
public class Square extends Shape {
private double side;
public Square(double side) {
this.side = side;
}
@Override
public void showArea() {
double area = side * side;
System.out.println("The area of the square is " + area);
}
}
// Circle.java
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public void showArea() {
double area = Math.PI * radius * radius;
System.out.println("The area of the circle is " + area);
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Shape rect = new Rectangle(10, 20);
Shape square = new Square(5);
Shape circle = new Circle(3);
rect.showArea();
square.showArea();
circle.showArea();
}
}
```
输出结果:
```
The area of the rectangle is 200.0
The area of the square is 25.0
The area of the circle is 28.274333882308138
```
编写一个Java程序,在程序中定义一个抽象类Shape,包含两个抽象方法——area 和 showarea,再定义两个Shape类的子类,在子类中实现父类的抽象方法。
以下是一个实现上述要求的Java程序:
```java
// 定义抽象类Shape
abstract class Shape {
// 定义计算面积的抽象方法
public abstract double area();
// 定义显示面积的抽象方法
public abstract void showarea();
}
// 定义Shape类的子类:Rectangle
class Rectangle extends Shape {
private double length; // 长
private double width; // 宽
// 构造函数
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// 实现父类的抽象方法:计算面积
public double area() {
return length * width;
}
// 实现父类的抽象方法:显示面积
public void showarea() {
System.out.println("矩形的面积为:" + area());
}
}
// 定义Shape类的子类:Circle
class Circle extends Shape {
private double radius; // 半径
// 构造函数
public Circle(double radius) {
this.radius = radius;
}
// 实现父类的抽象方法:计算面积
public double area() {
return Math.PI * radius * radius;
}
// 实现父类的抽象方法:显示面积
public void showarea() {
System.out.println("圆的面积为:" + area());
}
}
// 测试类
public class TestShape {
public static void main(String[] args) {
// 创建一个矩形对象
Rectangle rect = new Rectangle(5.0, 3.0);
rect.showarea(); // 调用显示面积的方法
// 创建一个圆对象
Circle circle = new Circle(2.0);
circle.showarea(); // 调用显示面积的方法
}
}
```
运行结果:
```
矩形的面积为:15.0
圆的面积为:12.566370614359172
```
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)