用java建立一个图形接口,声明一个面积函数,圆形和矩形都实现这个接口,并得出两个图形的面积。 注:体现面向对象的特征,对数值进行判断,用异常处理。不合法的数值要出现“这个数值是非法的”提示,不在进行运算。
时间: 2023-09-03 13:26:50 浏览: 90
Java 实验 用接口实现求三角形,圆形,矩形的面积和周长
5星 · 资源好评率100%
### 回答1:
以下是一个可能的实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
interface Area {
double getArea();
}
class Circle implements Area {
private double radius;
Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
class Rectangle implements Area {
private double width;
private double height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
}
public class ShapeAreaGUI {
private JFrame frame;
private JTextField radiusField;
private JTextField widthField;
private JTextField heightField;
private JLabel circleAreaLabel;
private JLabel rectangleAreaLabel;
public ShapeAreaGUI() {
frame = new JFrame("Shape Area");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(3, 2));
radiusField = new JTextField();
widthField = new JTextField();
heightField = new JTextField();
circleAreaLabel = new JLabel("Circle area: ");
rectangleAreaLabel = new JLabel("Rectangle area: ");
panel.add(new JLabel("Circle radius: "));
panel.add(radiusField);
panel.add(new JLabel("Rectangle width: "));
panel.add(widthField);
panel.add(new JLabel("Rectangle height: "));
panel.add(heightField);
JButton calculateButton = new JButton("Calculate");
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
double radius = Double.parseDouble(radiusField.getText());
Circle circle = new Circle(radius);
circleAreaLabel.setText("Circle area: " + circle.getArea());
} catch (NumberFormatException ex) {
circleAreaLabel.setText("Circle area: Not a number");
} catch (IllegalArgumentException ex) {
circleAreaLabel.setText("Circle area: " + ex.getMessage());
}
try {
double width = Double.parseDouble(widthField.getText());
double height = Double.parseDouble(heightField.getText());
Rectangle rectangle = new Rectangle(width, height);
rectangleAreaLabel.setText("Rectangle area: " + rectangle.getArea());
} catch (NumberFormatException ex) {
rectangleAreaLabel.setText("Rectangle area: Not a number");
} catch (IllegalArgumentException ex) {
rectangleAreaLabel.setText("Rectangle area: " + ex.getMessage());
}
}
});
panel.add(calculateButton);
panel.add(new JPanel()); // empty panel for alignment
JPanel resultPanel = new JPanel(new GridLayout(2, 1));
resultPanel.add(circleAreaLabel);
resultPanel.add(rectangleAreaLabel);
frame.add(panel, BorderLayout.CENTER);
frame.add(resultPanel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ShapeAreaGUI();
}
});
}
}
```
这个程序使用了 Java 的图形界面工具包 Swing 来创建用户界面。它声明了一个 `Area` 接口,包含一个 `getArea()` 方法。`Circle` 和 `Rectangle` 类都实现了这个接口,并分别计算圆形和矩形的面积。在用户界面中,程序通过文本框和标签来获取用户输入和显示结果,使用异常处理来处理不合法的输入。注意,这个程序并没有进行数值范围的检查,可以根据需要添加相应的代码。
### 回答2:
首先,我们需要建立一个图形接口,其中声明了一个计算面积的函数。接口的代码如下:
```java
public interface Shape {
double calculateArea();
}
```
接下来,我们建立一个圆形类,实现这个接口,并在其中实现了计算面积的方法。代码如下:
```java
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
```
然后,我们建立一个矩形类,同样实现这个接口,并在其中实现了计算面积的方法。代码如下:
```java
public class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double calculateArea() {
return length * width;
}
}
```
最后,我们可以在主函数中使用这两个类,并进行面积的计算。代码如下:
```java
public class Main {
public static void main(String[] args) {
try {
double radius = -5; // 圆形半径,非法数值
double length = 10; // 矩形长度
double width = -5; // 矩形宽度,非法数值
Shape circle = new Circle(radius);
double circleArea = circle.calculateArea();
System.out.println("圆形的面积为:" + circleArea);
Shape rectangle = new Rectangle(length, width);
double rectangleArea = rectangle.calculateArea();
System.out.println("矩形的面积为:" + rectangleArea);
} catch (IllegalArgumentException e) {
System.out.println("这个数值是非法的");
}
}
}
```
当我们将非法的数值(如负数)传入圆形或矩形的构造函数时,会抛出IllegalArgumentException异常。我们通过使用try-catch来捕获这个异常,并在catch语句块中输出提示信息。这样,就能保证不合法的数值不会进行运算,并在控制台上打印出相应的提示。
### 回答3:
首先,需要创建一个图形接口,接口中声明一个求面积的函数:
```java
public interface Graphic {
double calculateArea();
}
```
然后,创建圆形类和矩形类分别实现这个接口:
```java
public class Circle implements Graphic {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * Math.pow(radius, 2);
}
}
public class Rectangle implements Graphic {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double calculateArea() {
return width * height;
}
}
```
接下来,可以通过输入半径和长宽来计算圆形和矩形的面积,同时进行异常处理判断输入是否合法:
```java
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double radius, width, height;
try {
System.out.print("请输入圆的半径:");
radius = scanner.nextDouble();
if (radius < 0) {
throw new IllegalArgumentException("这个数值是非法的");
}
Circle circle = new Circle(radius);
System.out.println("圆的面积为:" + circle.calculateArea());
System.out.print("请输入矩形的宽:");
width = scanner.nextDouble();
if (width < 0) {
throw new IllegalArgumentException("这个数值是非法的");
}
System.out.print("请输入矩形的高:");
height = scanner.nextDouble();
if (height < 0) {
throw new IllegalArgumentException("这个数值是非法的");
}
Rectangle rectangle = new Rectangle(width, height);
System.out.println("矩形的面积为:" + rectangle.calculateArea());
} catch (InputMismatchException e) {
System.out.println("输入的不是合法的数值");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
```
这样,在运行程序时,会要求输入圆的半径和矩形的宽高,然后分别计算并输出圆和矩形的面积。如果输入的数值不合法(负数等),则会抛出相应的异常并输出错误信息。
阅读全文