Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Local Development Environments Advanced Tooling Refactoring

questions about few lines of code

Hi!, someone can explain to me how we can use the contains method to check if a song request is equal to song request?>>> we can do it because we added to the Song class and the SongRequest class an equals and hashCode methods?, if so how exactly they work because craig didnt go deep to explain the why and when we should use them.

here is the code:

 case "choose":
                        String singerName = promptForSingerName();
                        String artist = promptArtist();//prompting to the console the custom index of all the artists available.
                        Song artistSong = promptSongForArtist(artist);//prompt to the console the custom index of the songs of the artists
                        ///TODO: ADD to a song queue
                        SongRequest songRequest = new SongRequest(singerName, artistSong);
                        if (mSongQueue.contains(songRequest)) {
                            System.out.printf("%n%n Whoops %s already requested! %s %n",singerName, artistSong);
                            break;
                        }
                        mSongQueue.add(songRequest);
                        System.out.printf("You chose : %s %n", artistSong);
                        break;

another question i have is when we create a new SongRequest instance and pass in "singerName and artistSong, we pass it because the SongRequest constructor intalizes a singerName and a song? does artistSong is of type song? if it is then i understand why he pass "artistsong" as the second patameter.

I will appreciate ur help. Steven Parker Tonnie Fanadez Eric McKibbin

4 Answers

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Set.contains() method uses equals() and hashCode () methods to check equality for SongRequest Objects. This makes sure that the same singer cannot request for the same song twice. So if noob developer requests to sing Beat It by MJ his request is saved into the Karaoke Machine and prevents noob from singing Beat It by MJ again as it will be the SAME song request by same singer singing the same song.

How does equals() and hashCode () work?

Every Java class inherits the equals() and a hashcode() method from the Parent Object Class. Java Documentations say to check for equality for object we must override equals() and a hashcode() method

equals() method compares the object attributes values whether they are the same or as Craig said equals() checks for 'sameness' . Java rules state that when you override equals() you must also override hashcode().

The hashcode () returns a unique integer ID for each object. If an object’s hashcode is not the same as the object’s hashcode, there is no reason to execute the equals() method: you just know the two objects are not the same. On the other hand, if the hashcode is the same, then you must execute the equals() method to determine whether the values and fields are the same.

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Hello noob developer

SongRequest constructor initializes a singerName and a song.

To get the Song Craig calls the promptSongForArtist using the below line.

Song artistSong = promptSongForArtist(artist);

The return type for promptSongForArtist(String artist) method is a Song Object. For this case Craig names the returned Song Object as "artistSong" and uses it as a Song on SongRequest Constructor.

Hope this clarifies your second question.

Script Ninja , do you have a copy of your Karaoke code. Mine is something wrong and I don't know where the mistake is because the Karaoke Course is completely deleted and apparently no one from the staff has a copy of it...

Dinu Alexandru Hey dino this is the snapshot: https://w.trhou.se/fwo279nna7

it's in the model

thanks a lot dude! You really saved me!