Hello, I'm trying to make a method that creates objects of a parameterized type randomly, but also to store the hashCode of different objects created and if at any time the percentage of different objects is less than 50% throw an exception.
This last part is where I've gotten stuck. I have created a population property where I store the different hashCodes and update it in the method adding the new hashCode from the new object. But I don't know how to do for to know if the percentage of different objects is less than 50%. See if someone can help me out.
package fp.tipos.apps; import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.List; import java.util.Random; public class FactoriaApps { private static Integer codigos = 0; private static Integer numCodigos = 0; public static Integer getCodigos(){ return codigos; } public static App createApp(Random rnd){ String nombre = cadenaAleatoria(rnd, 5); Double tamaņo = rnd.nextDouble() * 1024.0; Calendar fechaVersion = Calendar.getInstance(); fechaVersion.set(2000, 3, 3); Integer numInstalaciones = rnd.nextInt(2000001); Double precio = rnd.nextDouble() * 6.0; String desarrollador = cadenaAleatoria(rnd, 3); App a = new AppImpl(TipoSO.ANDROID, nombre, tamaņo, fechaVersion, numInstalaciones, precio, desarrollador); codigos = codigos + a.getSO().hashCode() + a.getDesarrollador().hashCode()*31 + a.getNombre().hashCode()*31; if(codigos < numCodigos) //here is where I don't know what to do return a; }
Regards!