使用JavaBean封装数据和业务逻辑

学习到JavaBean了,打个小栗子,大神勿喷

功能:输入三条边,判断是否构成三角形,并计算面积

Stringtonum.java:封装了三角形三条边及一个字符串分离出三条边

Triangle.java:封装了三角形三条边,判断是否构成三角形,并计算面积

index.jsp:用户访问页面

index.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <jsp:useBean id="angle" class="bean.Stringtonum" scope="page"></jsp:useBean>
 3 <jsp:useBean id="tri" class="bean.Triangle" scope="page"></jsp:useBean>
 4 
 5 
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>
 9     
10     <title>使用JavaBean封装数据和业务逻辑,输出三角形面积</title>
11   </head>
12   
13   <body>
14     请输入三角形三条边的长度,并使用逗号分隔:<br />
15   <form method="post">
16       <input type="text" name="boy">
17       <input type="submit" value="提交">
18   </form>
19   <%
20       String str = request.getParameter("boy");
21       if(!angle.strtonum(str)){
22           out.println("请输入三个数");
23           return;
24       }
25       tri.setEdge1(angle.getNum1());
26       tri.setEdge2(angle.getNum2());
27       tri.setEdge3(angle.getNum3());
28       if(!tri.isTriangle()){
29           out.println("您输入的三条边不能构成一个三角形");
30           return;
31       }else{
32           out.println("三角形的面积 = "+tri.calArea());
33       }
34    %>
35    <br>您输入的三条边是:<% out.println(tri.getEdge1()); %>,<% out.println(tri.getEdge2()); %>,<% out.println(tri.getEdge3()); %>
36   </body>
37 </html>

Stringtonum.java

 1 package bean;
 2 
 3 /*
 4  * 功能:获取输入的三个数字,并对这三个数字进行分割处理
 5  */
 6 
 7 public class Stringtonum {
 8 
 9     private double num1;
10     private double num2;
11     private double num3;
12     public Stringtonum(){}
13     public double getNum1() {
14         return num1;
15     }
16     public double getNum2() {
17         return num2;
18     }
19     public double getNum3() {
20         return num3;
21     }
22     public void setNum1(double n) {
23         num1 = n;
24     }
25     public void setNum2(double n) {
26         num2 = n;
27     }
28     public void setNum3(double n) {
29         num3 = n;
30     }
31     public boolean strtonum(String str){
32         //取三边
33         double a[] = new double[3];
34         int i;
35         if(str==null){
36             return false;
37         }
38         String substr=",";
39         //字符串分割
40         String[] as = str.split(substr);
41         if(as.length != 3){
42             return false;
43         }else{
44             for(i=0;i<3;i++){
45                 //将字符类型a数组转换成double类型a数组
46                 a[i] = Double.valueOf(as[i]);
47             }
48         }
49         num1 = a[0];
50         num2 = a[1];
51         num3 = a[2];
52         if(num1<0.0||num2<0.0||num3<0.0){
53             return false;
54         }
55         return true;
56     }
57     
58 }

Triangle.java

 1 package bean;
 2 
 3 public class Triangle {
 4     private double edge1;
 5     private double edge2;
 6     private double edge3;
 7     public Triangle(){}
 8     public Triangle(double e1,double e2,double e3){
 9         this.edge1 = e1;
10         this.edge2 = e2;
11         this.edge3 = e3;
12     }
13     public double getEdge1() {
14         return edge1;
15     }
16     public void setEdge1(double edge) {
17         this.edge1 = edge;
18     }
19     public double getEdge2() {
20         return edge2;
21     }
22     public void setEdge2(double edge) {
23         this.edge2 = edge;
24     }
25     public double getEdge3() {
26         return edge3;
27     }
28     public void setEdge3(double edge) {
29         this.edge3 = edge;
30     }
31     //判断是否构成三角形
32     public boolean isTriangle(){
33         if(edge1 + edge2 > edge3 && edge1 + edge3 > edge2 && edge3 + edge2 > edge1){
34             return true;
35         }else{
36             return false;
37         }
38     }
39     public double calArea(){
40         //海伦公式求三角形面积
41         double p = (edge1 + edge2 +edge3)/2;
42         return Math.sqrt(p * (p-edge1) * (p-edge2) * (p-edge3));
43     }
44 }
posted @ 2017-10-26 21:40  时光丶巷陌  阅读(1306)  评论(0编辑  收藏  举报