Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Random;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- /**
- *
- * @author Krzysztof Kuźniar
- */
- public class Block extends HttpServlet {
- static double maximumPointShots = 500000.0d;
- double defectSumVolume = 0.0;
- int counterDefect = 0;
- HttpSession sessionInstance;
- // double postA;
- double postC;
- double postG;
- double postR;
- double postH;
- double defectX;
- double defectY;
- double defectZ;
- double defectR;
- double randomXPoint;
- double xPointPow = 0.0;
- double randomYPoint;
- double yPointPow = 0.0;
- double randomZPoint;
- double zPointPow = 0.0;
- double endResult;
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(
- HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- sessionInstance = request.getSession();
- if (request.getMethod().equals("GET")) {
- setDefect(request);
- } else if (request.getMethod().equals("POST")) {
- calculateMass(request, response);
- }
- }
- private void setDefect(HttpServletRequest request) {
- Defect defect = new Defect();
- defect.setR(Double.parseDouble(request.getParameter("r")));
- defect.setX(Double.parseDouble(request.getParameter("x")));
- defect.setY(Double.parseDouble(request.getParameter("y")));
- defect.setZ(Double.parseDouble(request.getParameter("z")));
- sessionInstance.setAttribute(String.valueOf(counterDefect), defect);
- counterDefect++;
- }
- private void calculateAllDefects(){
- for (int i=0; i < counterDefect; i++) {
- Defect defect=(Defect)sessionInstance
- .getAttribute(String.valueOf(i));
- // defectX = defect.getX();
- // defectY = defect.getY();
- // defectZ = defect.getZ();
- // defectR = defect.getR();
- defectSumVolume += (algorithmMonteCarlo(
- defect.getX(),
- defect.getY(),
- defect.getZ(),
- defect.getR(),
- Math.pow(defect.getR(), 2))
- /(maximumPointShots * defect.getR())) * Math.pow(2 * defect.getR(), 3);
- }
- }
- private int algorithmMonteCarlo(double x, double y, double z, double r, double r2) {
- int counter = 0;
- // Random rand = new Random();
- for (int i = 0; i < maximumPointShots * r; i++) {
- randomXPoint = randomPointCalculate(r, x);// -r + rand.nextDouble() * 2*r + x;
- randomYPoint = randomPointCalculate(r, y);// -r + rand.nextDouble() * 2*r + y;
- randomZPoint = randomPointCalculate(r, z);// o -r + rand.nextDouble() * 2*r + z;
- if (allRandomPointsPowSum(randomXPoint, x, randomYPoint, y, randomZPoint, z) <= r2
- && checkRandomPointsLessThanR(randomXPoint, randomYPoint, randomZPoint, postR) ) {
- counter++;
- }
- }
- return counter;
- }
- private double allRandomPointsPowSum(double randomXPoint, double x, double randomYPoint, double y, double randomZPoint, double z) {
- xPointPow = Math.pow(randomXPoint - x, 2);
- yPointPow = Math.pow(randomYPoint - y, 2);
- zPointPow = Math.pow(randomZPoint - z, 2);
- return xPointPow + yPointPow + zPointPow;
- }
- private boolean checkRandomPointsLessThanR(double randomXPoint, double randomYPoint, double randomZPoint, double postR) {
- return randomXPoint < postR && randomYPoint < postR && randomZPoint < postR;
- }
- private double randomPointCalculate(double r, double param) {
- Random rand = new Random();
- return (-r) * rand.nextDouble() * 2.0 * r + param;
- }
- private double getBlockVolumeWithDensity() {
- return (Math.PI * Math.pow(postR, 2) * postH) * postC;
- }
- private double getDefectVolumeWithDensity( double density ) {
- return defectSumVolume * density;
- }
- private void calculateMass(HttpServletRequest request,
- HttpServletResponse response) {
- // postA = Double.parseDouble(request.getParameter("a"));
- postR = Double.parseDouble(request.getParameter("r"));
- postH = Double.parseDouble(request.getParameter("h"));
- postC = Double.parseDouble(request.getParameter("c"));
- postG = Double.parseDouble(request.getParameter("g"));
- calculateAllDefects();
- // endResult = Math.abs(getBlockVolume() - defectSumVolume) * postC
- // + defectSumVolume * postG;
- endResult = Math.abs(getBlockVolumeWithDensity() - getDefectVolumeWithDensity(postC))
- + getDefectVolumeWithDensity(postG);
- try{
- PrintWriter pw = response.getWriter();
- pw.format("%.2f", endResult);
- }catch(IOException ex){
- System.out.println(ex);
- }
- }
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- class Defect {
- private double x;
- private double y;
- private double z;
- private double r;
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- public double getZ() {
- return z;
- }
- public double getR() {
- return r;
- }
- public void setX(double x) {
- this.x = x;
- }
- public void setY(double y) {
- this.y = y;
- }
- public void setZ(double z) {
- this.z = z;
- }
- public void setR(double r) {
- this.r = r;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement