04/29/2022
As exams come closer, I figured that I could train my coding skills, hence the need to develop this small, (pretty much) functional calculator, with some basic mathematical functions.
Below in this documentation I will list methods that work, and ones that don't.
Note that it is currently not possibile to perform operations with more than two operands.
Every possible operation is listed in class Calculator.
Below I made a list that elencates every possible operation:
SUM()- Performs a simple addition between operands.
SUB()- Performs a simple subtraction between operands.
MUL()- Performs a simple multiplication between two operands.
DIV()- Performs a simple division between two operands.
- Trying to divide by zero will throw a
DivisionExcetpion()
squareRoot()- Evaluates the square root of an operand.
toPercentage()- Converts the number in a percentage format.
elv()- Currently unavailable, further details can be found below.
elvTen()- Evaluates ten to the power of a value.
factorial()- Evaluates the factorial of an operand in recursive process.
logarithm()- Evaluates the logarithm in base 10 of an operand.
radToDeg()- Converts from radians to degrees an operand.
degToRad()- Reverse process of the above method.
toSin()- Evaluates the sin of an operand.
toCos()- Evaluates the cosin of an operand.
toTan()- Evaluates the tangent of an operand.
fractionString()- A formatter to print in fraction syntax.
To actually make a request to the server, follow the below syntax:
https://localhost:8080/Servlet/Calculator/"operation"?param1=value¶m2=value
Where "operation" is the mathematical function you want to apply.
As stated before, the elv() method is currently unavailable, this is because of formatting issues when parsing the actual URL request, this is mainly because of this block of code:
if(var1 == null || var2 == null || var1.equals("") || var2.equals("")){
response.sendError(400, "Missing parameters.\r\n");
return;
}
Which stops the program if either param x or y is missing.
Because the elv() method actually requires two parameters: base and exponent I couldn't figure out how to (decently) format the request URL.
I know it's a stupid concern, and it is easily solvable; I think it was because I spent too much time on this project solving Tomcat issues and such.
There are some lines of codes that are commented out, the ones below for reference:
// double z = Double.parseDouble(var3);
// out.println("<prefix>" + c.getPrefix() + "</prefix>");
// out.println("<suffix>" + c.getSuffix() + "</suffix>");
Believe it or not, the z parameter is what actually caused the issue of the elv() method - for some reason I thought of using a third additional - and optional - parameter.
About using only one parameter, for instance, the elvTen() method only needs one, and as such a user must be able to type the request with only one parameter; this is an issue because, as stated before, entering only one parameter results in a
400 BAD REQUEST - Missing parameters.
response, which should not happen.
The same issue applies with pretty much every other method, exclduing the main four operation ones.
- [] Fix single parameters methods.
- [] Fix code readability.
- [] Add more complex functions.
- [] Fix general, smaller bugs.
- [] Implement formatting methods to visualize powers, fractions etc.