lunes, 9 de febrero de 2015

Clase monitor con uso en un array para buscar repeticiones

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package preparacion4;
/**
*
* @author juan
*/
public class Monitor {
String Fabricante;
int Pulgadas;
int ResolucionH;
int ResolucionV;
String Modelo;
public Monitor() {
}

public Monitor(String Fabricante, int Pulgadas, int ResolucionH, int ResolucionV, String Modelo) {
this.Fabricante = Fabricante;
this.Pulgadas = Pulgadas;
this.ResolucionH = ResolucionH;
this.ResolucionV = ResolucionV;
this.Modelo = Modelo;
}
public Monitor(String Modelo) {
this.Modelo = Modelo;
}

public String getFabricante() {
return Fabricante;
}
public void setFabricante(String Fabricante) {
this.Fabricante = Fabricante;
}
public int getPulgadas() {
return Pulgadas;
}
public void setPulgadas(int Pulgadas) {
this.Pulgadas = Pulgadas;
}
public int getResolucionH() {
return ResolucionH;
}
public void setResolucionH(int ResolucionH) {
this.ResolucionH = ResolucionH;
}
public int getResolucionV() {
return ResolucionV;
}
public void setResolucionV(int ResolucionV) {
this.ResolucionV = ResolucionV;
}
public String getModelo() {
return Modelo;
}
public void setModelo(String Modelo) {
this.Modelo = Modelo;
}

public boolean comprobarModelo(String Modelo)
{
return Modelo.equalsIgnoreCase(this.Modelo);
}


}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package preparacion4;
import java.util.Random;
/**
*
* @author juan
*/
public class Preparacion4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Monitor [] Monitores = new Monitor[1000] ;
String [] comprobados = new String[Monitores.length];
String [] pre ={"uno","dos","tres","cuatro","cinco"};
Random al = new Random();
int numVeces,posMayor,cont,ultString;
boolean encontrado;

for (int i = 0; i < Monitores.length; i++) {
Monitores[i]= new Monitor(pre[al.nextInt(5)]);
}

numVeces=posMayor=ultString=0;
for (int i = 0; i < Monitores.length; i++) {
cont=0;
encontrado=false;
while (cont if (Monitores[i].comprobarModelo(comprobados[cont])) encontrado=true;
else cont++;
}
if (!encontrado)
{
comprobados[ultString]=Monitores[i].getModelo();
++ultString;
cont=0;
for (int j = i; j < Monitores.length; j++) {
if (Monitores[j].comprobarModelo(Monitores[i].getModelo())) ++cont;
}
if (cont>numVeces)
{
numVeces=cont;
posMayor=i;
}
}
}
System.out.println("El que aparece más veces es "+ Monitores[posMayor].getModelo()+ " y aparece "+ numVeces + " veces");


}

}

} } System.out.println("El que aparece más veces es "+ Monitores[posMayor].getModelo()+ " y aparece "+ numVeces + " veces"); } }

No hay comentarios: