Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
The solution is going to cover part three where we start setting up the game class.
We'll start by defining the scoreboard field, a scoreboard needs to monitor the score of each team.
So we're going to use a Hashmat because there's parity between team and integer data.
Private Hashmat.
The Hashmat is going to store team manager key value entries and it's going to be called scoreboard.
The next step is to create a constructor, the constructor runs as soon as we create an object of the
game class and when it runs, it will receive two parameters the home team and the away team.
Now, inside, the constructor will set the scoreboard equal to a new object of the Hashmat class.
And then I'm going to put two entries into the scoreboard.
A home team that starts with a score of zero.
And an away team that starts with a score of zero and now notice that I'm adding copies of each parameter
to the Hashmat.
That's because you don't want your keys to share the same reference as the outside variable that's being
passed in.
Otherwise, the set of each key can be affected if you change the outside variable, which is a security
leak.
You know what?
Why don't I just show you really quickly instead of passing in copies, I'll pass in each parameter
directly and I'll create a new object of the game class using the format.
I left you all and learn the parts.
OK, they're both of type team.
Now we're going to pass into a new object of the game class.
A lot of breakpoint.
Step inside.
And check it out, each key has the same reference as the outside variable that was passed in as a parameter.
If I update one of the objects, I'll change the house name for whom?
Restart the debugger.
The state of the key should get affected as well.
That's not good.
But if the constructor creates copies of each parameter, we can rest assured that each key store is
a unique reference that points to a team copy.
You can feel free to test that out yourself and monitor the references, but I'm going to move on enough
about that.
Now we want to create two getters, we'll start with the Geter get score.
Which receives one parameter.
The team who score you want to get.
We'll get the score for this particular team.
And it's going to get the score value for this particular team or will it we'll come back to this now.
We are a center that updates the score for a particular team.
It's going to be called set score and receive two parameters, the team for which you want to update
the score.
And the score itself.
Then you can say scoreboard put.
The following key value pair.
Now, we're going to quality control this center later, because put either can add this as another
entry in the hash map or it can update the entry that already contains this time object.
Obviously, we want to update because the hash map should only have two entries.
So for now, let's move on.
Now we're going to to get her called get team that receives a parameter string name.
We'll come back to this in the next video for now, just return null.
And now you might be asking, should there be a set team setter, no.
It's impossible to update a key after you add it, you can remove a key and then add another one, but
you cannot change a key.
It's not possible.
All right, we're going to wrap up this video by testing the getters and setters, I'll get the score
for Gryffindor.
I'll set the score for Gryffindor to 10.
Spoiler alert, none of these should work, so here's why I'm going out to break points.
I'll step in to get score.
The parameter we passed in has this reference which doesn't match any of the references in the hash
map.
And unless you define an equals method, Java uses the default equals method to compare to objects,
which is why I can't find anything in the Hashmat that matches the parameter and returns no.
Is that really the issue?
We'll find out soon enough.
Now, what would happen here, once again, the reference being passed and doesn't match any of the
references in the Hashmat.
So instead of updating the entry that already contains this theme object, it's going to think it's
a new key and add another entry to the hash map and that's also not good.
Java's using the default equals method to compare team objects.
So we need to provide our own equals method that allows Java to compare to objects based on their fields,
not the reference.
The equals method needs to return a boolean.
And receives an object.
What's the type of that object?
We don't know, it can be any object.
First, we're going to check if the object that we're comparing against is no, because if it happens
to be null, then there's no way it equals the object that's calling this method.
Let's assume the object, is it?
No.
Then we need to check if it isn't of type team if the object isn't an instance of the team class.
OK, so at this point, the object, is it no, it is an instance of team, so we can typecast it to
type team.
And finally, we need to check if the objects fields are equal to the current object that's calling
this method, we need to compare the house return.
This thought house equals timecode house.
We need to compare the keeper.
This storekeeper equals team goalkeeper.
And how do we compare the array, remember, we can use arrays not to string.
The new string is going to convert the contents of the array into one long string, and so if the two
happened to share the same string, then they must have the same elements.
And that's a.
And so now I guess it should work.
Let's run the code.
And it still doesn't work.
Hmm, well, test out the setar.
Same thing.
I'm pretty sure we defined our equals method perfectly.
It compares to objects based on their fields.
Those two objects, one hundred percent have the same fields.
So why isn't Hashmat able to retrieve the key?
For Hashmat to retrieve a key, it's not enough for an object to be equal to it.
It must also share the same hash code.
You're going to learn more about hash codes in the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.