title: how to dynamically calculate formulas in java?
body: <p>i am developing an application in JAVA jdbc, in which i add new shapes and their formulas than calculate its area, question is something like this, </p>
  
  <p>web page should show a list of shapes (circle, square, rectangle, elipse) and statistics for each shape.</p>
  
  <p>When you click on a shape, you are asked to enter dimensions.</p>
  
  <p>For circle, only diameter is required</p>
  
  <p>For elipse, height and width is required</p>
  
  <p>... etc ...</p>
  
  <p>After entering the dimensions, the area of the shape will be calculated, and the statistics will be updated.</p>
  
  <p>It should be easy to add new types of shapes in the future.</p>
  
  <p>i have developed almost everything, but only problem is how to dynamically calculate formulas, when a shape is entered its formula saved in database, now when i ask for calculate formula, so i gave values from frontend and their formula arrive from dataabse so it calculate and show result, recently i hard code formulas in servlet like this</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          Shapes shape = new Shapes();
          int id = Integer.parseInt(request.getParameter("id"));
  
          int count = Integer.parseInt(request.getParameter("count")); 
          double values = Double.parseDouble(request.getParameter("name0")); // here values of shapes came from form jsp (form)
  
          double area = 3.14159 * values * values; // i don't want to hardcode formulas
          shape.setShapeId(id);
          shape.setShapeCount(count);
          shape.setShapeArea(area);
          shapesDao.updateShapeArea(shape);
  }
  </code></pre>
  
  <p>**All i Want to formula values came from jsp (forms) and in servlet formula of that shape came from database by its id, than formula executed and return result **</p>
  
  <p>i searched and figure out it is done by JEXL apache library, but i am not able to use this library kindly provide me help with code, i'll be greatfull to you.</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          String formula = null;
          Shapes shape = new Shapes();
          int id = Integer.parseInt(request.getParameter("id"));
  
          int count = Integer.parseInt(request.getParameter("count"));
          double values = Double.parseDouble(request.getParameter("name0"));
  
          Map m  = request.getParameterMap();
          Set s = m.entrySet();
          Iterator it = s.iterator();
  
          while(it.hasNext()){
  
              Map.Entry&lt;String,String[]&gt; entry = (Map.Entry&lt;String,String[]&gt;)it.next();
  
              String key             = entry.getKey();
              String[] value         = entry.getValue();
  
              System.out.println("Key is "+key);
  
                  if(value.length&gt;1){    
                      for (int i = 0; i &lt; value.length; i++) {
                          System.out.println("value is "+value[i].toString());
                      }
                  }else
                      System.out.println("Value is "+value[0].toString());
  
  
          }
  
      List&lt;Shapes&gt; allShapes = shapesDao.getAllShapes();
          for (Shapes shapeobj : allShapes)
          {
              if(shapeobj.getShapeId() == id)
              {
                  formula = shapeobj.getShapeFormula();
              }
          }
  
  here i have values in map and formula is in formula varialbe
  now how to store values in this polynomial answered bt Bohemian in the way its formula is ? 
  
  
  
  
  
  
          System.out.println("Formula is "+formula);
          shape.setShapeId(id);
          shape.setShapeCount(count);
          shape.setShapeArea(area);
          shapesDao.updateShapeArea(shape);
  
          List&lt;Shapes&gt; shapes = shapesDao.getAllShapes();
          System.out.println("size in server "+shapes.size());
          request.setAttribute("data", shapes);
          request.getRequestDispatcher("/index.jsp").forward(request, response);
      }
  </code></pre>
  
  <p>formulas may be different for different shapes </p>
  
  <ul>
  <li>circle (3.14*radius*radius)</li>
  <li>elippse(3.14*aaxis*baxis)</li>
  <li>rectangle(width*length)</li>
  <li>square(a*a)</li>
  </ul>
  
  <p>and so on</p>