【jsp練習】与えられた三角形の三辺が三角形を構成できるか否か及び計算面積を判断する

10749 ワード

Test.java
package package1;



public class Test {

    double side1 = -1 , side2 = -1 , side3 = -1 , area = -1;

    boolean triangle;

    

    public double getSide1() {

        return side1;

    }

    public void setSide1(double side1) {

        this.side1 = side1;

    }

    public double getSide2() {

        return side2;

    }

    public void setSide2(double side2) {

        this.side2 = side2;

    }

    public double getSide3() {

        return side3;

    }

    public void setSide3(double side3) {

        this.side3 = side3;

    }

    public double getArea() {

        double p = (side1 + side2 + side3)/2.0;

        if(triangle) area = Math.sqrt( p * (p - side1) * (p - side2) * (p - side3) );

        return area;

        

    }

    public void setArea(double area) {

        this.area = area;

    }

    public boolean isTriangle() {

        if(side1<side2+side3 && side2<side1+side3 && side3<side1+side2)

            triangle=true;

        else triangle=false;

        return triangle;

    }

    public void setTriangle(boolean triangle) {

        this.triangle = triangle;

    }   

}

 
 
 
 
triangle.jsp
<%@page contentType="text/html;charset=gb2312" pageEncoding="gb2312" %>

<%@page import="package1.Test" %>



<jsp:useBean id="tri" class="package1.Test" scope="page" />



<html>

    <head></head>

    <body>

        <form action="" method="post">

            a:<input type="text" name="side1" value="0" />

            b:<input type="text" name="side2" value="0" />

            c:<input type="text" name="side3" value="0" />

            <input type="submit" value="submit" />

        </form>

        

        <jsp:setProperty name="tri" property="*" />

        

         :

         a :<jsp:getProperty name="tri" property="side1" />

         b :<jsp:getProperty name="tri" property="side2" />

         c :<jsp:getProperty name="tri" property="side3" />

        <br /><jsp:getProperty name="tri" property="triangle" />

        

        <!--  triangle ,servlet triangle ,area -1 -->

        

         :

        <jsp:getProperty name="tri" property="area" />

    </body>

</html>

 
実行結果:
 
a:
 b:
 c:
 
三角形の3辺は:辺a:2.0辺b:4.0辺c:5.0辺が三角形を構成できるかどうか:true面積は:3.99671038392666