28 octubre 2013

rectngulo

import java.awt.geom.Point2D;
import java.util.Scanner;

/**
 * @author Nestor Fico Cuchuyrume Mamani
 * @version 1.0 Creada 23 de Octubre del 2013.
 */
public class DemoRect {

public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);

double x,y,x1,y1;

System.out.print("Ingrese primera esquina del 1er rectangulo: ");
x=sc.nextDouble();
y=sc.nextDouble();
System.out.print("Ingrese segunda esquina del 1er rectangulo: ");
x1=sc.nextDouble();
y1=sc.nextDouble();

Point2D.Double p1 = new Point2D.Double(min(x,x1),min(y,y1));
Point2D.Double p2 = new Point2D.Double(max(x,x1),max(y,y1));




System.out.print("Ingrese primera esquina del 2er rectangulo: ");
x=sc.nextDouble();
y=sc.nextDouble();
System.out.print("Ingrese segunda esquina del 2er rectangulo: ");
x1=sc.nextDouble();
y1=sc.nextDouble();

Point2D.Double p3 = new Point2D.Double(min(x,x1),min(y,y1));
Point2D.Double p4 = new Point2D.Double(max(x,x1),max(y,y1));


MiRectangulo rectangulo1=new MiRectangulo(p1,p2);
MiRectangulo rectangulo2=new MiRectangulo(p3,p4);

System.out.println("Rectanulo A = "+rectangulo1);
System.out.println("Rectanulo B = "+rectangulo2);

rectangulo2.CasoRectangulo(rectangulo1);

}

/**
* Para determinar los puntos inferiores
* @param x double
* @param x1 double
* @return regresa menor valor
*/
public static  double min(double x,double x1){
if(x return x;
else
return x1;
}
/**
* Para determinar los puntos superiores
* @param x double
* @param x1 double
* @return regresa mayor valor
*/
public static  double max(double x,double x1){
if(x return x1;
else
return x;
}
}



////////////////////////////////////////////////////////////////////////////////////////


import java.awt.geom.Point2D;


public class MiRectangulo {
/**
* puntoInferiorIzq representara las coordenadas del
* punto inferior izquierdo del rectangulo.
* puntoSuperiorDer representara las coordenadas del
* punto superior derecho del rectangulo.
*/
private Point2D.Double puntoInferiorIzq;
private Point2D.Double puntoSuperiorDer;

/**
* constructor de la clase para inicializar directamente
* con dos puntos con sus respectivas coordenadas
* @param pt1 double
* @param pt2 double
*/
MiRectangulo(Point2D.Double pt1, Point2D.Double pt2) {
setPuntoInferiorIzq(pt1);
setPuntoSuperiorDer(pt2);
}

/**
* Asigna valores deseados a punto inferior izquierda
* @param pt con cordenaas x e y
*/
public void setPuntoInferiorIzq(Point2D.Double pt) {

puntoInferiorIzq=new Point2D.Double(pt.x,pt.y);

}
/**
* Asigna valores deseados a punto superior derecha
* @param pt con cordenaas x e y
*/
public void setPuntoSuperiorDer(Point2D.Double pt) {

puntoSuperiorDer=new Point2D.Double(pt.x,pt.y);
}
/**
* @return punto inferior izquierda del rectangulo
*/
public Point2D.Double getPuntoInferiorIzq() {

return puntoInferiorIzq;
}
/**
* @return punto superior derecha del rectangulo
*/
public Point2D.Double getPuntoSuperiorDer() {

return puntoSuperiorDer;
}
/**arr toma valores de los x e y de los puntos
* para hallar el area y verificar casos
* @param rx, rectangulo x para comparar con el rectangulo 1
* El for que se muestra ordena los valores del array en cada fila
* xx es la suma de las longitudes horizontales de los rectangulos
* yy es la suma de las longitudes verticales de los rectangulos
*/
public void CasoRectangulo(MiRectangulo rx)
{
int i,j,k;
double x;
double [][] arr = {
{rx.puntoInferiorIzq.x,rx.puntoSuperiorDer.x,puntoInferiorIzq.x,puntoSuperiorDer.x},
{rx.puntoInferiorIzq.y,rx.puntoSuperiorDer.y,puntoInferiorIzq.y,puntoSuperiorDer.y},
                  };

for(k=0; k      for (i=1; i            j=i;        
           while (j>0 && arr[k][j-1]>arr[k][j]){
           
                 x=arr[k][j];
                 arr[k][j]=arr[k][j-1];
                 arr[k][j-1]=x;
                 j--;
           }
     }
}

double area=Math.abs((arr[0][2]-arr[0][1])*(arr[1][2]-arr[1][1]));

double xx=Math.abs(puntoSuperiorDer.x-puntoInferiorIzq.x)+Math.abs(rx.puntoSuperiorDer.x-rx.puntoInferiorIzq.x);
double yy=Math.abs(puntoSuperiorDer.y-puntoInferiorIzq.y)+Math.abs(rx.puntoSuperiorDer.y-rx.puntoInferiorIzq.y);

if(xx
System.out.println("Rectanguloa A y B son disjuntos.");
}
else{
if(area==0){

System.out.println("Rectanguloa A y B se tocan.");
        }
else{
System.out.println("Rectanguloa A y B se sobreponen.");
System.out.println("Area sobrepuesta = "+Math.abs(area));
}

}
}


//Muestra las coordenada como: ([1.0, 2.0], [23.0, 4.0])
public String toString(){

return "(["+puntoInferiorIzq.x+", "+puntoInferiorIzq.y+"], ["+puntoSuperiorDer.x+", "+puntoSuperiorDer.y+"])";
}

}

14 octubre 2013

master mind

/**
 *
 * Fundamentos de Programacion 2: CuatroEsferas.java
 *
 */



class CuatroEsferas {
static  int MENOS =0;
   // Datos miembro
   private Esfera esfera1;
   private Esfera esfera2;
   private Esfera esfera3;
   private Esfera esfera4;

   // Constructores
 
   public CuatroEsferas() {
     esfera1 = new Esfera('R');
esfera2 = new Esfera('R');
esfera3 = new Esfera('R');
esfera4 = new Esfera('R');
   }

   public CuatroEsferas(CuatroEsferas cuatroEsferas) {
      esfera1 =  new Esfera(cuatroEsferas.esfera1);
      esfera2 =  new Esfera(cuatroEsferas.esfera2);
      esfera3 =  new Esfera(cuatroEsferas.esfera3);
      esfera4 =  new Esfera(cuatroEsferas.esfera4);
   }

   public void CuatroPegs(Esfera e1, Esfera e2, Esfera e3, Esfera e4){
      esfera1 = e1;
      esfera2 = e2;
      esfera3 = e3;
      esfera4 = e4;
   }

//-------------------------------------------------
//      Metodos publicos
//          void setColor ( int, char );
//          char getColor ( int );
//          String toString ( );
//          int contandotPseudoHits ( CuastroEsferas );
//          int contandotHits ( CuatroEsferas );
//------------------------------------------------
 
   // Cambia el color del peg seleccionado
   public void setColor (int queColor, char elColor) {
 
       switch (queColor) {
         case 1: esfera1.setColor(elColor);
          break;
         case 2: esfera2.setColor(elColor);
        break;
         case 3: esfera3.setColor(elColor);
        break;
         case 4: esfera4.setColor(elColor);
        break;
         default: System.err.println("No hay tal color!");
                  System.exit(1);
       }
   }




   // Retorna el color de la esfera seleccionada
   public char getColor (int queColor) {
       char color = ' ';
       switch (queColor) {
         case 1: color = esfera1.getColor();
break;
         case 2: color = esfera2.getColor();
break;
         case 3: color = esfera3.getColor();
break;
         case 4: color = esfera4.getColor();
break;
         default: System.err.println("No hay tal color!g");
                  System.exit(2);
       }
       return color;
   }

   // Retorna la representacion  string del objeto CuatroEsferas

   public String toString (){
       // este es un stub (recuerde que un stub es un metodo incompleto)
       return esfera1.getColor()+" "+esfera2.getColor()+" "+esfera3.getColor()+" "+esfera4.getColor();
   }



   // Retorna el numero de pseudoHits entre el objeto y la suposición

   public int contandoPseudoHits (CuatroEsferas esferaAdivina) {
       // este es un stub (recuerde que un stub es un metodo incompleto)
 
 
  int phit=0,i=0,j=0,C=4;
 // int i=0,j=0;
  //int C=4;
  char f[]={getColor(1),getColor(2),getColor(3),getColor(4)};
 
  for(i=0;i   {
  if(f[i]==esferaAdivina.getColor(i+1))
  {
  f[i]='*';
  esferaAdivina.setColor(i+1, '*');
  }
  }
 
  for(i=1;i   {
  for(j=1;j   {
  if(f[i-1]==esferaAdivina.getColor(j)&&f[i-1]!='*')
  {
  phit++;
  esferaAdivina.setColor(j, '*');
 break;
  }
  }
  }


       return phit;
     
     
   }


   // Retorna el numero de hits entre el objeto y la suposicion

   public int contandoHits (CuatroEsferas esferaAdivina) {
       // este es un stub (recuerde que un stub es un metodo incompleto)
 
  int hit=0,i=0,C=4;
 
  for(i=1; i   {
  if(esferaAdivina.getColor(i)==getColor(i))
  {
  hit++;
      }
  }
 
       return hit;
   }

}



     
       //////////////////////////////////////////////////////////////////7

/**
 * Definicion de los colores
 * Esta clase esta completa y NO DEBE SER MODIFICADA
 */

class Esfera {
 
   // Dato miembro
   private char color;

   // Constructores
   public Esfera() {
      color = 'R';
   }

   public Esfera(Esfera e) {
      color = e.getColor();
   }

   public Esfera(char c) {
      color = c;
   }

//-------------------------------------------------
//      Metodos Publicos:
//          void setColor ( char );
//          char getColor ( );
//          String toString ( );
//------------------------------------------------
 
   // Cambia el color de la esfera
   public void setColor (char c) {
       color = c;
   }

   // Retorna el color de la esfera
   public char getColor () {
       return color;
   }

   // Retorna la representacion  string del ojbeto Esfera
   public String toString () {
       return "" + getColor();
   }

}


////////////////////////////////////////////////////////////////////////

import java.text.DecimalFormat;
import java.util.Scanner;


public class MenteMestra {

public static void main(String[] arg)
{
/** df 01,02...10
* hit guarda hit
* cdx=cadenax=cadena ingresada
* cd=cadena clave
* cdg=cadena guardada (ingresada)
* OkSize para verificar correcto numero de char
* EsferasIni para inicialiar cdx con valores ingresados (cdg)
*/
DecimalFormat df = new DecimalFormat("00");

int i=0,hit;
String cdg="";
CuatroEsferas cdx = new CuatroEsferas();
CuatroEsferas cd = new CuatroEsferas();

cd.setColor(1,'M');
cd.setColor(2,'L');
cd.setColor(3,'A');
cd.setColor(4,'R');


System.out.println("\t\tCodgo secreto: "+cd);
do{

   cdg=OkSize(cdg);
   EsferasIni(cdg,cdx);
   hit=cd.contandoHits(cdx);

   System.out.println("\t\tSuposicion #"+df.format((i+1))+": "+cdx+"   Hits: "+hit+"; PseudoHits = "+cd.contandoPseudoHits(cdx));

        if(hit==4){

       System.out.print("Felicitaciones! \nEspero que hayas disfrutado el juego. Adios!");
       break;
        }

i++;

}while(i<10 p="">
if(hit!=4)
{
System.out.println();
System.out.println("Lo siento, no lo logró. El código secreto es "+cd);
System.out.println("Espero que hayas disfrutado el juego. Adios!");
}


}


public static String OkSize(String s)
{
Scanner sc=new Scanner(System.in);

do{
System.out.print("Ingrese su suposcion: ");
s=sc.nextLine();

}while(s.length()!=4);

return s;
}


public static CuatroEsferas EsferasIni(String s, CuatroEsferas e4)
{
int i=0;
for(i=0;i {
e4.setColor(i+1, s.charAt(i));
}
return e4;
}

}