Page 83 - การโปรแกรมเว็บ
P. 83

เจเอสพี 7-71

ตัวอย่างท่ี 7.29 การเขียนฟังกช์ ันสำ� หรบั คำ� นวณพ้ืนทีส่ ่เี หลี่ยมคางหมู

MyJSP_CalTrapeZoidArea.jsp
โปรแกรมนีเ้ ปน็ การเขยี นฟังก์ชันเพอ่ื หาคา่ พนื้ ทีส่ เี่ หล่ียมคางหมู พร้อมทง้ั แสดงผลลพั ธ์บนหน้าเวบ็

   1 <html>
  2 <head>
  3 	 <title> JSP Page : Test Function for Calculate Trapezoid Area </title>
  4 </head>
  5 <body>
  6 		 <%! double CalTrapeZiodArea(int n1, int n2, int h) {
  7 			 return 0.5 * (n1+n2) * h;
  8 		 } %>
  9 		 <h3> โปรแกรมคำ� นวณพน้ื ที่สี่เหลย่ี มคางหมู </h3>
  10 		 <hr>
  11 		 <h3> TrapedZoidArea 1 is : <% out.print(CalTrapeZiodArea (5, 8, 10)); %> </h3>
  12 		 <h3> TrapedZoidArea 2 is : <% out.print(CalTrapeZiodArea (3, 4, 6)); %> </h3>
  13 </body>
  14 </html>

ผลการท�ำงานของโปรแกรมแสดงดงั ภาพที่ 7.28

ภาพท่ี 7.28 ผลการท�ำงานของโปรแกรมค�ำนวณพ้ืนท่ีส่ีเหลี่ยมคางหมู
   78   79   80   81   82   83   84   85   86   87   88