题目:
一、有一个名为Complex的类,它的UML图如下,实现该类的相关方法,通过图形界面来显示运行结果。
Complex
-realPart:double 实部
-imaginaryPart:double 虚部
+Complex() 默认构造方法
+Complex(double r,double i) 带参数的构造方法
+getRealPart():double 返回实部
+setRealPart(double d): void 置实部
+getImaginary (): double 返回虚部
+setImaginary (double d):void 设置虚部
+complexAdd(Complex c1, Complex c2): void 复数对象与复数对象相加
+complexMinus(Complex c1, Complex c2) : void 复数对象与复数对象相减
+complexMul(Complex c1, Complex c2) : void 复数对象与复数对象相乘
+complexDiv(Complex c1, Complex c2) : void 复数对象与复数对象相除
+toString():String 以a+bi的形式显示复数
public class Complex22 extends Application {
// 实部
double realPart;
// 虚部
double imaginaryPart;
// 无参构造函数
public Complex22() {
realPart = 0;
imaginaryPart = 0;
}
// 有参数构造函数
public Complex22(double r, double i) {
this.realPart = r;
this.imaginaryPart = i;
}
// getter和setter方法
public double getRealPart() {
return realPart;
}
public void setRealPart(double d) {
this.realPart = d;
}
public double getImaginaryPart() {
return imaginaryPart;
}
public void setImaginaryPart(double d) {
this.imaginaryPart = d;
}
// 复数的加法
public void Add(Complex22 c1, Complex22 c2, TextArea textArea) {
double a = c1.getRealPart();
double b = c1.getImaginaryPart();
double c = c2.getRealPart();
double d = c2.getImaginaryPart();
Complex22 result = new Complex22(a + c, b + d);
textArea.appendText("(" + c1 + ")" + "+" + "(" + c2 + ")" + "=" + result + "\n");
}
// 复数的减法
public void Minus(Complex22 c1, Complex22 c2, TextArea textArea) {
double a = c1.getRealPart();
double b = c1.getImaginaryPart();
double c = c2.getRealPart();
double d = c2.getImaginaryPart();
Complex22 result = new Complex22(a - c, b - d);
textArea.appendText("(" + c1 + ")" + "-" + "(" + c2 + ")" + "=" + result + "\n");
}
// 复数的乘法
public void Mul(Complex22 c1, Complex22 c2, TextArea textArea) {
double a = c1.getRealPart();
double b = c1.getImaginaryPart();
double c = c2.getRealPart();
double d = c2.getImaginaryPart();
Complex22 result = new Complex22((a * c) - (b * d), (a * d) + (b * c));
textArea.appendText("(" + c1 + ")" + "×" + "(" + c2 + ")" + "=" + result + "\n");
}
// 复数的除法
public void Div(Complex22 c1, Complex22 c2, TextArea textArea) {
double a = c1.getRealPart();
double b = c1.getImaginaryPart();
double c = c2.getRealPart();
double d = c2.getImaginaryPart();
Complex22 result = new Complex22((a * c + b * d) / (c * c + d * d), (b * c - a * d) / (c * c + d * d));
textArea.appendText("(" + c1 + ")" + "÷" + "(" + c2 + ")" + "=" + result + "\n");
}
// toString方法
public String toString() {
if (realPart == 0 && imaginaryPart == 0) {
return realPart + "+" + imaginaryPart + "i";
} else if (realPart == 0) {
return imaginaryPart + "i";
} else if (imaginaryPart == 0) {
return realPart + " ";
} else if (imaginaryPart < 0) {
return realPart + "" + imaginaryPart + "i";
} else {
return realPart + "+" + imaginaryPart + "i";
}
}
// 字体
Font f1 = Font.font("宋体", 19);
Font f2 = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 18);
Font f3 = Font.font("宋体", 18);
// 复数
HBox h1 = new HBox();
Label complex1 = new Label("复数1");
TextField tf1 = new TextField();
Label complex2 = new Label("复数2");
TextField tf2 = new TextField();
// 文本框
HBox h2 = new HBox();
TextArea tArea = new TextArea();
// 按钮
HBox h3 = new HBox();
Button add = new Button("加 法"), sub = new Button("减 法"), mul = new Button("乘 法"), div = new Button("除 法");
@Override
public void start(Stage stage) {
// 创建内部类对象
ButtonHandler handler = new ButtonHandler();
// 复数的样式布局
h1.setPadding(new Insets(50, 40, 50, 50));
h1.setSpacing(10);
complex1.setFont(f1);
tf1.setPromptText("a+bi");
tf1.setPrefSize(200, 20);
tf1.setFont(f2);
complex2.setFont(f1);
tf2.setPromptText("c+di");
tf2.setPrefSize(200, 20);
tf2.setFont(f2);
h1.getChildren().addAll(complex1, tf1, complex2, tf2);
// 文本框的样式布局
h2.setPadding(new Insets(0, 40, 50, 65));
tArea.setPrefSize(500, 300);
tArea.setPromptText("计算结果...");
tArea.setFont(f3);
h2.getChildren().add(tArea);
// 按钮的样式布局
h3.setPadding(new Insets(0, 50, 50, 100));
h3.setSpacing(25);
add.setPrefSize(90, 20);
add.setFont(f3);
sub.setPrefSize(90, 20);
sub.setFont(f3);
mul.setPrefSize(90, 20);
mul.setFont(f3);
div.setPrefSize(90, 20);
div.setFont(f3);
h3.getChildren().addAll(add, sub, mul, div);
// 为加法按钮注册事件处理器
add.setOnAction(handler);
// 为减法按钮注册事件处理器
sub.setOnAction(handler);
// 为乘法按钮注册事件处理器
mul.setOnAction(handler);
// 为除法按钮注册事件处理器
div.setOnAction(handler);
// 创建根面板
BorderPane rootNode = new BorderPane();
rootNode.setTop(h1);
rootNode.setCenter(h2);
rootNode.setBottom(h3);
// 设置场景和舞台
Scene scene = new Scene(rootNode, 630, 630);
stage.setTitle("复数计算器");
stage.setScene(scene);
stage.setResizable(false);// 固定窗口大小
stage.show();
}
// 内部类实现事件处理方法
public class ButtonHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
// 对象实例化
Complex22 c1 = new Complex22();
Complex22 c2 = new Complex22();
// 复数实部和虚部的提取
if (tf1.getText().matches("(\\d{1,}\\+\\d{1,})[i]")) { // a+bi
String[] str = tf1.getText().split("[+i]");
c1.realPart = Double.parseDouble(str[0]);
c1.imaginaryPart = Double.parseDouble(str[1]);
} else if (tf1.getText().matches("(\\d{1,}\\-\\d{1,})[i]")) { // a-bi
String[] str = tf1.getText().split("[-i]");
c1.realPart = Double.parseDouble(str[0]);
double b = Double.parseDouble(str[1]);
c1.imaginaryPart = -b;
} else if (tf1.getText().matches("(\\-\\d{1,}\\+\\d{1,})[i]")) { // -a+bi
String[] str = tf1.getText().split("[+i]");
c1.realPart = Double.parseDouble(str[0]);
c1.imaginaryPart = Double.parseDouble(str[1]);
} else if (tf1.getText().matches("(\\-\\d{1,}\\-\\d{1,})[i]")) {// -a-bi
String[] str = tf1.getText().split("[-i]");
double a = Double.parseDouble(str[1]);
double b = Double.parseDouble(str[2]);
c1.realPart = -a;
c1.imaginaryPart = -b;
} else if (tf1.getText().matches("(\\-?\\d{1,})[i]")) { // bi // //-bi
String[] str = tf1.getText().split("[i]");
c1.realPart = 0;
c1.imaginaryPart = Double.parseDouble(str[0]);
} else if (tf1.getText().matches("\\-?\\d{1,}\\+[i]")) { // a+i // //-a+i
String[] str = tf1.getText().split("[+i]");
c1.realPart = Double.parseDouble(str[0]);
c1.imaginaryPart = 1;
} else if (tf1.getText().matches("\\d{1,}\\-[i]")) { // a-i
String[] str = tf1.getText().split("[-i]");
c1.realPart = Double.parseDouble(str[0]);
c1.imaginaryPart = -1;
} else if (tf1.getText().matches("\\-\\d{1,}-[i]")) { // -a-i
String[] str = tf1.getText().split("[-i]");
double a = Double.parseDouble(str[1]);
c1.realPart = -a;
c1.imaginaryPart = -1;
} else if (tf1.getText().matches("[i]")) { // i
c1.realPart = 0;
c1.imaginaryPart = 1;
} else if (tf1.getText().matches("\\-[i]")) { // -i
c1.realPart = 0;
c1.imaginaryPart = -1;
} else if (tf1.getText().matches("\\d{1,}")) { // a
String[] str = tf1.getText().split("[ ]");
c1.realPart = Double.parseDouble(str[0]);
c1.imaginaryPart = 0;
} else if (tf1.getText().matches("\\-\\d{1,}")) { // -a
String[] str = tf1.getText().split("[-]");
double a = Double.parseDouble(str[1]);
c1.realPart = -a;
c1.imaginaryPart = 0;
}else {
Alert alert =new Alert(AlertType.WARNING);
alert.titleProperty().set("出错了");
alert.headerTextProperty().set("数据格式不对");
alert.show();
}
if (tf2.getText().matches("(\\d{1,}\\+\\d{1,})[i]")) { // a+bi
String[] str1 = tf2.getText().split("[+i]");
c2.realPart = Double.parseDouble(str1[0]);
c2.imaginaryPart = Double.parseDouble(str1[1]);
} else if (tf2.getText().matches("(\\d{1,}\\-\\d{1,})[i]")) { // a-bi
String[] str1 = tf2.getText().split("[-i]");
c2.realPart = Double.parseDouble(str1[0]);
double b1 = Double.parseDouble(str1[1]);
c2.imaginaryPart = -b1;
} else if (tf2.getText().matches("(\\-\\d{1,}\\+\\d{1,})[i]")) { // -a+bi
String[] str1 = tf2.getText().split("[+i]");
c2.realPart = Double.parseDouble(str1[0]);
c2.imaginaryPart = Double.parseDouble(str1[1]);
} else if (tf2.getText().matches("(\\-\\d{1,}\\-\\d{1,})[i]")) {// -a-bi
String[] str1 = tf2.getText().split("[-i]");
double a1 = Double.parseDouble(str1[1]);
double b1 = Double.parseDouble(str1[2]);
c2.realPart = -a1;
c2.imaginaryPart = -b1;
} else if (tf2.getText().matches("(\\-?\\d{1,})[i]")) { // bi // //-bi
String[] str1 = tf2.getText().split("[i]");
c2.realPart = 0;
c2.imaginaryPart = Double.parseDouble(str1[0]);
} else if (tf2.getText().matches("\\-?\\d{1,}\\+[i]")) { // a+i // //-a+i
String[] str1 = tf2.getText().split("[+i]");
c2.realPart = Double.parseDouble(str1[0]);
c2.imaginaryPart = 1;
} else if (tf2.getText().matches("\\d{1,}\\-[i]")) { // a-i
String[] str1 = tf2.getText().split("[-i]");
c2.realPart = Double.parseDouble(str1[0]);
c2.imaginaryPart = -1;
} else if (tf2.getText().matches("\\-\\d{1,}-[i]")) { // -a-i
String[] str1 = tf2.getText().split("[-i]");
double a1 = Double.parseDouble(str1[1]);
c2.realPart = -a1;
c2.imaginaryPart = -1;
} else if (tf2.getText().matches("[i]")) { // i
c2.realPart = 0;
c2.imaginaryPart = 1;
} else if (tf2.getText().matches("\\-[i]")) { // -i
c2.realPart = 0;
c2.imaginaryPart = -1;
} else if (tf2.getText().matches("\\d{1,}")) { // a
String[] str1 = tf1.getText().split("[ ]");
c2.realPart = Double.parseDouble(str1[0]);
c2.imaginaryPart = 0;
} else if (tf2.getText().matches("\\-\\d{1,}")) { // -a
String[] str1 = tf2.getText().split("[-]");
double a1 = Double.parseDouble(str1[1]);
c2.realPart = -a1;
c2.imaginaryPart = 0;
}else {
Alert alert =new Alert(AlertType.WARNING);
alert.titleProperty().set("出错了");
alert.headerTextProperty().set("数据格式不对");
alert.show();
}
if ((Button) (event.getSource()) == add) { // 加法
try {
Complex22 c = new Complex22();
c.Add(c1, c2, tArea);
} catch (Exception e) {
// TODO: handle exception
}
} else if (event.getSource() == sub) { // 减法
try {
Complex22 c = new Complex22();
c.Minus(c1, c2, tArea);
} catch (Exception e) {
// TODO: handle exception
}
} else if (event.getSource() == mul) { // 乘法
try {
Complex22 c = new Complex22();
c.Mul(c1, c2, tArea);
} catch (Exception e) {
// TODO: handle exception
}
} else if (event.getSource() == div) { // 除法
try {
Complex22 c = new Complex22();
c.Div(c1, c2, tArea);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
缺点:只能够实现以上几种a+bi形式复数的计算,如果实部和虚部之间加一些空格,或者是“3+3”、“6i+6i”的形式,会跳出警告框。