Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: HashCode for calculate different objects

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Thanked 0 Times in 0 Posts

    Default HashCode for calculate different objects

    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;


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 697 Times in 679 Posts

    Default Re: HashCode for calculate different objects

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.
    the percentage of different objects is less than 50% throw an exception
    This seems like a simple if() statement:
    if ( percentDifferent < 0.5 )
        throw new Exception();
    Is it more complicated than that?

Similar Threads

  1. Trying to get values from arrays rather than hashcode
    By ACrappyDJ in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 5th, 2014, 01:32 PM
  2. what is the way to Calculate
    By ohad in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 8th, 2013, 11:29 AM
  3. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  4. Cannot calculate. Please help me....
    By safarina02 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2011, 01:11 PM
  5. What are HashCode and equals in Java and thay are implement in java?
    By diyaots in forum Java Theory & Questions
    Replies: 2
    Last Post: September 9th, 2011, 08:38 AM