• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap implementation

 
Greenhorn
Posts: 11
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
muac(cm) const.A  Age(yrs) const.B  Hip  (cm) constant C   Wrist (cm)             Constan D
20.5     1.096     17       0.0086  79          0.0957           13                   0.819
21    1.0954       18       0.0091  79.5  0.0963          13.2           0.0832
(see the above table in the attachment)

The above is part of my research data. it is for calculating %Body Fat for females....
so one is going to supply muac, age,hip and wrist...
the program then will pick the corresponding constant for each parameter
i want to store all the data in a hashmap
then i will output on request.
i want to store each parameter and its corresponding constant, so if a user inputs their measurements i have to iterate over the stored values and pick corresponding constants for each
parameter input . then i use the constants to calculate Body Density then % Body fat using some formula.
//my main




my problem:
how is hashmap used to implement the rest of the program.

 
femaleBD.PNG
[Thumbnail for femaleBD.PNG]
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I seem to miss what the question/problem is.

Two remarks about your code:
  • Fields: age, muac, hip, wrist should be private
  • Your equals method breaks the contract. An object should always be equal to itself.
  •  
    michael lenasalon
    Greenhorn
    Posts: 11
    Netbeans IDE MySQL Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paweł Baczyński wrote:I seem to miss what the question/problem is.

    Two remarks about your code:

  • Fields: age, muac, hip, wrist should be private
  • Your equals method breaks the contract. An object should always be equal to itself.


  • my problem is how to stores the above table in a hashmap then on supply of the pramaters i iterate over the hashmap and pick
    the constants accordingly
     
    Ranch Hand
    Posts: 180
    1
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    After fixing the code as per Pawel's suggestions, you can just use Female objects as keys to the map, with constants as values, like this:

     
    Bartender
    Posts: 5465
    212
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @michael,

    in your opening post, you have the arm, hip and wrist as non-integers, while these are defined as integers in your Female class.
    What are they? And, for instance, does constant B (related to age?) also depend on arm, wrist and hip?
     
    michael lenasalon
    Greenhorn
    Posts: 11
    Netbeans IDE MySQL Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piet Souris wrote:@michael,

    in your opening post, you have the arm, hip and wrist as non-integers, while these are defined as integers in your Female class.
    What are they? And, for instance, does constant B (related to age?) also depend on arm, wrist and hip?


    I have realised that they are double , Wil change once on my PC , for parameters arm, wrist, hip and age there corresponding constants does not depend on other parameter constants ...
     
    Piet Souris
    Bartender
    Posts: 5465
    212
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay, but then there is no reason for creating a HashMap with a Female as key.
    Just create four HashMaps, with keys the arm, wrist, hip and age, and values
    the respective constants.

    It is not advisable to use a double as key, so try to map these doubles to
    integers (for instance by measuring in mm in stead of cm).
    Your BodyFat class then only needs a method that calculates the body fat, given
    the four variables.

    You could store all your measurements in a Female class, as already mentioned,
    to be able to calculate all desired statistics.
     
    michael lenasalon
    Greenhorn
    Posts: 11
    Netbeans IDE MySQL Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piet Souris wrote:Okay, but then there is no reason for creating a HashMap with a Female as key.
    Just create four HashMaps, with keys the arm, wrist, hip and age, and values
    the respective constants.

    It is not advisable to use a double as key, so try to map these doubles to
    integers (for instance by measuring in mm in stead of cm).
    Your BodyFat class then only needs a method that calculates the body fat, given
    the four variables.

    You could store all your measurements in a Female class, as already mentioned,
    to be able to calculate all desired statistics.





    what next? kindly help on how i can take the stored values in Female class and compare with the hashmap stored key value.



     
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry, I have come to this thread late and not noticed it before.

    Piet Souris wrote:. . . Just create four HashMaps, with keys the arm, wrist, hip and age, and values the respective constants.

    Please explain. That looks a bit like parallel arrays. You should have some sort of object which encapsulates those four data. I think we already have one of them. The difficult part is finding the appropriate thing to use as a “K” in the Map.

    It is not advisable to use a double as key, . . .

    Why not? A double will be boxed to a Double, and that is an immutable object. The problem I can see is that you will try something like
    myMap.put(1.23, xxx)
    and then not remember which “K” you used.
    myMap.get1.2299999999999) won't find xxx. When you said to use an integer, did you mean that as an ID number to use as the “K”?

    michael lenasalon, your hash code and equals methods are implemented incorrectly. All right, the hash code method might satisfy the letter of the law about hash codes, but it most certainly does not satisfy the spirit of the law. That equals method does not even satisfy the letter of the law about equals because it is not even reflexive. Every object is equal to itself but your method does not even recognise that. The hash code method is easy to override and equals is notoriously difficult to override correctly; I suggest that you delete both those methods for the time being. Remember those methods will ensure that you can add a Female object to a hash‑based Collection and never find it back.

    Why did you say you want to add things to a Map? What do you know about Maps? Have you ever used a Map? The answer to the latter question is probably yes; you probably have a paper address book somewhere with names in alphabetical order, and that behaves like a Map<Name, AddressAndPhoneNumber>.
     
    Master Rancher
    Posts: 4796
    72
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Piet Souris wrote:It is not advisable to use a double as key, . . .

    Why not? A double will be boxed to a Double, and that is an immutable object. The problem I can see is that you will try something like
    myMap.put(1.23, xxx)
    and then not remember which “K” you used.
    myMap.get1.2299999999999) won't find xxx.


    That is exactly the problem.  Depending how these key are obtained, they may well have rounding errors, making them unreliable keys.  There are various possible workarounds.  You could switch to a String representation of the double, enforcing precise rounding rules consistently.  Or you could use a TreeMap or other NavigableMap instance, and use methods like floorKey() and ceilingKey() to find the two (or one, or zero) closest keys, and see if either one is "close enough", however you might choose to define that.
     
    michael lenasalon
    Greenhorn
    Posts: 11
    Netbeans IDE MySQL Database Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    someone who might have the best and possible solution for this problem?.
    i want it done because it is important for my learning and also for the app to work.
    it is a small app it can help people.
     
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Apart from all the suggestions given above, I have a different perspective on this.
    Why are you using a HashMap to store a multi - dimensions based data ?

    A HashMap allows you to store a set of data in a key-value pair format. Based on your inputs so far, I feel that you just want a set of inputs and give the output based on some formula.
    If you are going to base your output based on statistical records, its quite vague, but how will the application behave if the user enters a value thats not in your list ?
     
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:Apart from all the suggestions given above, I have a different perspective on this.
    Why are you using a HashMap to store a multi - dimensions based data ?

    A HashMap allows you to store a set of data in a key-value pair format. Based on your inputs so far, I feel that you just want a set of inputs and give the output based on some formula.
    If you are going to base your output based on statistical records, its quite vague, but how will the application behave if the user enters a value thats not in your list ?

    Exactly. Why not have just a List<Female> ?
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:Exactly. Why not have just a List<Female> ?


    Or indeed, a List<Person>, and a Gender enum that encapsulates any differences in BMI calculation.

    @michael: If you're worrying about hashmaps at this stage, you're falling into the common trap of "premature implementation". Work out what you need to do first; there'll be plenty of time to decide how to do it later.

    HIH

    Winston
     
    salvin francis
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:.. that encapsulates any differences in BMI calculation.



    I think you mean %Body Fat which is what OP is going for
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic