6 Replies - 257 Views - Last Post: 25 June 2019 - 08:23 PM Rate Topic: -----

#1 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Confused about code (Hashmaps & Hashset)

Posted 25 June 2019 - 07:12 PM

package com.example.test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Main {
    private static Map<String, HeavenlyBody> solarSystem = new HashMap<>();
    private static Set<HeavenlyBody> planets = new HashSet<>();

    public static void main(String[] args) {

        HeavenlyBody temp = new HeavenlyBody("Mercury", 88);
        solarSystem.put(temp.getName(), temp);
        planets.add(temp);

        temp = new HeavenlyBody("Venus", 225);
        solarSystem.put(temp.getName(), temp);
        planets.add(temp);

        temp = new HeavenlyBody("Earth", 365);
        solarSystem.put(temp.getName(), temp);
        planets.add(temp);

        HeavenlyBody tempMoon = new HeavenlyBody("Moon", 27);
        solarSystem.put(tempMoon.getName(), tempMoon);
        temp.addMoon(tempMoon);

        temp = new HeavenlyBody("Mars", 687);
        solarSystem.put(temp.getName(), temp);
        planets.add(temp);

        tempMoon = new HeavenlyBody("Deimos", 1.3);
        solarSystem.put(tempMoon.getName(), tempMoon);
        temp.addMoon(tempMoon); // temp is still Mars

        tempMoon = new HeavenlyBody("Phobos", 0.3);
        solarSystem.put(tempMoon.getName(), tempMoon);
        temp.addMoon(tempMoon); // temp is still Mars

        temp = new HeavenlyBody("Jupiter", 4332);
        solarSystem.put(temp.getName(), temp);
        planets.add(temp);

        tempMoon = new HeavenlyBody("Io", 1.8);
        solarSystem.put(tempMoon.getName(), tempMoon);
        temp.addMoon(tempMoon); // temp is still Jupiter

        tempMoon = new HeavenlyBody("Europa", 3.5);
        solarSystem.put(tempMoon.getName(), tempMoon);
        temp.addMoon(tempMoon); // temp is still Jupiter

        tempMoon = new HeavenlyBody("Ganymede", 7.1);
        solarSystem.put(tempMoon.getName(), tempMoon);
        temp.addMoon(tempMoon); // temp is still Jupiter

        tempMoon = new HeavenlyBody("Callisto", 16.7);
        solarSystem.put(tempMoon.getName(), tempMoon);
        temp.addMoon(tempMoon); // temp is still Jupiter

        temp = new HeavenlyBody("Saturn", 10759);
        solarSystem.put(temp.getName(), temp);
        planets.add(temp);

        temp = new HeavenlyBody("Uranus", 30660);
        solarSystem.put(temp.getName(), temp);
        planets.add(temp);

        temp = new HeavenlyBody("Neptune", 165);
        solarSystem.put(temp.getName(), temp);
        planets.add(temp);

        temp = new HeavenlyBody("Pluto", 248);
        solarSystem.put(temp.getName(), temp);
        planets.add(temp);

        System.out.println("Planets");
        for (HeavenlyBody planet : planets) {
            System.out.println("\t" + planet.getName());
        }

        HeavenlyBody body = solarSystem.get("Mars");
        System.out.println("Moons of " + body.getName());
        for (HeavenlyBody jupiterMoon : body.getSatellites()) {
            System.out.println("\t" + jupiterMoon.getName());
        }

        Set<HeavenlyBody> moons = new HashSet<>();
        for (HeavenlyBody planet : planets) {
            moons.addAll(planet.getSatellites());
        }

        System.out.println("All Moons");
        for (HeavenlyBody moon : moons) {
            System.out.println("\t" + moon.getName());
        }


    }
}









package com.example.test;

import java.util.HashSet;
import java.util.Set;

public final class HeavenlyBody {
    private final String name;
    private final double orbitalPeriod;
    private final Set<HeavenlyBody> satellites;

    public HeavenlyBody(String name, double orbitalPeriod) {
        this.name = name;
        this.orbitalPeriod = orbitalPeriod;
        this.satellites = new HashSet<>();
    }

    public String getName() {
        return name;
    }

    public double getOrbitalPeriod() {
        return orbitalPeriod;
    }

    public boolean addMoon(HeavenlyBody moon) {
        return this.satellites.add(moon);
    }

    public Set<HeavenlyBody> getSatellites() {
        return new HashSet<>(this.satellites);
    }
}






So i understand how it prints all the plants... but what i'm confused about is how it's getting the moons for mars specifically, how it's adding moons to mars, doens't make sense to me


i also understand how it prints all moons at the end, but i don't get how it's adding moons to mars specifically

Is This A Good Question/Topic? 0
  • +

Replies To: Confused about code (Hashmaps & Hashset)

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12617
  • View blog
  • Posts: 45,777
  • Joined: 27-December 08

Re: Confused about code (Hashmaps & Hashset)

Posted 25 June 2019 - 07:37 PM

Here is a segment from the code you provided where a moon is being added to Mars. The HeavenlyBody class provides the addMoon() method. Have you looked through the HeavenlyBody class, as well as the documentation for the Set interface? The code is fairly self-explanatory, and any details can really be filled in by reading through the code and documentation.

tempMoon = new HeavenlyBody("Deimos", 1.3);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Mars


Was This Post Helpful? 0
  • +
  • -

#3 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Re: Confused about code (Hashmaps & Hashset)

Posted 25 June 2019 - 07:50 PM

I understand how the moon is added, i don't understand how it's linked to mars



also not sure what this is for.....
HeavenlyBody tempMoon = new HeavenlyBody("Moon", 27);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon);


This post has been edited by ferfykins: 25 June 2019 - 07:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12617
  • View blog
  • Posts: 45,777
  • Joined: 27-December 08

Re: Confused about code (Hashmaps & Hashset)

Posted 25 June 2019 - 08:00 PM

Explain to me line-by-line what is going on in this code sample.

tempMoon = new HeavenlyBody("Deimos", 1.3);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Mars




Also, do read the code comments.

temp.addMoon(tempMoon); // temp is still Mars



Note that Mars is a HeavenlyBody object, which provides the addMoon() method.

This post has been edited by macosxnerd101: 25 June 2019 - 08:01 PM

Was This Post Helpful? 0
  • +
  • -

#5 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Re: Confused about code (Hashmaps & Hashset)

Posted 25 June 2019 - 08:16 PM

View Postmacosxnerd101, on 25 June 2019 - 08:00 PM, said:

Explain to me line-by-line what is going on in this code sample.

tempMoon = new HeavenlyBody("Deimos", 1.3);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Mars





Also, do read the code comments.

temp.addMoon(tempMoon); // temp is still Mars



Note that Mars is a HeavenlyBody object, which provides the addMoon() method.



Yeah i actually get it now i think... umm



tempMoon = new HeavenlyBody("Deimos", 1.3);
solarSystem.put(tempMoon.getName(), tempMoon);
temp.addMoon(tempMoon); // temp is still Mars



tempMoon is a new object with diemos as name/ 1.3 as orbital period
solarSystem.put puts tempmoon inside it into hashmap
temp.addMoon adds moon to temp
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12617
  • View blog
  • Posts: 45,777
  • Joined: 27-December 08

Re: Confused about code (Hashmaps & Hashset)

Posted 25 June 2019 - 08:22 PM

Correct.

Quote

temp.addMoon adds moon to temp


Note that temp is Mars in this context.
Was This Post Helpful? 0
  • +
  • -

#7 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Re: Confused about code (Hashmaps & Hashset)

Posted 25 June 2019 - 08:23 PM

View Postmacosxnerd101, on 25 June 2019 - 08:22 PM, said:

Correct.

Quote

temp.addMoon adds moon to temp


Note that temp is Mars in this context.



yeah someone renamed the stuff for me, and that's when it made sense LOL :)
ty for your help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1