Commit 2f484668 by Matias Ferreira

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

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