Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CuadradoMagico
package cuadrado;
public class CuadradoMagico {
private int[][] m;
public CuadradoMagico(int[][] digitos){
this.m = digitos;
}
// Public Functions
public boolean validate(){
boolean validation = false;
if(this.validateValues()){
int d = sumDiagonal();
int v = sumVert();
int h = sumHoriz();
if( d==v && v==h && d==h)
validation = true;
}
return validation;
}
// Private Functions
private boolean validateValues(){
boolean validation = true;
int e = 0;
//for each element
for(int a=0; a<m.length; a++){
for(int b=0; b<m[0].length; b++){
e = m[a][b];
//exit false if out of range
if(e > 9 || e < 1){ System.out.println("validateValues() : Valor fuera de rango 1-9");return false; };
//check against the others
for(int i=0; i<m.length; i++){
for(int j=0; j<m[0].length; j++){
if(a!=i && b!=j){
if(m[i][j] == e){
validation = false;
System.out.println("validateValues() : Valor repetido");
}
}
}
}
//----
}//end for b
}//end for a
return validation;
}
private int sumDiagonal(){
int result = -1;
int d1 = 0;
int d2 = 0;
for(int i=0; i<m.length; i++){
d1 += m[i][i];
}
for(int i= m.length-1; i >= 0; i--){
d2 += m[i][i];
}
if(d1 == d2)
result = d1;
return result;
}
private int sumHoriz(){
boolean equal = true;
int[] res = new int[m.length];
for(int i=0; i<m.length; i++){
int sum = 0;
for(int j=0; j<m.length; j++){
sum += m[i][j];
}
res[i] = sum;
}
for(int i=0; i<res.length-1; i++){
if(res[i] != res[i+1]){
equal = false;
}
}
if(!equal){res[0] = -2;};
return res[0];
}
private int sumVert(){
boolean equal = true;
int[] res = new int[m.length];
for(int i=0; i<m.length; i++){
int sum = 0;
for(int j=0; j<m.length; j++){
sum += m[j][i];
}
res[i] = sum;
}
for(int i=0; i<res.length-1; i++){
if(res[i] != res[i+1]){
equal = false;
}
}
if(!equal){res[0] = -3;};
return res[0];
}
}
package cuadradoServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cuadrado.CuadradoMagico;
/**
* Servlet implementation class Cuadrado
*/
@WebServlet("/CuadradoMagicoServlet")
public class CuadradoMagicoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CuadradoMagicoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Parse params
int[][] m = new int[3][3];
int c=1;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
String s = request.getParameter(Integer.toString(c));
int n = 0;
if(s.matches("-?\\d+(\\.\\d+)?")){
n = Integer.parseInt(s);
}
m[i][j] = n;
c++;
}
}
//System.out.println(java.util.Arrays.toString(m));
CuadradoMagico cuadrado = new CuadradoMagico(m);
if(cuadrado.validate()){
request.setAttribute("result", "<p class='success'>Cuadrado mágico válido</p>");
}
else{
request.setAttribute("result", "<p class='invalid'>Cuadrado mágico inválido</p>");
}
request.getRequestDispatcher("/Formulario.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.