Skip to content

Commit

Permalink
Merge pull request rage#77 from Nurou/master
Browse files Browse the repository at this point in the history
5.3
  • Loading branch information
nygrenh authored Feb 19, 2020
2 parents 7558ad3 + 4825438 commit 3332335
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 78 deletions.
2 changes: 1 addition & 1 deletion data/part-1/6-conditional-statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ hidden: false

- Become familiar with comparison and logical operators commonly used in conditional statements.

- know how to compare both numbers and strings, remembering the equals-command for strings.
- Know how to compare both numbers and strings, remembering the equals-command for strings.

- Become familiar with the order of execution for a conditional statement, and know that the parsing of a conditional statement stops at the first condition whose statement evaluates to true.

Expand Down
55 changes: 29 additions & 26 deletions data/part-5/2-method-and-constructor-overloading.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ hidden: false

<text-box variant='learningObjectives' name='Learning objectives'>

- Know the term overloading
- Can create multiple constructors for a class.
- Can create multiple methods with the same name in a class.
- Becoming familiar with the term overloading
- Creating multiple constructors for a class.
- Creating multiple methods with the same name in a class.

</text-box>


<!-- Palataan jälleen henkilöitä käsittelevän luokan pariin. Luokka `Henkilo` näyttää tällä hetkellä seuraavalta: -->
Let's get back to the Person class once more. The class 'Person' currently is as follows:
Let's once more return to our Person class. It currently looks like this:


```java
Expand Down Expand Up @@ -83,7 +83,7 @@ public class Person {


<!-- Kaikki henkilöoliot ovat luontihetkellä 0-vuotiaita, sillä konstruktori asettaa uuden henkilön `ika`-oliomuuttujan arvoksi 0: -->
Initially all person objects are 0 years old, because the constructor sets the value of the instance variable `age` to 0:
All person objects are 0 years old when created. This is because the constructor sets the value of the instance variable `age` to 0:

```java
public Person(String name) {
Expand All @@ -95,12 +95,13 @@ public Person(String name) {
```


## Constructor overloading
## Constructor Overloading


<!-- Haluaisimme luoda henkilöitä myös siten, että konstruktorin parametrina annettaisiin ikä nimen lisäksi. Tämä onnistuu, sillä konstruktoreja voi olla useita. Tehdään vaihtoehtoinen konstruktori. Vanhaa konstruktoria ei tarvise poistaa. -->
We would like to also be able to create Persons so that the constructor is given the age as well as the name as parameters. This is possible, because a class can have multiple constructors.
Let's make an alternative constructor. You don't have to delete the old constructor.
We would also like to be able to create persons so that the constructor is provided both the age as well as the name as parameters. This is possible since a class may have multiple constructors.

Let's make an alternative constructor. The old constructor can remain in place.


```java
Expand All @@ -121,7 +122,7 @@ public Person(String name, int age) {


<!-- Nyt olioiden luonti onnistuu kahdella vaihtoehtoisella tavalla: -->
Now we have two alternative ways to create objects:
We now have two alternative ways to create objects:


```java
Expand All @@ -143,27 +144,29 @@ Ada is 0 years old.


<!-- Tekniikkaa jossa luokalla on kaksi konstruktoria, kutsutaan *konstruktorin kuormittamiseksi*. Luokalla voi siis olla useita konstruktoreja, jotka poikkeavat toisistaanparametriensa määrältä tai tyypeiltä. Ei kuitenkaan ole mahdollista tehdä kahta erilaista konstruktoria joilla on täysin saman tyyppiset parametrit. Emme siis voi edellisten lisäksi lisätä konstruktoria `public Henkilo(String nimi, int paino)` sillä Javan on mahdoton erottaa tätä kaksiparametrisesta konstruktorissa, jossa luku tarkoittaa ikää. -->
This technique, where a class has two (or more) constructors, is called *constructor overloading*. A class can have multiple constructors which differ on the amount or type of their parameters. It is however not possible to have two constructor with exactly the same parameters.
We cannot for example now add a constructor `public Person(String name, int weight)` because it is impossible for Java to differentiate this constructor with two parameters from the constructor where the int parameter means age.
The technique of having two (or more) constructors in a class is known as *constructor overloading*. A class can have multiple constructors that differ in the number and/or type of their parameters. It's not, however, possible to have two constructors with the exact same parameters.

We cannot, for example, add a `public Person(String name, int weight)` constructor since it would be impossible for Java to differentiate between this and the one that has two parameters where int parameter is used for age.



## Calling your constructor
## Calling Your Constructor


<!-- Mutta hetkinen, aiemmin todettiin että "copy-paste"-koodi ei ole hyvä idea. Kun tarkastellaan edellä tehtyjä kuormitettuja konstruktoreita, niissä on aika paljon samaa. Emme ole oikein tyytyväisiä tilanteeseen. -->
But wait a second, previously you were told that "copy-paste" code is never a good idea. When you look at the overloaded constructors above, they have a lot of the same code. We are not happy with this.
Hold on a moment. We'd previously concluded that "copy-paste" code is not a good idea. When you look at the overloaded constructors above, however, they have a lot in common. We're not happy with this.


<!-- Konstruktoreista ylempi, eli nimen parametrinaan saava konstruktori, on oikeastaan alemman, eli nimen ja iän parametrinaan saavan konstruktorin, erikoistapaus. Entä jos ylempi konstruktori voisi "kutsua" alempaa konstruktoria? -->
The first constructor, the one that is only given a name as a parameter,is actually a special case of the second constructor, which is given both name and age. What if the first constructor could call the second constructor?
The first constructor - the one that receives a name as a parameter - is in fact a special case of the second constructor - the one that's given both name and age. What if the first constructor could call the second constructor?


<!-- Tämä onnistuu, sillä konstruktorin sisältä voi kutsua toista konstruktoria juuri tähän olioon liittyvän `this`-ilmauksen avulla! -->
That is no problem, because you can call a constructor from another constructor using the `this` keyword tied to this exact object!
This is possible. A constructor can be called from another constructor using the `this` keyword, which refers to this object in question!


<!-- Muutetaan ylempää konstruktoria siten, että se ei itse tee mitään vaan ainoastaan kutsuu alempaa konstruktoria ja pyytää sitä asettamaan iäksi 0: -->
Let's modify the first constructor so, that it does not do anything itself, but calls the second constructor and asks it to set the age to 0.
Let's modify the first constructor so that it does not do anything by itself, but instead calls the second constructor and asks it to set the age to 0.


```java
Expand All @@ -181,11 +184,11 @@ public Person(String name, int age) {
```

<!-- Oman konstruktorin kutsu `this(nimi, 0);` saattaa vaikuttaa erikoiselta. Asiaa voi vaikka ajatella siten, että kutsun kohdalle tulee "copy-pastena" automaattisesti alemman konstruktorin koodi, siten että ika parametrin arvoksi tulee 0. Huom! Jos konstruktorista kutsutaan toista konstruktoria, tulee konstruktorin kutsun olla ensimmäinen toiminto konstruktorin sisällä. -->
The constructor call `this(name, 0);` might seem a bit weird. One way to understand it is to imagine, that the call is automatically replaced with "copy-paste" of the second constructor so, that the age parameter is set to 0. NB! If a constructor calls another constructor, the constructor call must be the first command in the constructor.
The constructor call `this(name, 0);` might seem a bit weird. A way to think about it is to imagine that the call is automatically replaced with "copy-paste" of the second constructor in such a way that the age parameter is set to 0. NB! If a constructor calls another constructor, the constructor call must be the first command in the constructor.


<!-- Olioiden luonti onnistuu aivan kuten edellisessä esimerkissä: -->
New objects can be created just like before:
New objects can be created just as before:


```java
Expand All @@ -210,10 +213,10 @@ Eve is 0 years old.
<!-- TODO: tänne tehtävä -->


<programming-exercise name='Constructor overload' tmcname='part05-Part05_05.ConstructorOverload'>
<programming-exercise name='Constructor Overload' tmcname='part05-Part05_05.ConstructorOverload'>

<!-- Tehtäväpohjassa on luokka `Esine`, joka kuvaa kaupassa olevaa esinettä. Jokaisella esineellä on nimi, sijainti sekä paino. -->
The exercise template has a class `Product`, which represents a product in a shop. Every product has a name, location and weight.
The exercise template has a class `Product`, which represents a product in a shop. Every product has a name, location and weight.


<!-- Lisää luokkaan `Esine` seuraavat kolme konstruktoria: -->
Expand Down Expand Up @@ -255,10 +258,10 @@ Tyre (5 kg) can be found from the warehouse.



## Method overloading
## Method Overloading

<!-- Konstruktorien tapaan myös metodeja voi kuormittaa, eli samannimisestä metodista voi olla useita versioita. Jälleen eri versioiden parametrien tyyppien on oltava erilaiset. Tehdään `vanhene`-metodista toinen versio, joka mahdollistaa henkilön vanhentamisen parametrina olevalla vuosimäärällä: -->
Like constructors, methods can also be overloaded, so you can have multiple versions of one method. Again, the parameters of the different versions must be different. Let's make another version of the `growOlder` method, which ages the person the amount of years given to it as a parameter.
Methods can be overloaded in the same way as constructors, i.e., multiple versions of a given method can be created. Once again, the parameters of the different versions must be different. Let's make another version of the `growOlder` method that ages the person by the amount of years given to it as a parameter.


```java
Expand All @@ -272,7 +275,7 @@ public void vanhene(int years) {
```

<!-- Seuraavassa "Pekka" syntyy 24-vuotiaana, vanhenee ensin vuoden ja sitten 10 vuotta: -->
Below "Paul" is born 24 years old, first ages one year and then ages 10 years:
In the example below, "Paul" is born 24 years old, ages by a year and then by 10 years:


```java
Expand Down Expand Up @@ -302,11 +305,11 @@ Paul is 35 years old.


<!-- Henkilöllä on nyt siis käytännössä kaksi kappaletta `vanhene`-nimisiä metodeja. Se kumpi metodeista valitaan suoritettavaksi, riippuu metodikutsussa käytettyjen parametrien määrästä. -->
A Person now has two methods called `growOlder`. Which one is executed debends on the amount of parameters given.
A Person now has two methods, both called `growOlder`. The one that gets executed depends on the number of parameters provided.


<!-- Ohjelmaa voi muokata myös niin, että parametriton metodi `vanhene` toteutetaan metodin `vanhene(int vuodet)` avulla: -->
We can also modify the program so, that the method without parameters is implemented using the method `growOlder(int years)`:
We may also modify the program so that the parameterless method is implemented using the method `growOlder(int years)`:


```java
Expand Down Expand Up @@ -334,7 +337,7 @@ public void growOlder(int years) {


<!-- Toteuta luokka `Laskuri`, joka sisältää luvun, jota voi vähentää ja suurentaa. Luokalla tulee olla seuraavat konstruktorit: -->
Implement a class called `Counter`. The class contains a number, whichs value can be increased and decreased. The class must have the following constructors:
Implement a class called `Counter`. The class contains a number whose value can be incremented and decremented. The class must have the following constructors:


<!-- - `public Counter(int startValue)` asettaa laskurin alkuarvoksi parametrin `alkuarvo` arvon. -->
Expand Down
Loading

0 comments on commit 3332335

Please sign in to comment.