使用 Struts2 实现复数加减乘除运算的 Java Web 程序示例
下面是一个使用 Struts2 的 Action 设计方式实现两个复数的加减乘除的 Java Web 程序的示例。
首先,创建一个名为'ComplexNumberAction'的 Action 类来处理复数运算逻辑。在该类中,定义两个私有成员变量'real1'和'imaginary1'表示第一个复数的实部和虚部,以及'real2'和'imaginary2'表示第二个复数的实部和虚部。同时,定义一个私有成员变量'result'表示计算结果。
package com.example;
import com.opensymphony.xwork2.ActionSupport;
public class ComplexNumberAction extends ActionSupport {
private double real1;
private double imaginary1;
private double real2;
private double imaginary2;
private String result;
public double getReal1() {
return real1;
}
public void setReal1(double real1) {
this.real1 = real1;
}
public double getImaginary1() {
return imaginary1;
}
public void setImaginary1(double imaginary1) {
this.imaginary1 = imaginary1;
}
public double getReal2() {
return real2;
}
public void setReal2(double real2) {
this.real2 = real2;
}
public double getImaginary2() {
return imaginary2;
}
public void setImaginary2(double imaginary2) {
this.imaginary2 = imaginary2;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String add() {
ComplexNumber c1 = new ComplexNumber(real1, imaginary1);
ComplexNumber c2 = new ComplexNumber(real2, imaginary2);
ComplexNumber sum = c1.add(c2);
result = sum.toString();
return SUCCESS;
}
public String subtract() {
ComplexNumber c1 = new ComplexNumber(real1, imaginary1);
ComplexNumber c2 = new ComplexNumber(real2, imaginary2);
ComplexNumber difference = c1.subtract(c2);
result = difference.toString();
return SUCCESS;
}
public String multiply() {
ComplexNumber c1 = new ComplexNumber(real1, imaginary1);
ComplexNumber c2 = new ComplexNumber(real2, imaginary2);
ComplexNumber product = c1.multiply(c2);
result = product.toString();
return SUCCESS;
}
public String divide() {
ComplexNumber c1 = new ComplexNumber(real1, imaginary1);
ComplexNumber c2 = new ComplexNumber(real2, imaginary2);
ComplexNumber quotient = c1.divide(c2);
result = quotient.toString();
return SUCCESS;
}
}
接下来,创建一个名为'ComplexNumber'的辅助类来表示复数,并实现加减乘除运算方法。
package com.example;
public class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber add(ComplexNumber other) {
double sumReal = real + other.real;
double sumImaginary = imaginary + other.imaginary;
return new ComplexNumber(sumReal, sumImaginary);
}
public ComplexNumber subtract(ComplexNumber other) {
double differenceReal = real - other.real;
double differenceImaginary = imaginary - other.imaginary;
return new ComplexNumber(differenceReal, differenceImaginary);
}
public ComplexNumber multiply(ComplexNumber other) {
double productReal = real * other.real - imaginary * other.imaginary;
double productImaginary = real * other.imaginary + imaginary * other.real;
return new ComplexNumber(productReal, productImaginary);
}
public ComplexNumber divide(ComplexNumber other) {
double divisor = other.real * other.real + other.imaginary * other.imaginary;
double quotientReal = (real * other.real + imaginary * other.imaginary) / divisor;
double quotientImaginary = (imaginary * other.real - real * other.imaginary) / divisor;
return new ComplexNumber(quotientReal, quotientImaginary);
}
@Override
public String toString() {
return real + " + " + imaginary + "i";
}
}
最后,在 Struts2 配置文件(如'struts.xml')中添加以下配置:
<struts>
<package name="default" extends="struts-default">
<action name="add" class="com.example.ComplexNumberAction" method="add">
<result name="success">/result.jsp</result>
</action>
<action name="subtract" class="com.example.ComplexNumberAction" method="subtract">
<result name="success">/result.jsp</result>
</action>
<action name="multiply" class="com.example.ComplexNumberAction" method="multiply">
<result name="success">/result.jsp</result>
</action>
<action name="divide" class="com.example.ComplexNumberAction" method="divide">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
在 Web 应用的根目录下创建一个名为'result.jsp'的 JSP 页面,用于显示计算结果。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Complex Number Result</title>
</head>
<body>
<h1>Complex Number Result</h1>
<p>Result: ${result}</p>
</body>
</html>
现在,你可以通过发送 HTTP 请求来执行复数的加减乘除运算。例如,使用以下 URL 来执行两个复数的加法运算:
http://localhost:8080/your-web-app/add?real1=1&imaginary1=2&real2=3&imaginary2=4
这将返回一个包含计算结果的页面。其他运算(减法、乘法、除法)的 URL 类似。
请注意,上述示例中的代码仅用于演示目的,并未进行输入验证和错误处理。在实际应用中,你应该添加适当的验证和错误处理机制。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/pi1Y 著作权归作者所有。请勿转载和采集!