基于JSP+JavaBean的设计模式,设计任意两个复数实现4则运算(复数加法、减法、乘法、除法)的Web程序。
时间: 2024-05-04 16:22:27 浏览: 155
好的,以下是基于JSP+JavaBean的复数计算器的设计:
1. 创建一个名为 ComplexNumber 的 JavaBean 类,该类包含以下属性和方法:
- 实部 real 和虚部 imag
- 无参构造函数和带参构造函数
- 加法方法 add,减法方法 subtract,乘法方法 multiply,除法方法 divide
2. 创建一个名为 ComplexCalculator 的 JSP 页面,该页面包含一个表单,用户可以输入两个复数的实部和虚部,并选择要执行的运算。
3. 当用户提交表单时,ComplexCalculator 页面将调用 ComplexNumber JavaBean 中相应的方法并显示结果。
下面是 ComplexNumber JavaBean 的代码:
```java
public class ComplexNumber {
private double real;
private double imag;
public ComplexNumber() {}
public ComplexNumber(double real, double imag) {
this.real = real;
this.imag = imag;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImag() {
return imag;
}
public void setImag(double imag) {
this.imag = imag;
}
public ComplexNumber add(ComplexNumber other) {
double newReal = this.real + other.real;
double newImag = this.imag + other.imag;
return new ComplexNumber(newReal, newImag);
}
public ComplexNumber subtract(ComplexNumber other) {
double newReal = this.real - other.real;
double newImag = this.imag - other.imag;
return new ComplexNumber(newReal, newImag);
}
public ComplexNumber multiply(ComplexNumber other) {
double newReal = this.real * other.real - this.imag * other.imag;
double newImag = this.real * other.imag + this.imag * other.real;
return new ComplexNumber(newReal, newImag);
}
public ComplexNumber divide(ComplexNumber other) {
double denominator = other.real * other.real + other.imag * other.imag;
double newReal = (this.real * other.real + this.imag * other.imag) / denominator;
double newImag = (this.imag * other.real - this.real * other.imag) / denominator;
return new ComplexNumber(newReal, newImag);
}
}
```
下面是 ComplexCalculator JSP 页面的代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.example.ComplexNumber" %>
<html>
<head>
<title>复数计算器</title>
</head>
<body>
<h1>复数计算器</h1>
<form method="post">
<label>第一个复数:</label>
实部:<input type="text" name="real1">,虚部:<input type="text" name="imag1"><br>
<label>第二个复数:</label>
实部:<input type="text" name="real2">,虚部:<input type="text" name="imag2"><br>
<label>选择运算:</label>
<select name="operation">
<option value="add">加法</option>
<option value="subtract">减法</option>
<option value="multiply">乘法</option>
<option value="divide">除法</option>
</select><br>
<input type="submit" value="计算">
</form>
<%-- 处理表单提交 --%>
<%
if (request.getMethod().equalsIgnoreCase("post")) {
double real1 = Double.parseDouble(request.getParameter("real1"));
double imag1 = Double.parseDouble(request.getParameter("imag1"));
double real2 = Double.parseDouble(request.getParameter("real2"));
double imag2 = Double.parseDouble(request.getParameter("imag2"));
String operation = request.getParameter("operation");
ComplexNumber num1 = new ComplexNumber(real1, imag1);
ComplexNumber num2 = new ComplexNumber(real2, imag2);
ComplexNumber result = null;
switch (operation) {
case "add":
result = num1.add(num2);
break;
case "subtract":
result = num1.subtract(num2);
break;
case "multiply":
result = num1.multiply(num2);
break;
case "divide":
result = num1.divide(num2);
break;
}
// 显示结果
out.println("<p>结果为:" + result.getReal() + " + " + result.getImag() + "i</p>");
}
%>
</body>
</html>
```
这样就完成了基于 JSP+JavaBean 的复数计算器的设计。用户可以在 ComplexCalculator 页面上输入两个复数及要执行的运算,然后提交表单,页面将调用 ComplexNumber JavaBean 中相应的方法并显示结果。
阅读全文