编写两个JSP页面a.jsp和b.jsp,a.jsp提供一个表单,用户可以通过表单输入两组点的坐标,并提交给b.jsp页面;b.jsp调用一个bean去完成计算两点距离的任务,b.jsp页面使用getProperty动作标记显示两点之间的距离。要求:编辑两个类,Point类和Line类,Line中成员为两个Point类对象,以及double类型的distance,即两点间距离,方法必须包括计算两点距离。
我帮你写吧。
package com;
public class Point {
private int x;
private int y;
public Point() {
}
public Point(int x,int y) {
this.x =x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
package com;
public class Line {
private Point p1;
private Point p2;
private double distance;
public Point getP1() {
return p1;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public Point getP2() {
return p2;
}
public void setP2(Point p2) {
this.p2 = p2;
}
public double getDistance() {
distance = Math.sqrt(Math.abs((p1.getX() - p2.getX())* (p1.getX() - p2.getX())+(p1.getY() - p2.getY())* (p1.getY() - p2.getY())));
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
}
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>计算2点之间的距离</title>
</head>
<body>
<h1>请输入2个点的坐标</h1>
<hr/>
<form name="form1" action="bb.jsp" method="post">
点1的X坐标:<input type="text" name="x1" value="10"><br/>
点1的Y坐标:<input type="text" name="y1" value="10"><br/>
点2的X坐标:<input type="text" name="x2" value="100"><br/>
点2的Y坐标:<input type="text" name="y2" value="100"><br/>
<input type="submit" value=" 提交 ">
</form>
</body>
</html>
<%@page import="com.Line"%>
<%@page import="com.Point"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>计算结果</title>
</head>
<body>
<h1>计算结果</h1>
<hr>
<%
int x1 = Integer.parseInt(request.getParameter("x1").toString());
int y1 = Integer.parseInt(request.getParameter("y1").toString());
int x2 = Integer.parseInt(request.getParameter("x2").toString());
int y2 = Integer.parseInt(request.getParameter("y2").toString());
Point p1 = new Point(x1,y1);
Point p2 = new Point(x2,y2);
Line line = new Line();
line.setP1(p1);
line.setP2(p2);
out.println("2点之间的距离是:"+line.getDistance());
%>
</body>
</html>
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632