Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.util.Random;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9.  
  10. /**
  11. *
  12. * @author Krzysztof Kuźniar
  13. */
  14. public class Block extends HttpServlet {
  15. static double maximumPointShots = 500000.0d;
  16. double defectSumVolume = 0.0;
  17. int counterDefect = 0;
  18.  
  19. HttpSession sessionInstance;
  20.  
  21. // double postA;
  22. double postC;
  23. double postG;
  24. double postR;
  25. double postH;
  26.  
  27. double defectX;
  28. double defectY;
  29. double defectZ;
  30. double defectR;
  31.  
  32. double randomXPoint;
  33. double xPointPow = 0.0;
  34. double randomYPoint;
  35. double yPointPow = 0.0;
  36. double randomZPoint;
  37. double zPointPow = 0.0;
  38.  
  39. double endResult;
  40. /**
  41. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  42. * methods.
  43. *
  44. * @param request servlet request
  45. * @param response servlet response
  46. * @throws ServletException if a servlet-specific error occurs
  47. * @throws IOException if an I/O error occurs
  48. */
  49. protected void processRequest(
  50. HttpServletRequest request,
  51. HttpServletResponse response)
  52. throws ServletException, IOException {
  53. response.setContentType("text/html;charset=UTF-8");
  54. sessionInstance = request.getSession();
  55. if (request.getMethod().equals("GET")) {
  56. setDefect(request);
  57. } else if (request.getMethod().equals("POST")) {
  58. calculateMass(request, response);
  59. }
  60. }
  61.  
  62. private void setDefect(HttpServletRequest request) {
  63. Defect defect = new Defect();
  64. defect.setR(Double.parseDouble(request.getParameter("r")));
  65. defect.setX(Double.parseDouble(request.getParameter("x")));
  66. defect.setY(Double.parseDouble(request.getParameter("y")));
  67. defect.setZ(Double.parseDouble(request.getParameter("z")));
  68. sessionInstance.setAttribute(String.valueOf(counterDefect), defect);
  69. counterDefect++;
  70. }
  71.  
  72. private void calculateAllDefects(){
  73. for (int i=0; i < counterDefect; i++) {
  74. Defect defect=(Defect)sessionInstance
  75. .getAttribute(String.valueOf(i));
  76. // defectX = defect.getX();
  77. // defectY = defect.getY();
  78. // defectZ = defect.getZ();
  79. // defectR = defect.getR();
  80. defectSumVolume += (algorithmMonteCarlo(
  81. defect.getX(),
  82. defect.getY(),
  83. defect.getZ(),
  84. defect.getR(),
  85. Math.pow(defect.getR(), 2))
  86. /(maximumPointShots * defect.getR())) * Math.pow(2 * defect.getR(), 3);
  87. }
  88. }
  89.  
  90. private int algorithmMonteCarlo(double x, double y, double z, double r, double r2) {
  91. int counter = 0;
  92. // Random rand = new Random();
  93. for (int i = 0; i < maximumPointShots * r; i++) {
  94. randomXPoint = randomPointCalculate(r, x);// -r + rand.nextDouble() * 2*r + x;
  95. randomYPoint = randomPointCalculate(r, y);// -r + rand.nextDouble() * 2*r + y;
  96. randomZPoint = randomPointCalculate(r, z);// o -r + rand.nextDouble() * 2*r + z;
  97.  
  98. if (allRandomPointsPowSum(randomXPoint, x, randomYPoint, y, randomZPoint, z) <= r2
  99. && checkRandomPointsLessThanR(randomXPoint, randomYPoint, randomZPoint, postR) ) {
  100. counter++;
  101. }
  102. }
  103. return counter;
  104. }
  105.  
  106. private double allRandomPointsPowSum(double randomXPoint, double x, double randomYPoint, double y, double randomZPoint, double z) {
  107. xPointPow = Math.pow(randomXPoint - x, 2);
  108. yPointPow = Math.pow(randomYPoint - y, 2);
  109. zPointPow = Math.pow(randomZPoint - z, 2);
  110.  
  111. return xPointPow + yPointPow + zPointPow;
  112. }
  113.  
  114. private boolean checkRandomPointsLessThanR(double randomXPoint, double randomYPoint, double randomZPoint, double postR) {
  115. return randomXPoint < postR && randomYPoint < postR && randomZPoint < postR;
  116. }
  117.  
  118. private double randomPointCalculate(double r, double param) {
  119. Random rand = new Random();
  120. return (-r) * rand.nextDouble() * 2.0 * r + param;
  121. }
  122.  
  123. private double getBlockVolumeWithDensity() {
  124. return (Math.PI * Math.pow(postR, 2) * postH) * postC;
  125. }
  126.  
  127. private double getDefectVolumeWithDensity( double density ) {
  128. return defectSumVolume * density;
  129. }
  130.  
  131. private void calculateMass(HttpServletRequest request,
  132. HttpServletResponse response) {
  133. // postA = Double.parseDouble(request.getParameter("a"));
  134.  
  135. postR = Double.parseDouble(request.getParameter("r"));
  136. postH = Double.parseDouble(request.getParameter("h"));
  137. postC = Double.parseDouble(request.getParameter("c"));
  138. postG = Double.parseDouble(request.getParameter("g"));
  139.  
  140. calculateAllDefects();
  141.  
  142. // endResult = Math.abs(getBlockVolume() - defectSumVolume) * postC
  143. // + defectSumVolume * postG;
  144. endResult = Math.abs(getBlockVolumeWithDensity() - getDefectVolumeWithDensity(postC))
  145. + getDefectVolumeWithDensity(postG);
  146. try{
  147. PrintWriter pw = response.getWriter();
  148. pw.format("%.2f", endResult);
  149. }catch(IOException ex){
  150. System.out.println(ex);
  151. }
  152. }
  153.  
  154. /**
  155. * Handles the HTTP <code>GET</code> method.
  156. *
  157. * @param request servlet request
  158. * @param response servlet response
  159. * @throws ServletException if a servlet-specific error occurs
  160. * @throws IOException if an I/O error occurs
  161. */
  162. @Override
  163. protected void doGet(HttpServletRequest request,
  164. HttpServletResponse response)
  165. throws ServletException, IOException {
  166. processRequest(request, response);
  167. }
  168.  
  169. /**
  170. * Handles the HTTP <code>POST</code> method.
  171. *
  172. * @param request servlet request
  173. * @param response servlet response
  174. * @throws ServletException if a servlet-specific error occurs
  175. * @throws IOException if an I/O error occurs
  176. */
  177. @Override
  178. protected void doPost(HttpServletRequest request,
  179. HttpServletResponse response)
  180. throws ServletException, IOException {
  181. processRequest(request, response);
  182. }
  183.  
  184. /**
  185. * Returns a short description of the servlet.
  186. *
  187. * @return a String containing servlet description
  188. */
  189. @Override
  190. public String getServletInfo() {
  191. return "Short description";
  192. }// </editor-fold>
  193.  
  194. class Defect {
  195. private double x;
  196. private double y;
  197. private double z;
  198. private double r;
  199.  
  200. public double getX() {
  201. return x;
  202. }
  203.  
  204. public double getY() {
  205. return y;
  206. }
  207.  
  208. public double getZ() {
  209. return z;
  210. }
  211.  
  212. public double getR() {
  213. return r;
  214. }
  215.  
  216. public void setX(double x) {
  217. this.x = x;
  218. }
  219.  
  220. public void setY(double y) {
  221. this.y = y;
  222. }
  223.  
  224. public void setZ(double z) {
  225. this.z = z;
  226. }
  227.  
  228. public void setR(double r) {
  229. this.r = r;
  230. }
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement