写一个Java程序来求正方形、长方形和圆形的面积,用三种不同的方法来求面积。
时间: 2024-05-15 09:18:32 浏览: 152
求面积的程序
好的,我可以回答您的问题。以下是Java程序来求正方形、长方形和圆形的面积,使用三种不同的方法:
方法1:使用公式直接求解
```java
import java.util.Scanner;
public class Area {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入图形的类型:1.正方形 2.长方形 3.圆形");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("请输入正方形的边长:");
double side = sc.nextDouble();
double area = side * side;
System.out.println("正方形的面积为:" + area);
break;
case 2:
System.out.println("请输入长方形的长:");
double length = sc.nextDouble();
System.out.println("请输入长方形的宽:");
double width = sc.nextDouble();
area = length * width;
System.out.println("长方形的面积为:" + area);
break;
case 3:
System.out.println("请输入圆形的半径:");
double radius = sc.nextDouble();
area = Math.PI * radius * radius;
System.out.println("圆形的面积为:" + area);
break;
default:
System.out.println("输入有误,请重新输入!");
}
sc.close();
}
}
```
方法2:使用多态性和继承
```java
abstract class Shape {
public abstract double getArea();
}
class Square extends Shape {
private final double side;
public Square(double side) {
this.side = side;
}
public double getArea() {
return side * side;
}
}
class Rectangle extends Shape {
private final double length;
private final double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
}
class Circle extends Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
import java.util.Scanner;
public class Area {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入图形的类型:1.正方形 2.长方形 3.圆形");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("请输入正方形的边长:");
double side = sc.nextDouble();
Shape square = new Square(side);
System.out.println("正方形的面积为:" + square.getArea());
break;
case 2:
System.out.println("请输入长方形的长:");
double length = sc.nextDouble();
System.out.println("请输入长方形的宽:");
double width = sc.nextDouble();
Shape rectangle = new Rectangle(length, width);
System.out.println("长方形的面积为:" + rectangle.getArea());
break;
case 3:
System.out.println("请输入圆形的半径:");
double radius = sc.nextDouble();
Shape circle = new Circle(radius);
System.out.println("圆形的面积为:" + circle.getArea());
break;
default:
System.out.println("输入有误,请重新输入!");
}
sc.close();
}
}
```
方法3:使用反射和注解
```java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.Scanner;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface AreaMethod {
String value();
}
class AreaCalculator {
@AreaMethod("正方形的面积为")
public static double squareArea(double side) {
return side * side;
}
@AreaMethod("长方形的面积为")
public static double rectangleArea(double length, double width) {
return length * width;
}
@AreaMethod("圆形的面积为")
public static double circleArea(double radius) {
return Math.PI * radius * radius;
}
}
public class Area {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("请输入图形的类型:1.正方形 2.长方形 3.圆形");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("请输入正方形的边长:");
double side = sc.nextDouble();
Method squareMethod = AreaCalculator.class.getMethod("squareArea", double.class);
double squareArea = (double) squareMethod.invoke(null, side);
AreaMethod squareAnnotation = squareMethod.getAnnotation(AreaMethod.class);
System.out.println(squareAnnotation.value() + " " + squareArea);
break;
case 2:
System.out.println("请输入长方形的长:");
double length = sc.nextDouble();
System.out.println("请输入长方形的宽:");
double width = sc.nextDouble();
Method rectangleMethod = AreaCalculator.class.getMethod("rectangleArea", double.class, double.class);
double rectangleArea = (double) rectangleMethod.invoke(null, length, width);
AreaMethod rectangleAnnotation = rectangleMethod.getAnnotation(AreaMethod.class);
System.out.println(rectangleAnnotation.value() + " " + rectangleArea);
break;
case 3:
System.out.println("请输入圆形的半径:");
double radius = sc.nextDouble();
Method circleMethod = AreaCalculator.class.getMethod("circleArea", double.class);
double circleArea = (double) circleMethod.invoke(null, radius);
AreaMethod circleAnnotation = circleMethod.getAnnotation(AreaMethod.class);
System.out.println(circleAnnotation.value() + " " + circleArea);
break;
default:
System.out.println("输入有误,请重新输入!");
}
sc.close();
}
}
```
以上三种方法都可以用来求解正方形、长方形和圆形的面积,具体使用哪种方法取决于场合和需求。
阅读全文