Skip to content

Commit a0f4e68

Browse files
authored
Typos & grammar (8-3)
Fixed spelling mistakes and awkward wordings. Commented out Finnish where showing.
1 parent 27da383 commit a0f4e68

1 file changed

Lines changed: 35 additions & 34 deletions

File tree

data/part-8/3-similarity-of-objects.md

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ hidden: false
2121

2222
</text-box>
2323

24-
Kerrataan seuraavaksi olioiden vertailuun käytettyä metodia `equals` ja tutustutaan suurpiirteiseen vertailuun käytettyyn metodiin `hashCode`.
24+
<!-- Kerrataan seuraavaksi olioiden vertailuun käytettyä metodia `equals` ja tutustutaan suurpiirteiseen vertailuun käytettyyn metodiin `hashCode`. -->
2525

26-
Let's revise the `equals` method used to compare object, and become familiar with the `hashCode` method used in making approximate comparisons.
26+
Let's revise the `equals` method used to compare objects, and become familiar with the `hashCode` method used in making approximate comparisons.
2727

2828
<!-- ## Samuudesta kertova metodi "equals" -->
2929

@@ -33,7 +33,7 @@ Let's revise the `equals` method used to compare object, and become familiar wit
3333
3434
Tämä selvenee seuraavalla esimerkillä. Luokassa `Kirja` ei ole omaa `equals`-metodin toteutusta, joten se käyttää Javan tarjoamaa oletustoteutusta. -->
3535

36-
The equals <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object" target="_blank"> method </a> > checks by default whether the object given as a parameter has the same reference as the object its being compared to. In other words, the default behaviour checks whether the two objects are the same. If the reference is the same, the method returns `true`, and `false` otherwise.
36+
The <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object" target="_blank">equals method </a> checks by default whether the object given as a parameter has the same reference as the object it is being compared to. In other words, the default behaviour checks whether the two objects are the same. If the reference is the same, the method returns `true`, and `false` otherwise.
3737

3838
This can be illustrated with the following example. The `Book` class does not have its own implementation of the `equals` method, so it falls back on the default implementation provided by Java.
3939

@@ -62,18 +62,18 @@ Book bookObject = new Book("Book object", 2000, "...");
6262
Book anotherBookObject = bookObject;
6363

6464
if (bookObject.equals(anotherBookObject)) {
65-
System.out.println("The books were the same");
65+
System.out.println("The books are the same");
6666
} else {
67-
System.out.println("The books weren't the same");
67+
System.out.println("The books aren't the same");
6868
}
6969

7070
// we now create an object with the same content that's nonetheless its own object
7171
anotherBookObject = new Book("Book object", 2000, "...");
7272

7373
if (bookObject.equals(anotherBookObject)) {
74-
System.out.println("The books were the same");
74+
System.out.println("The books are the same");
7575
} else {
76-
System.out.println("The books weren't the same");
76+
System.out.println("The books aren't the same");
7777
}
7878
```
7979

@@ -85,8 +85,8 @@ Kirjat eivät olleet samat
8585
</sample-output> -->
8686
<sample-output>
8787

88-
The books were the same
89-
The books weren't the same
88+
The books are the same
89+
The books aren't the same
9090

9191
</sample-output>
9292

@@ -96,7 +96,7 @@ Merkkijonojen eli Stringien yhteydessä `equals` toimii odotetulla tavalla, eli
9696
9797
Mikäli haluamme, että omien luokkiemme vertailu onnistuu `equals`-metodilla, tulee metodi määritellä luokkaan. Luotava metodi saa parametrina `Object`-tyyppisen viitteen, joka voi olla mikä tahansa olio. Vertailussa tarkastellaan ensin viitettä. Tätä seuraa parametrin olion tyypin tarkastelu `instanceof`-operaatiolla -- mikäli olion tyyppi ei vastaa luokkamme tyyppiä, olio ei voi olla sama. Tämän jälkeen oliosta luodaan luokkamme tyyppinen versio, jonka jälkeen oliomuuttujia verrataan toisiinsa. -->
9898

99-
The internal structure of the book objects (i.e., the values of their instance variables ) in the previous example is the same, but only the first comparison prints "`The books were the same "`. This is because the references are the same in the first case, i.e., the object is compared to itself. The second comparison is about two different entities, even though the variables have the same values.
99+
The internal structure of the book objects (i.e., the values of their instance variables ) in the previous example is the same, but only the first comparison prints "`The books are the same`". This is because the references are the same in the first case, i.e., the object is compared to itself. The second comparison is about two different entities, even though the variables have the same values.
100100

101101
For strings, `equals` works as expected in that it declares two strings _identical in content_ to be 'equal' even if they are two separate objects. The String class has replaced the default `equals` with its own implementation.
102102

@@ -151,14 +151,14 @@ public boolean equals(Object comparedObject) {
151151
return true;
152152
}
153153

154-
// otherwise, the object's aren't the same
154+
// otherwise, the objects aren't the same
155155
return false;
156156
}
157157
```
158158

159159
<!-- Alla `Kirja`-luokka kokonaisuudessaan. -->
160160

161-
The `Book`-class in it's entirety.
161+
The `Book` class in its entirety.
162162

163163
<!-- ```java
164164
public class Kirja {
@@ -292,7 +292,7 @@ public class Book {
292292
return true;
293293
}
294294

295-
// otherwise, the object's aren't the same
295+
// otherwise, the objects aren't the same
296296
return false;
297297
}
298298
}
@@ -318,9 +318,9 @@ Kirja bookObject = new Kirja("Book Object", 2000, "...");
318318
Kirja anotherBookObject = new Kirja("Book Object", 2000, "...");
319319

320320
if (bookObject.equals(anotherBookObject)) {
321-
System.out.println("The books were the same");
321+
System.out.println("The books are the same");
322322
} else {
323-
System.out.println("The books weren't the same");
323+
System.out.println("The books aren't the same");
324324
}
325325
```
326326

@@ -331,15 +331,15 @@ Kirjat olivat samat
331331
</sample-output> -->
332332
<sample-output>
333333

334-
The books were the same
334+
The books are the same
335335

336336
</sample-output>
337337

338338
<quiz id='8e476233-b04d-53cd-8470-aae57279f9e2'></quiz>
339339

340340
<!-- Myös ArrayList käyttää equals-metodia osana sisäistä toteutustaan. Mikäli emme toteuta omissa olioissamme `equals`-metodia, ei ArrayListin tarjoama `contains`-metodi toimi oikein. Mikäli kokeilet alla olevaa koodia kahdella Kirja-luokalla, jossa toisessa on määritelty `equals`-metodi, ja toisessa ei, huomaat eron. -->
341341

342-
The ArrayList also uses the equals method in its internal implementation. If we don't define the `equals` method in our objects, the `contains` method of the ArrayList does not work properly. If you try out the code below with two Book classes, one with the `equals` method defined, and another without it, you'll see the difference.
342+
The ArrayList also uses the `equals` method in its internal implementation. If we don't define the `equals` method in our objects, the `contains` method of the ArrayList does not work properly. If you try out the code below with two Book classes, one with the `equals` method defined and another without it, you'll see the difference.
343343

344344
<!-- ```java
345345
ArrayList<Kirja> kirjat = new ArrayList<>();
@@ -435,9 +435,9 @@ Olemme aiemmin käyttäneet `String`-olioita menestyksekkäästi HashMapin avaim
435435

436436
We find the borrower when searching for the same object that was given as a key to the hash map's `put` method. However, when searching by the exact same book but with a different object,a borrwer isn't found, and we get the _null_ reference instead. The reason lies in the default implementation of the `hashCode` method in the `Object` class. The default implementation creates a `hashCode` value based on the object's reference, which means that books having the same content that are nonetheless different objects get different results from the hashCode method. As such, the object is not being searched for in the right place.
437437

438-
For the HashMap to work in thw way we want it to, that is, to return the borrower when given an object with the correct _content_ (not necessarily the same object as the original key), the class that's the key must overwrite the `hashCode` method in addition to the `equals` method. The method must be overwritten so that it gives the same numerical result for all objects with the same content. Also, some objects with different contents may get the same result from the hashCode method. However, with the HashMap's performance in mind, it is essential that objects with different contents get the same hash value as rarely as possible.
438+
For the HashMap to work in the way we want it to, that is, to return the borrower when given an object with the correct _content_ (not necessarily the same object as the original key), the class that the key belongs to must overwrite the `hashCode` method in addition to the `equals` method. The method must be overwritten so that it gives the same numerical result for all objects with the same content. Also, some objects with different contents may get the same result from the hashCode method. However, with the HashMap's performance in mind, it is essential that objects with different contents get the same hash value as rarely as possible.
439439

440-
We've previously used `String` objects as HashMap keys, so we can deduce that the`String` class has a well-functioning `hashCode` implementation of its own. We'll _Delegate_, i.e., transfer the computational responsibility to the `String` object.
440+
We've previously used `String` objects as HashMap keys, so we can deduce that the `String` class has a well-functioning `hashCode` implementation of its own. We'll _delegate_, i.e., transfer the computational responsibility to the `String` object.
441441

442442
<!-- ```java
443443
public int hashCode() {
@@ -453,7 +453,7 @@ public int hashCode() {
453453

454454
<!-- Yllä oleva ratkaisu on melko hyvä, mutta jos `nimi` on _null_, näemme `NullPointerException`-virheen. Korjataan tämä vielä määrittelemällä ehto: jos `nimi`-muuttujan arvo on _null_, palautetaan hajautusarvoksi julkaisuvuosi. -->
455455

456-
The above solution is quite good. However, if `name` is _null_, we see a `NullPointerException` error. Let'fix this by defining a condition: if the value of the `name` variable is _null_, we'll return the year of publication as the hash value.
456+
The above solution is quite good. However, if `name` is _null_, we see a `NullPointerException` error. Let's fix this by defining a condition: if the value of the `name` variable is _null_, we'll return the year of publication as the hash value.
457457

458458
<!-- ```java
459459
public int hashCode() {
@@ -475,7 +475,7 @@ public int hashCode() {
475475
}
476476
```
477477

478-
Now, all of the books that share a name are bundleded into one group. Let's improve it further so that the year of publiciation is also taken into account in the hash value calculation that's based on the book title.
478+
Now, all of the books that share a name are bundled into one group. Let's improve it further so that the year of publication is also taken into account in the hash value calculation that's based on the book title.
479479

480480
<!-- ```java
481481
public int hashCode() {
@@ -525,7 +525,8 @@ System.out.println(borrowers.get(new Book("Book Object", 2000, "...")));
525525
System.out.println(borrowers.get(new Book("Test Driven Development", 1999)));
526526
```
527527

528-
Tulostuu:
528+
<!--Tulostuu:-->
529+
Output:
529530

530531
<sample-output>
531532

@@ -540,10 +541,10 @@ Arto
540541
- metodi `equals` siten, että kaikki samansuuruisena (tai saman sisältöisinä) ajatellut oliot tuottavat vertailussa tuloksen true ja muut false
541542
- metodi `hashCode` siten, että mahdollisimman harvalla erisuuruisella oliolla on sama hajautusarvo -->
542543

543-
**Let's revise the ideas once more:** for a class to be used as a HashMap's key, we need to define for it:
544+
**Let's review the ideas once more:** for a class to be used as a HashMap's key, we need to define for it:
544545

545-
- method `equals` , so that all equal or apporiximately equal objects cause the comparison to return true and all false for all the rest
546-
- metho `hashCode` , so that as few objects as possible end up with the same hash value
546+
- the `equals` method, so that all equal or approximately equal objects cause the comparison to return true and all false for all the rest
547+
- the `hashCode` method, so that as few objects as possible end up with the same hash value
547548

548549
<!-- <text-box variant='hint' name='Metodien equals ja hashCode avustettu luominen'>
549550
@@ -559,11 +560,11 @@ NetBeans tarjoaa tuen metodien `equals` ja `hashCode` avustettuun luomisen. Voit
559560
Käytä NetBeansin avustettua equals- ja hashCode-metodien luomista kunnes tiedät, että omat metodisi ovat varmasti paremmat kuin NetBeansin automaattisesti luomat metodit.
560561
561562
</text-box> -->
562-
<text-box variant='hint' name='Assited creation of the equals method and hashCode '>
563+
<text-box variant='hint' name='Assisted creation of the equals method and hashCode '>
563564

564-
NetBeans provides support for the creation of both `equals` and `hashCode`. You can select Source -> Insert Code from the menu and then select _equals() and hashCode() _ from the ensuing drop-down list. NetBeans then asks for the instance variables used in the methods. The methods developed by NetBeans are typically sufficient enough for our own needs.
565+
NetBeans provides support for the creation of both `equals` and `hashCode`. You can select Source -> Insert Code from the menu and then select *equals() and hashCode()* from the drop-down list. NetBeans then asks for the instance variables used in the methods. The methods developed by NetBeans are typically sufficient for our needs.
565566

566-
Use NetBeans's support in creating equals and hashCode methods until you know that your methods are definitely better than those created automatically by net beans.
567+
Use NetBeans's support in creating the equals and hashCode methods until you know that your methods are better than those created automatically by NetBeans.
567568

568569
</text-box>
569570

@@ -627,7 +628,7 @@ Eurooppalaiset rekisteritunnukset koostuvat kahdesta osasta: yksi tai kaksikirja
627628

628629
<h2>Equals and hashCode for the LicensePlate class</h2>
629630

630-
European license plates have to parts, a two letter country code and a nationally unique license number. The license number is made up of numbers and characters. License plates are represented by the following class:
631+
European license plates have two parts: a two letter country code and a nationally unique license number. The license number is made up of numbers and characters. License plates are represented by the following class:
631632

632633
```java
633634
public class LicensePlate {
@@ -650,7 +651,7 @@ public class LicensePlate {
650651

651652
<!-- Rekisterinumeroja halutaan tallettaa esim. ArrayList:eille ja käyttää HashMap:in avaimina, eli kuten yllä mainittu, tulee niille toteuttaa metodit `equals` ja `hashCode`, muuten ne eivät toimi halutulla tavalla. Toteuta luokalle rekisterinumero metodit `equals` ja `hashCode`. -->
652653

653-
We want to be able to save the license plates in e.g ArrayLists and to use them as keys in a HashMap. Which, as explained above, means that the `equals` and `hashcode` methods need to be overwritten, or they won't work as intended. Implement the methods `equals` and `hashCode` for the LicensePlate class.
654+
We want to be able to save the license plates in ArrayLists and to use them as keys in a HashMap. This, as explained above, means that the `equals` and `hashcode` methods need to be overwritten, or they won't work as intended. Implement the methods `equals` and `hashCode` for the LicensePlate class.
654655
655656
<!-- Esimerkkiohjelma: -->
656657
@@ -725,11 +726,11 @@ Toteuta luokka `Ajoneuvorekisteri` jolla on seuraavat metodit:
725726

726727
Implement the class `VehicleRegistry`, which has the following methods:
727728

728-
- `public boolean add(LicensePlate licensePlate, String owner)` assigns the owner it received as a parameter to car corresponding with the license plate received as a parameter. If the license plate didn't have an owner returns true. If the license already had an owner attached, the method returns false and does nothing.
729+
- `public boolean add(LicensePlate licensePlate, String owner)` assigns the owner it received as a parameter to a car that corresponds to the license plate received as a parameter. If the license plate doesn't have an owner, the method returns true. If the license already has an owner attached, the method returns false and does nothing.
729730

730-
- `public String get(LicensePlate licensePlate)` returns the owner of the car corresponding to the license plate received as a parameter. If the car isn't in the registry, returns null.
731+
- `public String get(LicensePlate licensePlate)` returns the owner of the car corresponding to the license plate received as a parameter. If the car isn't in the registry, the method returns null.
731732

732-
- `public boolean remove(LicensePlate licensePlate)` removes the license plate and attached data from the registry. Returns true if removed successfully and false if the license plate wasn't in the registry.
733+
- `public boolean remove(LicensePlate licensePlate)` removes the license plate and attached data from the registry. The method returns true if removed successfully and false if the license plate wasn't in the registry.
733734

734735
<!-- <h2>Ajoneuvorekisteri laajenee</h2>
735736
@@ -749,6 +750,6 @@ Add the following methods to the VehicleRegistry:
749750

750751
- `public void printOwners()` prints the owners of the cars in the registry. Each name should only be printed once, even if a particular person owns more than one car.
751752

752-
Useful tip! In the printOwners method, you can create a list used for remembering the owners that were already printed. If an owner is not on the their name is printed and they are added to the list -- if an owner is on the list their name isn't printed.
753+
Useful tip! In the printOwners method, you can create a list used for remembering the owners that were already printed. If an owner is not on the list, their name is printed and they are added to the list; conversely, if an owner is on the list, their name isn't printed.
753754

754755
</programming-exercise>

0 commit comments

Comments
 (0)