Commit 2f484668 by Matias Ferreira

cambie tipo de dato de factorial de int a BigInteger para evitar desbordamento

parent c8659dc5
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
<body> <body>
<p>hola mundos</p> <p>hola mundos</p>
<ol> <ol>
<li><a href="http://localhost:8080/WebProjectTest/tabla-multiplicar">aprende a multiplicar!</a></li> <li><a href="http://localhost:8080/JWeb-e001/tabla-multiplicar">aprende a multiplicar!</a></li>
<li><a href="http://localhost:8080/WebProjectTest/factorial">aprende a factorializar!</a></li> <li><a href="http://localhost:8080/JWeb-e001/factorial">aprende a factorializar!</a></li>
<li><a href="http://localhost:8080/WebProjectTest/show-me-the-bits">aprender conversion decimal/binario!</a></li> <li><a href="http://localhost:8080/JWeb-e001/show-me-the-bits">aprender conversion decimal/binario!</a></li>
</ol> </ol>
</body> </body>
......
package com.roshka.webprojecttest.servlets; package com.roshka.webprojecttest.servlets;
import java.math.BigInteger;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
...@@ -15,6 +16,7 @@ public class FactorialServlet extends HttpServlet { ...@@ -15,6 +16,7 @@ public class FactorialServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException throws ServletException, IOException
{ {
System.out.println("dentro del doGet");
PrintWriter pw = new PrintWriter(resp.getOutputStream()); PrintWriter pw = new PrintWriter(resp.getOutputStream());
Boolean valido=true; Boolean valido=true;
String parametro = req.getParameter("number"); String parametro = req.getParameter("number");
...@@ -43,9 +45,11 @@ public class FactorialServlet extends HttpServlet { ...@@ -43,9 +45,11 @@ public class FactorialServlet extends HttpServlet {
negative(pw); negative(pw);
} }
else{ else{
long facto=1; BigInteger facto = BigInteger.ONE;
for (long i = 2; i <=numero;i++) for (Integer i = 2; i <=numero; i++) {
facto*=i; facto = facto.multiply(new BigInteger(i.toString()));
System.out.printf("factoActual[%d]: ", i, facto );
}
factoDisplay(pw, numero, facto); factoDisplay(pw, numero, facto);
} }
} }
...@@ -53,7 +57,8 @@ public class FactorialServlet extends HttpServlet { ...@@ -53,7 +57,8 @@ public class FactorialServlet extends HttpServlet {
pw.close(); pw.close();
} }
void factoDisplay(PrintWriter pw, Integer numero, Long facto) { void factoDisplay(PrintWriter pw, Integer numero, BigInteger facto) {
System.out.println("dentro del factoDisplay");
pw.write("<html>" pw.write("<html>"
+ "<head>" + "<head>"
+ "<title>Hola, bienvenidos al factorializador</title>" + "<title>Hola, bienvenidos al factorializador</title>"
...@@ -61,13 +66,13 @@ public class FactorialServlet extends HttpServlet { ...@@ -61,13 +66,13 @@ public class FactorialServlet extends HttpServlet {
+ "<body>" + "<body>"
+ "<h1>Soy un generador de factorial de numeros</h1>" + "<h1>Soy un generador de factorial de numeros</h1>"
+ "<div>" + "<div>"
+ "<form action=\"factorial-response\" >" + "<form action=\"factorial\" method=\"get\">"
+ "Introduzca el numero: <input type=\"text\" name=number><br>" + "Introduzca el numero: <input type=\"text\" name=number><br>"
+ "<input type=\"submit\">" + "<input type=\"submit\">"
+ "</form>" + "</form>"
+ "</div>" + "</div>"
+ "<div>" + "<div>"
+ "<p>El factorial de "+ numero+" es: " + facto +"</p>" + "<p>El factorial de "+ numero+" es: " + facto.toString() +"</p>"
+ "</div>" + "</div>"
+ "</body>" + "</body>"
+ "</html>"); + "</html>");
...@@ -112,7 +117,7 @@ public class FactorialServlet extends HttpServlet { ...@@ -112,7 +117,7 @@ public class FactorialServlet extends HttpServlet {
+ "<body>" + "<body>"
+ "<h1>Soy un factorializador</h1>" + "<h1>Soy un factorializador</h1>"
+ "<div>" + "<div>"
+ "<form action=\"factorial-response\" >" + "<form action=\"factorial\" method=\"get\" >"
+ "Introduzca el numero: <input type=\"text\" name=number><br>" + "Introduzca el numero: <input type=\"text\" name=number><br>"
+ "<input type=\"submit\">" + "<input type=\"submit\">"
+ "</form>" + "</form>"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment