Skip to content

Commit 98d0c8f

Browse files
committed
Merge branch 'kevin0110w-newBranchKW'
2 parents cfdd559 + 62d6331 commit 98d0c8f

4 files changed

Lines changed: 28 additions & 21 deletions

File tree

data/part-2/1-problems-and-patterns.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public class Program {
8181

8282
<!-- Ohjelmissa tulee usein laskea asioita kuten lukujen keskiarvoa tai lukujen summaa. Ratkaisumalli tällaisissa ohjelmissa on seuraava. -->
8383

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.
8585

8686
<!-- 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.
8787
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
9393

9494
<!-- Esimerkiksi ongelman _Tee ohjelma, jonka avulla voidaan laskea kahden kokonaisluvun summa_. ratkaisumalli on seuraava. -->
9595

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.
9797

9898
<!-- ```java
9999
// 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);
122122

123123
<!-- Sekä lukemista että laskemista sisältävä ohjelma yhdistää edelliset ratkaisumallit. Kahden käyttäjältä pyydetyn luvun tulon laskeva ohjelma on seuraavanlainen. -->
124124

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:
126126

127127
<!-- ```java
128128
// Tuodaan Scanner-apuväline ohjelman tietoon
@@ -316,7 +316,7 @@ Here are a few examples:
316316

317317
<!-- 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. -->
318318

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.
320320

321321
<!-- ```java
322322
// jos luku on suurempi kuin viisi

data/part-2/2-repeating.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ System.out.println("The sum of the numbers is " + sum);
5353
```
5454

5555
<!-- 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?
5757

5858
<!-- Tämän ongelman voi ratkaista toistolauseella, joka pitää kirjaa sekä summasta että lukemiskerroista. Viiden luvun summan tulostava toistolauseella toteutettava ohjelma näyttää seuraavalta. -->
5959
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
151151
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.
152152

153153
<!-- 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.
155155

156156
```java
157157
Scanner scanner = new Scanner(System.in);
@@ -252,6 +252,7 @@ Write a program according to the preceding example that asks a user to input val
252252

253253
<sample-output>
254254

255+
Give a number:
255256
**5**
256257
Give a number:
257258
**744**
@@ -328,7 +329,7 @@ while (true) {
328329
<programming-exercise name="Only positives" tmcname='part02-Part02_07.OnlyPositives'>
329330

330331
<!-- 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.
332333

333334
<sample-output>
334335

data/part-2/3-more-loops.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Next, we'll come to know a few other ways to implement loops.
3535
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 ).
3636

3737
<!-- 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.
3939

4040
<!-- Seuraavassa esimerkissä tulostetaan luvut 1, 2, ..., 5. Kun `luku`-muuttujan arvo on yli 5, `while`-ehto ei ole enää voimassa ja toistaminen lopetetaan. -->
4141
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
131131
<programming-exercise name='Counting' tmcname='part02-Part02_14.Counting'>
132132

133133
<!-- 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.
135135

136136
<sample-output>
137137

@@ -279,9 +279,9 @@ int number = 1;
279279

280280
while (number != 2) {
281281
System.out.println(number);
282-
luku = 2;
282+
number = 2;
283283
System.out.println(number);
284-
luku = 1;
284+
number = 1;
285285
}
286286
```
287287

data/part-2/4-methods.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ Greetings!
450450

451451
<!-- Aivan kuten Javan valmista `System.out.println()`-metodia kutsuttaessa, voi oman metodin kutsussa parametrina antaa lausekkeen. -->
452452

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.
454454

455455
<!-- ```java
456456
public static void main(String[] args) {
@@ -626,7 +626,7 @@ sum(3, 5);
626626
int number1 = 2;
627627
int number2 = 4;
628628

629-
summa(number1, number2);
629+
sum(number1, number2);
630630
```
631631

632632
<!-- <sample-output>
@@ -884,7 +884,7 @@ The parameter `number` is copied for the method's use, i.e., a new variable call
884884

885885
<!-- 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. -->
886886

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.
888888

889889
<!-- ```java
890890
public static **void** kasvataKolmella() {
@@ -1089,7 +1089,7 @@ public static int functioningMethod(int parameter) {
10891089

10901090
System.out.println("The number received as parameter is ten or lesser.");
10911091

1092-
return parametri;
1092+
return parameter;
10931093
}
10941094
```
10951095

@@ -1109,6 +1109,16 @@ public static void jakolasku(int osoittaja, int nimittaja) {
11091109
}
11101110
``` -->
11111111

1112+
```java
1113+
public static void division(int numerator, int denominator) {
1114+
if (denominator == 0) {
1115+
System.out.println("Can not divide by 0!");
1116+
return;
1117+
}
1118+
1119+
System.out.println("" + numerator + " / " + denominator + " = " + (1.0 * numerator / denominator));
1120+
}
1121+
```
11121122

11131123
<!-- Muuttujien määrittely metodien sisällä -->
11141124

@@ -1308,7 +1318,7 @@ When the execution of the method reaches the statement `return first + second;`,
13081318

13091319
<!-- Metodin kutsutaan seuraavasti. Alla metodia käytetään laskemaan luvut 2 ja 7 yhteen. Metodikutsusta saatava paluuarvo asetetaan muuttujaan `lukujenSumma`: -->
13101320

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`.
13121322

13131323
<!-- ```java
13141324
int lukujenSumma = summa(2, 7);
@@ -1469,11 +1479,7 @@ Sum: 14
14691479

14701480
<!-- **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. -->
14711481

1472-
<<<<<<< HEAD:data/part-2/4-methods.md
14731482
**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.
1476-
>>>>>>> 73493be24544e9825a3a481cd6d5905f4653274f:data/osa-2/4-metodit.md
14771483

14781484
</programming-exercise>
14791485

@@ -1663,7 +1669,7 @@ How does the computer remember where to return after the execution of a method?
16631669

16641670
<!-- 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. -->
16651671

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.
16671673

16681674
<!-- Alla olevan visualisaation oikealla laidalla näytetään kutsupinon toimintaa. Metodikutsun yhteydessä kutsupinoon luodaan uusi kehys, joka poistetaan metodikutsusta poistuttaessa. -->
16691675

0 commit comments

Comments
 (0)