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,
web page should show a list of shapes (circle, square, rectangle, elipse) and statistics for each shape.
When you click on a shape, you are asked to enter dimensions.
For circle, only diameter is required
For elipse, height and width is required
... etc ...
After entering the dimensions, the area of the shape will be calculated, and the statistics will be updated.
It should be easy to add new types of shapes in the future.
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
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);
}
**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 **
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.
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<String,String[]> entry = (Map.Entry<String,String[]>)it.next();
String key = entry.getKey();
String[] value = entry.getValue();
System.out.println("Key is "+key);
if(value.length>1){
for (int i = 0; i < value.length; i++) {
System.out.println("value is "+value[i].toString());
}
}else
System.out.println("Value is "+value[0].toString());
}
List<Shapes> 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<Shapes> shapes = shapesDao.getAllShapes();
System.out.println("size in server "+shapes.size());
request.setAttribute("data", shapes);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
formulas may be different for different shapes
- circle (3.14*radius*radius)
- elippse(3.14*aaxis*baxis)
- rectangle(width*length)
- square(a*a)
and so on