You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!-- - Tunnet Javan valmiin rajapinnan Comparable ja osaat toteuttaa sen omissa luokissasi.
11
11
- Osaat hyödyntää Javan valmiita välineitä sekä listojen järjestämiseen että virran alkioiden järjestämiseen.
@@ -25,9 +25,9 @@ Comparable-rajapinnan vaatima compareTo-metodi saa parametrinaan olion, johon "t
25
25
Tarkastellaan tätä kerhossa käyvää lasta tai nuorta kuvaavan luokan Kerholainen avulla. Jokaisella kerholaisella on nimi ja pituus. Kerholaisten tulee mennä syömään pituusjärjestyksessä, joten toteutetaan kerholaisille rajapinta `Comparable`. Comparable-rajapinta ottaa tyyppiparametrinaan luokan, johon vertaus tehdään. Käytetään tyyppiparametrina samaa luokkaa `Kerholainen`. -->
26
26
<p>In the previous section, we looked at interfaces in more general terms - let's now familiarize oruselves with one of Java's ready interfaces. The <ahref="http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html"> Comparable </a> interface defines the `compareTo` method used to compare objects. If a class implements the Comparable interface, objects created from that class can be sorted using Java's sorting algorithms.</p>
27
27
28
-
The compareTo method required by the Comparable interface gets as its parameter the object to which the "this" object is compared. If the "this" object comes before the object received as a parameter in terms of sorting order, the method should return a negative number. If, on the other hand, id "this" object comes after the object received as a parameter, the method should return a positive number. Otherwise, 0 is returned. The sorting resulting from the `compareTo` method is called *natural ordering*.
28
+
The compareTo method required by the Comparable interface receives as its parameter the object to which the "this" object is compared. If the "this" object comes before the object received as a parameter in terms of sorting order, the method should return a negative number. If, on the other hand, the "this" object comes after the object received as a parameter, the method should return a positive number. Otherwise, 0 is returned. The sorting resulting from the `compareTo` method is called *natural ordering*.
29
29
30
-
Let's take a look at this with the help of a Member class that represents a child or youth who belongs to a club. Each club member has a name and height. The club members shoudl go to eat in order of height, so the Member class should implement the `Comparable` interface. The Comparable interface takes as its type parameter the class that is the subject of the comparison. We'll use the same `Member` class as the type parameter.
30
+
Let's take a look at this with the help of a Member class that represents a child or youth who belongs to a club. Each club member has a name and height. The club members should go to eat in order of height, so the Member class should implement the `Comparable` interface. The Comparable interface takes as its type parameter the class that is the subject of the comparison. We'll use the same `Member` class as the type parameter.
31
31
32
32
<!-- ```java
33
33
public class Kerholainen implements Comparable<Kerholainen> {
@@ -107,7 +107,7 @@ Koska `compareTo()`-metodista riittää palauttaa negatiivinen luku, jos `this`-
107
107
The `compareTo` method required by the interface returns an integer that informs us of the order of comparison.
108
108
109
109
110
-
Since returning a negative number from the `compareTo()` is enough if the `this` object is smaller than the parameter object, and zero when the lengths are the same, the `compareTo` method described above can also be implemented as follows.
110
+
As returning a negative number from `compareTo()` is enough if the `this` object is smaller than the parameter object, and returning zero is sufficient when the lengths are the same, the `compareTo` method described above can also be implemented as follows.
111
111
112
112
<!--
113
113
```java
@@ -130,9 +130,9 @@ Mikäli ohjelmoija haluaa järjestää alkuperäisen listan, tähän kannattaa k
130
130
131
131
Kerholaisten järjestäminen on nyt suoraviivaista. -->
132
132
133
-
Since the Member class implements the Comparable interface, it is possible to sort the list by using the `sorted` method. In fact, objects of any class that implements the `Comparable` interface can be sorted using the `sorted` method. Be aware, however, that a stream does not sort the original list - *only the items in the stream are sorted*.
133
+
Since the Member class implements the Comparable interface, it is possible to sort the list by using the `sorted` method. In fact, objects of any class that implement the `Comparable` interface can be sorted using the `sorted` method. Be aware, however, that a stream does not sort the original list - *only the items in the stream are sorted*.
134
134
135
-
If a programmer wants to organize the original list, the `sort` method of the` Collections` class should be used. This of course assumes that the objects on the list implement the `Comparable` interface.
135
+
If a programmer wants to organize the original list, the `sort` method of the` Collections` class should be used. This, of course, assumes that the objects on the list implement the `Comparable` interface.
136
136
137
137
Sorting club members is straightforward now.
138
138
@@ -199,7 +199,7 @@ matti (187)
199
199
200
200
<!--Saat valmiin luokan Ihminen. Ihmisellä on nimi- ja palkkatiedot. ToteutaIhminen-luokassa `Comparable`-rajapinta siten, että `compareTo`-metodi lajittelee ihmiset palkan mukaan järjestykseen isoimmasta palkasta pienimpään. -->
201
201
202
-
You are provided with the classhuman. A human has a name and wage information. Implement the interface `Comparable` in a way, that the overridden `compareTo`-method sorts the humans according to wage from biggest to smallest salary.
202
+
You are provided with the classhuman. A human has a name and wage information. Implement the interface `Comparable` in a way, such that the overridden `compareTo`method sorts the humans according to wage from largest to smallest salary.
203
203
204
204
</programming-exercise>
205
205
@@ -210,11 +210,11 @@ You are provided with the class human. A human has a name and wage information.
210
210
211
211
<!-- Saat valmiin luokan Opiskelija. Opiskelijalla on nimi. Toteuta Opiskelija-luokassa `Comparable`-rajapinta siten, että `compareTo`-metodi lajittelee opiskelijat nimen mukaan aakkosjärjestykseen. -->
212
212
213
-
The exercise template includes the classStudent, which has a name. Implement the `Comprable`-interfacein the Student-classin a way, that the `compareTo`-method sorts the students in alphabetical order based on their names.
213
+
The exercise template includes the classStudent, which has a name. Implement the `Comparable` interfacein the Studentclassin a way, such that the `compareTo`method sorts the students in alphabetical order based on their names.
214
214
215
215
<!-- **Vinkki:** Opiskelijan nimi on String, ja String-luokka on itsessään `Comparable`. Voit hyödyntää String-luokan `compareTo`-metodia Opiskelija-luokan metodia toteuttaessasi. `String.compareTo` kohtelee kirjaimia eriarvoisesti kirjainkoon mukaan, ja tätä varten String-luokalla on myös metodi `compareToIgnoreCase` joka nimensä mukaisesti jättää kirjainkoon huomioimatta. Voit käyttää opiskelijoiden järjestämiseen kumpaa näistä haluat. -->
216
216
217
-
**Hint:** The name of the Student is a String, which implements `Comparable` itself. You may use it's `compareTo`-method for your advantage when implementing the method for the `Student`-class. Note that `String.compareTo` also treats letters according to their size, while the `compareToIgnoreCase`-method of the same class ignores the capitalization completely. You may either of these methods in the exercise.
217
+
**Hint:** The name of the Student is a String, which implements `Comparable` itself. You may use its `compareTo`method when implementing the method for the `Student`class. Note that `String.compareTo()` also treats letters according to their size, while the `compareToIgnoreCase`method of the same class ignores the capitalization completely. You may either of these methods in the exercise.
218
218
219
219
</programming-exercise>
220
220
@@ -225,7 +225,7 @@ The exercise template includes the class Student, which has a name. Implement th
225
225
226
226
Oletetaan, että käytössämme on seuraava rajapinta `Tunnistettava`. -->
227
227
228
-
A class can implement multiple interfaces. Multiple interfaces are implemented by separating the implemented interfaces with commas (`public class ... implements *FirstInterface*, *SecondInterface* ...`). Implementing multiple interfaces requires us to implement all the of the methods whose implementations are required by the interfaces.
228
+
A class can implement multiple interfaces. Multiple interfaces are implemented by separating the implemented interfaces with commas (`public class ... implements *FirstInterface*, *SecondInterface* ...`). Implementing multiple interfaces requires us to implement all of the methods for which implementations are required by the interfaces.
229
229
230
230
Say we have the following `Identifiable` interface.
231
231
@@ -380,7 +380,7 @@ Sekä luokan `Collections` metodille `sort` että virran metodille `sorted` void
380
380
381
381
We want to sort the list without having to implement the `Comparable` interface.
382
382
383
-
<p>Both the `sort` method of `Collections` class and the stream's `sorted` method accept a lambda expression as a parameter that defines the sorting criteria. More specifically, both methods can be provided with an object that implements the <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank">Comparator</a> interface, which defines the desired order - the lambda expression is used to create this object.</p>
383
+
<p>Both the `sort` method of the `Collections` class and the stream's `sorted` method accept a lambda expression as a parameter that defines the sorting criteria. More specifically, both methods can be provided with an object that implements the <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank">Comparator</a> interface, which defines the desired order - the lambda expression is used to create this object.</p>
384
384
385
385
<!-- ```java
386
386
ArrayList<Henkilo> henkilot =newArrayList<>();
@@ -536,7 +536,7 @@ palat[1] = palat[1].trim();
536
536
537
537
```java
538
538
String string ="Adult literacy rate, population 15+ years, female (%),Zimbabwe,2015,85.28513";
539
-
String[] pieces =mjono.split(",");
539
+
String[] pieces =string.split(",");
540
540
// now pieces[0] equals "Adult literacy rate"
541
541
// pieces[1] equals " population 15+ years"
542
542
// etc.
@@ -553,7 +553,7 @@ pieces[1] = pieces[1].trim();
553
553
<!--
554
554
Joskus haluamme järjestää esineitä useamman asian perusteella. Tarkastellaan seuraavaksi esimerkkiä, missä elokuvat listataan nimen ja julkaisuvuoden perusteella järjestettynä.Tässä käytämme Javan valmista <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank" norel>Comparator</a>-luokkaa, joka tarjoaa menetelmiä järjestämiseen. Oletetaan, että käytössämme on seuraava luokka `Elokuva` -->
555
555
556
-
<p>We sometimes want to sort items based on a number of things. Let's look at an example in which films are listed in order of their name and year of release. We'll make use of Java's <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank" norel>Comparator</a> class here, which offers us the functionality required for sorting. Le'ts assume that we have the clas `Film` at our disposal.</p>
556
+
<p>We sometimes want to sort items based on a number of things. Let's look at an example in which films are listed in order of their name and year of release. We'll make use of Java's <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank" norel>Comparator</a> class here, which offers us the functionality required for sorting. Let's assume that we have the class `Film` at our disposal.</p>
557
557
558
558
<!--
559
559
```java
@@ -609,7 +609,7 @@ public class Film {
609
609
Kun järjestämme olioita, metodeille `comparing` ja `thenComparing` annetaan parametrina olion tyyppiin liittyvä metodiviite -- järjestyksessä kutsutaan metodia ja vertaillaan metodin palauttamia arvoja. Metodiviite annetaan muodossa `Luokka::metodi`.Alla olevassa esimerkissä tulostetaan elokuvat vuoden ja nimen perusteella järjestyksessä.-->
610
610
The `Comparator` classprovides two essential methods for sorting: `comparing` and `thenComparing`. The `comparing` method is passed the value to be compared first, and the `thenComparing` method is the next value to be compared. The `thenComparing` method can be used many times by chaining methods, which allows virtually unlimited values to be used for comparison.
611
611
612
-
When we sort objects, the `comparing` and` thenComparing` methods are given a reference to the object's type - the method is called in order and the values returned by the method are compared. The method reference is given as `Class::method`. In the example below, we print movies by year and title in order.
612
+
When we sort objects, the `comparing` and `thenComparing` methods are given a reference to the object's type - the method is called in order and the values returned by the method are compared. The method reference is given as `Class::method`. In the example below, we print movies by year and title in order.
613
613
614
614
<!-- ```java
615
615
List<Elokuva> elokuvat = new ArrayList<>();
@@ -678,7 +678,7 @@ Write a program that reads user input for books and their age recommendations.
678
678
679
679
<!--Ohjelma kysyy uusia kirjoja kunnes käyttäjä syöttää tyhjän merkkijonon kirjan nimen kohdalla (eli painaa rivinvaihtoa).Tämän jälkeen ohjelma tulostaa syötettyjen kirjojen lukumäärän sekä kirjat. -->
680
680
681
-
The program asks fornew books until the user gives an empty String (only presses enter).Afterthis, the program will print the amount- and names of the books.
681
+
The program asks fornew books until the user gives an empty String (only presses enter).Afterthis, the program will print the number of books, their names, and their recommended ages.
682
682
683
683
684
684
<!--<h2>Kirjojen lukeminen ja tulostaminen</h2>-->
0 commit comments