编写复数运算的javaApplication程序: (1)设计复数类Complex,具有实部、虚部两个属性,能完成复数的加、减、乘三种运算。 (2)使用Application创建一图形用户界面程序,界面如下,程序运行时,用户可在“复数1”后面的对应文本框中输入第一个复数的实部和虚部,在“复 数2”后面的对应文本框中输入第二个复数的实部和虚部,在“运算符”后面的文本框中输入“+”或“-”或“*”运算符号,当用户按下“=”按钮后,程序调用复数类Complex的对应方法,将运算结果(计算得到的复数的实部和虚部)显示在其后排列的两个文本框中。
时间: 2024-03-22 07:37:40 浏览: 16
以下是复数类Complex的代码:
```java
public class Complex {
private double real;
private double imag;
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public Complex add(Complex other) {
double realResult = this.real + other.real;
double imagResult = this.imag + other.imag;
return new Complex(realResult, imagResult);
}
public Complex subtract(Complex other) {
double realResult = this.real - other.real;
double imagResult = this.imag - other.imag;
return new Complex(realResult, imagResult);
}
public Complex multiply(Complex other) {
double realResult = this.real * other.real - this.imag * other.imag;
double imagResult = this.real * other.imag + this.imag * other.real;
return new Complex(realResult, imagResult);
}
public String toString() {
String sign = this.imag >= 0 ? "+" : "-";
return this.real + sign + Math.abs(this.imag) + "i";
}
}
```
以下是图形用户界面程序的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ComplexCalculator extends JFrame {
private JTextField realField1;
private JTextField imagField1;
private JTextField realField2;
private JTextField imagField2;
private JTextField operatorField;
private JTextField realResultField;
private JTextField imagResultField;
public ComplexCalculator() {
// Create labels
JLabel complex1Label = new JLabel("复数1:");
JLabel complex2Label = new JLabel("复数2:");
JLabel operatorLabel = new JLabel("运算符:");
JLabel equalsLabel = new JLabel("=");
JLabel resultLabel = new JLabel("计算结果:");
// Create text fields
realField1 = new JTextField(5);
imagField1 = new JTextField(5);
realField2 = new JTextField(5);
imagField2 = new JTextField(5);
operatorField = new JTextField(2);
realResultField = new JTextField(5);
imagResultField = new JTextField(5);
realResultField.setEditable(false);
imagResultField.setEditable(false);
// Create button
JButton calculateButton = new JButton("计算");
calculateButton.addActionListener(new CalculateButtonListener());
// Create panel for input fields
JPanel inputPanel = new JPanel(new GridLayout(2, 3));
inputPanel.add(complex1Label);
inputPanel.add(realField1);
inputPanel.add(new JLabel("+"));
inputPanel.add(imagField1);
inputPanel.add(complex2Label);
inputPanel.add(realField2);
inputPanel.add(new JLabel("+"));
inputPanel.add(imagField2);
// Create panel for operator field and button
JPanel operatorPanel = new JPanel();
operatorPanel.add(operatorLabel);
operatorPanel.add(operatorField);
operatorPanel.add(calculateButton);
// Create panel for result fields
JPanel resultPanel = new JPanel(new GridLayout(2, 3));
resultPanel.add(resultLabel);
resultPanel.add(realResultField);
resultPanel.add(new JLabel("+"));
resultPanel.add(imagResultField);
resultPanel.add(equalsLabel);
resultPanel.add(new JLabel(""));
// Create main panel and add components
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(operatorPanel, BorderLayout.CENTER);
mainPanel.add(resultPanel, BorderLayout.SOUTH);
// Add main panel to frame
this.setContentPane(mainPanel);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private class CalculateButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Get input values
double real1 = Double.parseDouble(realField1.getText());
double imag1 = Double.parseDouble(imagField1.getText());
double real2 = Double.parseDouble(realField2.getText());
double imag2 = Double.parseDouble(imagField2.getText());
String operator = operatorField.getText();
// Perform calculation
Complex complex1 = new Complex(real1, imag1);
Complex complex2 = new Complex(real2, imag2);
Complex result = null;
switch (operator) {
case "+":
result = complex1.add(complex2);
break;
case "-":
result = complex1.subtract(complex2);
break;
case "*":
result = complex1.multiply(complex2);
break;
}
// Display result
if (result != null) {
realResultField.setText(Double.toString(result.getReal()));
imagResultField.setText(Double.toString(result.getImag()));
}
}
}
public static void main(String[] args) {
new ComplexCalculator();
}
}
```
运行该程序,即可在界面上输入两个复数和运算符,然后按下“计算”按钮,即可显示计算结果。