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
Copy file name to clipboardExpand all lines: data/part-2/1-problems-and-patterns.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,7 +81,7 @@ public class Program {
81
81
82
82
<!-- Ohjelmissa tulee usein laskea asioita kuten lukujen keskiarvoa tai lukujen summaa. Ratkaisumalli tällaisissa ohjelmissa on seuraava. -->
83
83
84
-
We quite often need to calculate something in a program, such as an average or a sum. The solution patter to solve such problems is as follows.
84
+
We quite often need to calculate something in a program, such as an average or a sum. The solution pattern to solve such problems is as follows.
85
85
86
86
<!-- 1. Määrittele laskemiseen tarvittavat syötteet ja luo niitä varten muuttujat. Ohjelman syötteitä ovat laskemisessa käytettävät arvot. Syötteiden tyypit tunnistaa tyypillisesti ongelma-alueen kuvauksesta.
87
87
1. Selvitä tehtävä laskuoperaatio ja luo laskuoperaation tulokselle muuttuja. Tee ohjelman syötteiden perusteella lasku, jonka arvo asetetaan laskuoperaation tulokselle varattuun muuttujaan. Myös laskuoperaation tuloksen tyypin tunnistaa ongelma-alueen kuvauksesta.
@@ -93,7 +93,7 @@ We quite often need to calculate something in a program, such as an average or a
93
93
94
94
<!-- Esimerkiksi ongelman _Tee ohjelma, jonka avulla voidaan laskea kahden kokonaisluvun summa_. ratkaisumalli on seuraava. -->
95
95
96
-
For example, the solution patter for the problem _Create a program to calculate the sum of two integers_ is the following.
96
+
For example, the solution pattern for the problem _Create a program to calculate the sum of two integers_ is the following.
97
97
98
98
<!-- ```java
99
99
// Määritellään syötteet ja luodaan niitä varten muuttujat
@@ -122,7 +122,7 @@ System.out.println("The sum of " + first + " and " + second + " is " + sum);
122
122
123
123
<!-- Sekä lukemista että laskemista sisältävä ohjelma yhdistää edelliset ratkaisumallit. Kahden käyttäjältä pyydetyn luvun tulon laskeva ohjelma on seuraavanlainen. -->
124
124
125
-
A program that both reads and calculates combines both of these patterns. One that calculates the product of two integers provided by the use looks like this:
125
+
A program that both reads and calculates combines both of these patterns. One that calculates the product of two integers provided by the user looks like this:
126
126
127
127
<!-- ```java
128
128
// Tuodaan Scanner-apuväline ohjelman tietoon
@@ -316,7 +316,7 @@ Here are a few examples:
316
316
317
317
<!-- Ongelmat sisältävät usein vaihtoehtoista toiminnallisuutta. Tällaisen toteuttamiseen käytetään ehtolauseita. Ehtolause alkaa `if`-komennosta, jota seuraa suluissa oleva lauseke. Lauseke evaluoituu joko todeksi tai epätodeksi. Mikäli lauseke evaluoituu todeksi, suoritetaan ehtolauseen lohko, joka on rajattuna aaltosuluilla. -->
318
318
319
-
Problems often contain some alternative functionality, and in such case we use conditional statements. A Conditional statement starts with an `if` command followed by an expression in parentheses. The expression evaluates to either true or false. If it evaluates true, the following block delimited by curly brackets gets executed.
319
+
Problems often contain some alternative functionality, and in such case we use conditional statements. A conditional statement starts with an `if` command followed by an expression in parentheses. The expression evaluates to either true or false. If it evaluates true, the following block delimited by curly brackets gets executed.
Copy file name to clipboardExpand all lines: data/part-2/2-repeating.md
+4-3Lines changed: 4 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ System.out.println("The sum of the numbers is " + sum);
53
53
```
54
54
55
55
<!-- Hoitaa asian, mutta ei kovin tyylikkäästi. Entä jos ohjelman pitäisi lukea sata tai vaikkapa tuhat lukua ja tulostaa niiden summa? Entä jos ohjelman pitäisi lukea kolme lukua? -->
56
-
It does the job, but not elegantly. What if the program had to read a hundred, or perhaps a thousand, numbers and print their sum? What if the program had to read three numbers only?
56
+
It does the job, but not elegantly. What if the program had to read a hundred, or perhaps a thousand numbers and print their sum? What if the program had to read three numbers only?
57
57
58
58
<!-- Tämän ongelman voi ratkaista toistolauseella, joka pitää kirjaa sekä summasta että lukemiskerroista. Viiden luvun summan tulostava toistolauseella toteutettava ohjelma näyttää seuraavalta. -->
59
59
The problem can be solved with a loop, which keeps track of both the sum and the number of times input has been read. The program that prints the sum of five numbers now looks as follows
@@ -151,7 +151,7 @@ Breaking out of the loop occurs when a user enters a specified input or whenever
151
151
Users can also be asked for input within a loop. The variables that are commonly used in loops (such as Scanner readers) are defined before the loop, whereas variables (such as the value read from the user) that are specific to the loop are defined within it.
152
152
153
153
<!-- Alla olevassa esimerkissä ohjelma kysyy käyttäjältä pitäisikö toistolauseesta poistua. Mikäli käyttäjä syöttää merkkijonon "k", ohjelman suoritus siirtyy toistolausetta seuraavaan komentoon, jonka suorittamisen jälkeen ohjelman suoritus päättyy. -->
154
-
In the example below, the program asks the user whether to exit the loop or not. If the user inputs the string "k", the execution of the program moves to the command following the loop block, after which the execution of the program ends.
154
+
In the example below, the program asks the user whether to exit the loop or not. If the user inputs the string "y", the execution of the program moves to the command following the loop block, after which the execution of the program ends.
155
155
156
156
```java
157
157
Scanner scanner =newScanner(System.in);
@@ -252,6 +252,7 @@ Write a program according to the preceding example that asks a user to input val
<!-- Kirjoita ohjelma, joka kysyy käyttäjältä lukuja. Mikäli luku on negatiivinen (eli pienempi kuin nolla), käyttäjälle tulostetaan viesti "Epäkelpo luku" ja käyttäjältä kysytään uutta lukua. Jos taas luku on nolla, lukujen lukeminen lopetetaan ja ohjelma poistuu toistolauseesta. Mikäli luku on positiivinen, ohjelma tulostaa luvun toisen potenssin. -->
331
-
Write a program that asks a user for numbers. If the number is negative (smaller than zero), the program prints for user "unfit number" and asks the user for a new number. If the number is zero, the program exits the loop. If the number is positive, the program prints the number power of two.
332
+
Write a program that asks a user for numbers. If the number is negative (smaller than zero), the program prints for user "Unsuitable number" and asks the user for a new number. If the number is zero, the program exits the loop. If the number is positive, the program prints the number power of two.
Copy file name to clipboardExpand all lines: data/part-2/3-more-loops.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,7 @@ Next, we'll come to know a few other ways to implement loops.
35
35
So far we have been using a loop with the boolean 'true' in its paranthesis, so the loop continues forever (or until the loop is ended with the `break` command ).
36
36
37
37
<!-- Toistolauseen sulut sisältävät oikeastaan ehtolausekkeen, aivan samalla tavalla kuin `if`-komentoa seuraavat sulut. Arvon `true` voi korvata lausekkeella, joka evaluoidaan ohjelman suorituksen yhteydessä. Lauseke määritellään täsmälleen samalla tavalla kuin ehtolauseen lauseke. -->
38
-
Actually the paranthesis of a loop contain a conditional expression, or a condition, just like the paranthesis of an `if` statement. The `true` value can be replaced with an expression, which is evaluated as the program is executed. The expression is defined the same way as the condition of a conditional statement.
38
+
Actually the paranthesis of a loop can contain a conditional expression, or a condition, just like the paranthesis of an `if` statement. The `true` value can be replaced with an expression, which is evaluated as the program is executed. The expression is defined the same way as the condition of a conditional statement.
39
39
40
40
<!-- Seuraavassa esimerkissä tulostetaan luvut 1, 2, ..., 5. Kun `luku`-muuttujan arvo on yli 5, `while`-ehto ei ole enää voimassa ja toistaminen lopetetaan. -->
41
41
The following code prints the numbers 1,2,...,5. When the value of the variable `number` is more than 5, the `while`-condition evaluates to false and the execution ends.
@@ -131,7 +131,7 @@ We will continue practicing loops in the following exercises. You can use either
<!-- Kirjoita ohjelma, joka lukee käyttäjältä kokonaisluvun. Tämän jälkeen ohjelma tulostaa luvut nollasta käyttäjän syöttämään lukuun. Voit olettaa, että käyttäjä syöttää aina positiivisen luvun. Alla on muutamia esimerkkejä ohjelman toivotusta toiminnasta. -->
134
-
Write a program that reads an integer from the user. Then the program prints numbers from 0 to the number given by the user. You can assume that the user always gives a positive number. Below is some examples of the wanted functionality.
134
+
Write a program that reads an integer from the user. Then the program prints numbers from 0 to the number given by the user. You can assume that the user always gives a positive number. Below are some examples of the wanted functionality.
Copy file name to clipboardExpand all lines: data/part-2/4-methods.md
+16-10Lines changed: 16 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -450,7 +450,7 @@ Greetings!
450
450
451
451
<!-- Aivan kuten Javan valmista `System.out.println()`-metodia kutsuttaessa, voi oman metodin kutsussa parametrina antaa lausekkeen. -->
452
452
453
-
Just like when calling the predefined method `System.out.println`, you can pass an expression as a paratmeter.
453
+
Just like when calling the predefined method `System.out.println`, you can pass an expression as a parameter.
454
454
455
455
<!-- ```java
456
456
public static void main(String[] args) {
@@ -626,7 +626,7 @@ sum(3, 5);
626
626
int number1 =2;
627
627
int number2 =4;
628
628
629
-
summa(number1, number2);
629
+
sum(number1, number2);
630
630
```
631
631
632
632
<!-- <sample-output>
@@ -884,7 +884,7 @@ The parameter `number` is copied for the method's use, i.e., a new variable call
884
884
885
885
<!-- Metodin määrittelyssä kerrotaan palauttaako metodi arvon. Jos metodi palauttaa arvon, tulee metodimäärittelyn yhteydessä kertoa palautettavan arvon tyyppi. Muulloin määrittelyssä käytetään avainsanaa `void`. Tähän mennessä tekemämme metodit ovat määritelty avainsanaa `void` käyttäen eli eivät ole palauttaneet arvoa. -->
886
886
887
-
The definition of a method tells whether that method returns a value or not. If it does, the method definition has to include type of the return value. Otherwise the keyword `void` is used in the definition. The methods we've created so far have been defined with the keyword `void`, i.e., they've returned no values.
887
+
The definition of a method tells whether that method returns a value or not. If it does, the method definition has to include the type of the return value. Otherwise the keyword `void` is used in the definition. The methods we've created so far have been defined with the keyword `void`, i.e., they've returned no values.
888
888
889
889
<!-- ```java
890
890
public static **void** kasvataKolmella() {
@@ -1089,7 +1089,7 @@ public static int functioningMethod(int parameter) {
1089
1089
1090
1090
System.out.println("The number received as parameter is ten or lesser.");
1091
1091
1092
-
returnparametri;
1092
+
returnparameter;
1093
1093
}
1094
1094
```
1095
1095
@@ -1109,6 +1109,16 @@ public static void jakolasku(int osoittaja, int nimittaja) {
1109
1109
}
1110
1110
``` -->
1111
1111
1112
+
```java
1113
+
publicstaticvoid division(int numerator, int denominator) {
@@ -1308,7 +1318,7 @@ When the execution of the method reaches the statement `return first + second;`,
1308
1318
1309
1319
<!-- Metodin kutsutaan seuraavasti. Alla metodia käytetään laskemaan luvut 2 ja 7 yhteen. Metodikutsusta saatava paluuarvo asetetaan muuttujaan `lukujenSumma`: -->
1310
1320
1311
-
The method is called in the following way. Below, the method is used to add the numbers 2 and 7 together. The value resuting from the method call is placed into the variable `sumOfNumbers`.
1321
+
The method is called in the following way. Below, the method is used to add the numbers 2 and 7 together. The value resulting from the method call is placed into the variable `sumOfNumbers`.
1312
1322
1313
1323
<!-- ```java
1314
1324
int lukujenSumma = summa(2, 7);
@@ -1469,11 +1479,7 @@ Sum: 14
1469
1479
1470
1480
<!-- **Huom:** kun tehtävässä sanotaan että metodin pitää _palauttaa_ jotain, tarkoittaa tämä sitä että metodissa tulee olla määritelty paluutyyppi ja `return`-komento jolla haluttu asia palautetaan. Metodi ei itse tulosta (eli käytä komentoa `System.out.println(..)`), tulostuksen hoitaa metodin kutsuja, eli tässä tapauksessa pääohjelma. -->
1471
1481
1472
-
<<<<<<< HEAD:data/part-2/4-methods.md
1473
1482
**NB:** when an exercise describes a method that should _return_ something, this means that the type of the return value must be declared in the method definition, and that the method contains a `return` command that returns the wanted data. The method itself will print nothing (i.e. will not use the command `System.out.println`) - that task is left to the method caller, which in this case is the main program.
1474
-
=======
1475
-
**N.B.:** when an exercise describes a method that should _return_ something, this means that the type of the return value must be declared in the method definition, and that the method contains a `return` command that returns the desired data. The method itself will print nothing (i.e. will not use the command `System.out.println`) - that task is left to the method's caller, which in this case is the main program.
@@ -1663,7 +1669,7 @@ How does the computer remember where to return after the execution of a method?
1663
1669
1664
1670
<!-- Java-lähdekoodin suoritusympäristö pitää kirjaa suoritettavasta metodista kutsupinossa. **Kutsupino** sisältää kehyksiä, joista jokainen sisältää tiedon kyseisen metodin sisäisistä muuttujista sekä niiden arvoista. Kun metodia kutsutaan, kutsupinoon luodaan uusi kehys, joka sisältää metodin sisältämät muuttujat. Kun metodin suoritus loppuu, metodiin liittyvä kehys poistetaan kutsupinosta, jolloin suoritusta jatketaan kutsupinon edeltävästä metodista. -->
1665
1671
1666
-
The environment that executes Java source code keeps track of the method being executed in the call stack. **The call stack** contains frames, each of which includes information about a specific method's internal variables and their values. When a method is called, a new frame containing its variables is created in the call stack. When the execution of a method ends, the frame relating tt method is removed from the call stack, which leads to execution resuming at the previous method of the stack.
1672
+
The environment that executes Java source code keeps track of the method being executed in the call stack. **The call stack** contains frames, each of which includes information about a specific method's internal variables and their values. When a method is called, a new frame containing its variables is created in the call stack. When the execution of a method ends, the frame relating to a method is removed from the call stack, which leads to execution resuming at the previous method of the stack.
1667
1673
1668
1674
<!-- Alla olevan visualisaation oikealla laidalla näytetään kutsupinon toimintaa. Metodikutsun yhteydessä kutsupinoon luodaan uusi kehys, joka poistetaan metodikutsusta poistuttaessa. -->
0 commit comments