Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Random;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author Maciek
- */
- public class Cone extends HttpServlet {
- /**
- * 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");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet Cone</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Servlet Cone at " + request.getContextPath() + "</h1>");
- out.println("</body>");
- out.println("</html>");
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * 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);
- ArrayList<Double> defects;
- if (request.getSession().getAttribute("defects") == null) {
- defects = new ArrayList<Double>();
- } else {
- defects = (ArrayList<Double>) request.getSession().getAttribute("defects");
- }
- if ( request.getParameter("x") != null
- && request.getParameter("y") != null
- && request.getParameter("z") != null
- && request.getParameter("r") != null){
- double x = Double.parseDouble(request.getParameter("x"));
- double y = Double.parseDouble(request.getParameter("y"));
- double z = Double.parseDouble(request.getParameter("z"));
- double r = Double.parseDouble(request.getParameter("r"));
- defects.add(x);
- defects.add(y);
- defects.add(z);
- defects.add(r);
- request.getSession().setAttribute("defects", defects);
- }
- }
- /**
- * 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);
- if ( request.getParameter("ra") != null
- && request.getParameter("rb") != null
- && request.getParameter("h") != null
- && request.getParameter("c") != null
- && request.getParameter("g") != null){
- double ra = Double.parseDouble(request.getParameter("ra"));
- double h = Double.parseDouble(request.getParameter("h"));
- double rb = Double.parseDouble(request.getParameter("rb"));
- double coneDensity = Double.parseDouble(request.getParameter("c"));
- double globeDensity = Double.parseDouble(request.getParameter("g"));
- ArrayList<Double> defects = (ArrayList<Double>) request.getSession().getAttribute("defects");
- double H = (h * ra) / (ra - rb);
- double result = retMass(ra, rb, h, H, defects, coneDensity, globeDensity);
- }
- }
- private boolean isInFigure(double ra, double rb, double h,
- double H, double x, double y, double z){
- if( (Math.pow(x, 2) + Math.pow(y, 2) <= Math.pow(((z*ra)/H), 2) && ( 0 <= z ) && (z <= H))
- && !(Math.pow(x, 2) + Math.pow(y, 2) <= Math.pow(((z*rb)/h), 2) && ( 0 <= z ) && (z <= h)))
- return true;
- return false;
- }
- private int monteCarlo(int precision, double ra, double rb, double h, double H, ArrayList<Double> defects){
- double randomX = 0;
- double randomY = 0;
- double randomZ = 0;
- Random r = new Random();
- int defectsSize = defects.size();
- int defectHits = 0;
- for( int i = precision; i >=0; i--){
- do{
- randomX = -ra + ( ra + ra) * r.nextDouble();
- randomY = -ra + ( ra + ra) * r.nextDouble();
- randomZ = h * r.nextDouble();
- } while ( !isInFigure(ra, rb, h, H, randomX, randomY, randomZ));
- for(int j = 0; j < defectsSize; j+=4) {
- if ( (randomX - defects.get(j)) * (randomX - defects.get(j))
- + (randomY - defects.get(j+1)) * (randomY - defects.get(j+1))
- + (randomZ - defects.get(j+2)) * (randomZ - defects.get(j+2))
- <= defects.get(j+3) * defects.get(j+3)){
- defectHits++;
- }
- }
- }
- return defectHits;
- }
- private double retMass(double ra, double rb, double h, double H, ArrayList<Double> defects, double coneDens, double globeDens) {
- int precision = 10000000;
- double defectHits = monteCarlo(precision, ra, rb, h, H, defects);
- double truncatedConeVolume = ( Math.PI * h * ((ra * ra) + (ra * rb) +(rb * rb))) / 3;
- double coneHits = precision - defectHits;
- double coneHitsVolume = truncatedConeVolume * coneHits/precision;
- double defectsVolume = truncatedConeVolume - coneHitsVolume;
- double properMass = coneHitsVolume * coneDens + defectsVolume * globeDens;
- return properMass;
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement