-
Notifications
You must be signed in to change notification settings - Fork 3
/
materiaali-ohpe.html
11546 lines (10154 loc) · 518 KB
/
materiaali-ohpe.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ohjelmoinnin perusteet</title>
<link rel="stylesheet" href="stylesheets/menu.css" type="text/css" media="screen" />
<link rel="stylesheet" href="stylesheets/common-new.css" type="text/css" media="screen, print" />
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<!-- BEGIN MOOC SNIP -->
<script type="text/javascript" src="javascripts/html5shiv.js"></script>
<script type="text/javascript" src="javascripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="javascripts/exercises2.js"></script>
<script type="text/javascript" src="javascripts/common-new.js"></script>
<script type="text/javascript" src="javascripts/shjs/sh_main.min.js"></script>
<script type="text/javascript" src="javascripts/shjs/sh_java.min.js"></script>
<!-- END MOOC SNIP -->
<link rel="stylesheet" href="stylesheets/shjs/sh_style.css" type="text/css" media="screen, print" />
<link rel="stylesheet" href="stylesheets/common-new.css" type="text/css" media="screen, print" />
<script type="text/javascript" src="javascripts/jquery.easing.js"></script>
<!-- BEGIN MOOC SNIP -->
<script type="text/javascript">
jQuery(document).ready(function() {
$(".tehtava").hide();
$("div.toc_viikko").hide();
$(".heading").click(function() {
$(this).next(".tehtava").slideToggle(200);
});});
</script>
</head>
<!-- END MOOC SNIP -->
<body>
<p><small>Tämä materiaali on lisensoitu Creative Commons BY-NC-SA-lisenssillä, joten voit käyttää ja levittää sitä vapaasti, kunhan alkuperäisten tekijöiden nimiä ei poisteta. Jos teet muutoksia materiaaliin ja haluat levittää muunneltua versiota, se täytyy lisensoida samanlaisella vapaalla lisenssillä. Materiaalien käyttö kaupalliseen tarkoitukseen on ilman erillistä lupaa kielletty.</small></p>
<h1>Ohjelmoinnin perusteet</h1>
<p>Arto Vihavainen ja Matti Luukkainen</p>
<div id="toc" class="menu" data-first-chapter-index="1" data-first-exercise-index="1">
<div id="materiaali_toc"></div>
<!--div id="tehtavat_toc"></div-->
<div id="tehtavat_toc2"></div>
</div>
<section class="viikkoraja" id="0">
<div div class="viikkoraja-mooc" id="Huomautus lukijalle" tekija="Arto Vihavainen, Matti Luukkainen" deadline=" ">
</div>
</section>
<section class="viikkoraja" id="1" >
<div class="viikkoraja"></div>
<div class="viikkoraja-mooc" id="Viikko 1" deadline=" " data-first-chapter-index="1" data-first-exercise-index="1" data-first-week-index="1" data-first-chapter-index-mooc="1" data-first-exercise-index-mooc="1" data-first-week-index-mooc="1" tekija="Arto Vihavainen, Matti Luukkainen">
<h2>Huomautus lukijalle</h2>
<p>Tämä materiaali on tarkoitettu Helsingin yliopiston tietojenkäsittelytieteen laitoksen Ohjelmoinnin perusteet -kurssille. Materiaali pohjautuu syksyjen 2012-14 ja keväiden 2010-14 kurssimateriaaleihin, joiden sisältöön ovat vaikuttaneet Matti Paksula, Antti Laaksonen, Pekka Mikkola, Juhana Laurinharju, Martin Pärtel, Joel Kaasinen ja Mikael Nousiainen.</p>
<p>Lue materiaalia siten, että teet samalla lukemasi esimerkit itse. Esimerkkeihin kannattaa tehdä pieniä muutoksia ja tarkkailla, miten muutokset vaikuttavat ohjelman toimintaan. Äkkiseltään voisi luulla, että esimerkkien tekeminen ja muokkaaminen hidastaa opiskelua. Tämä ei kuitenkaan pidä paikkansa. Ohjelmoimaan ei ole vielä tietääksemme kukaan ihminen oppinut lukemalla (tai esim. luentoa kuuntelemalla). Oppiminen perustuu oleellisesti aktiiviseen tekemiseen ja rutiinin kasvattamiseen. Esimerkkien ja erityisesti erilaisten omien kokeilujen tekeminen on parhaita tapoja "sisäistää" luettua tekstiä. TMC:ssä on kokeiluja varten tehtäväpohja, jonka nimi on "Hiekkalaatikko".</p>
<p>Pyri tekemään tai ainakin yrittämään tehtäviä sitä mukaa kuin luet tekstiä. Jos et osaa heti tehdä jotain tehtävää, älä masennu, sillä saat ohjausta tehtävän tekemiseen <a href="http://mooc.cs.helsinki.fi/content/irc-ohjeet" target="_blank">IRC:ssä</a><span class="hidden-in-mooc"> ja pajassa</span>.</p>
<p>Tekstiä ei ole tarkoitettu vain kertaalleen luettavaksi. Joudut varmasti myöhemmin palaamaan jo aiemmin lukemiisi kohtiin tai aiemmin tekemiisi tehtäviin. Tämä teksti ei sisällä kaikkea oleellista ohjelmointiin liittyvää. Itse asiassa ei ole olemassa mitään kirjaa josta löytyisi kaikki oleellinen -- joudut etsimään tietoa myös omatoimisesti. Kurssin harjoitukset sisältävät jo jonkun verran ohjeita, mistä suunnista ja miten hyödyllistä tietoa on mahdollista löytää.</p>
<p>Muutamiin kohtiin olemme myös liittäneet screencasteja, eli videoita, joita katsomalla voi pelkän valmiin koodin lukemisen sijaan seurata miten ohjelma muodostuu. Tämän lisäksi materiaalissa on kyselyjä, joihin toivomme että vastaat rehellisesti. Kyselyjen vastauksia käytetään osana Helsingin yliopistolla tehtävää ohjelmoinnin oppimisen tutkimusta. Kyselyt ja tehtävät saa auki klikkaamalla kyselyn tai tehtävän nimeä.</p>
<div class="panel-group" id="1taustatiedot">
<div class="panel panel-default">
<a class="panel-title" data-toggle="collapse" data-parent="#1taustatiedot" href="#1taustatiedot2">
<div class="panel-heading">
<h4 id="taustatiedot-form-legend">Kysely: Taustatiedot</h4>
</div>
</a>
<div class="panel-collapse collapse" id="1taustatiedot2">
<div class="panel-body">
<!-- Taustatiedot -->
<form class="form-horizontal" id="taustatiedot-form">
<fieldset>
<p class="help-block">Käytämme taustatietoja kurssiemme kehittämiseen ja tutkimukseen. Taustatietoja ei luovuteta kolmansille osapuolille.</p>
<!-- TMC-tunnus-->
<div class="form-group">
<label class="col-md-4 control-label" for="taustatiedot-form-tmc-tunnus">TMC-tunnus</label>
<div class="col-md-4">
<input id="taustatiedot-form-tmc-tunnus" name="taustatiedot-form-tmc-tunnus" type="text" class="form-control input-md">
</div>
</div>
<!-- Syntymävuosi -->
<div class="form-group">
<label class="col-md-4 control-label" for="taustatiedot-form-syntymavuosi">Syntymävuosi</label>
<div class="col-md-1">
<input id="taustatiedot-form-syntymavuosi" name="taustatiedot-form-syntymavuosi" type="text" class="form-control" maxlength="4">
</div>
</div>
<!-- Sukupuoli -->
<div class="form-group">
<label class="col-md-4 control-label" for="sukupuoli">Sukupuoli</label>
<div class="col-md-4">
<div class="radio">
<label for="sukupuoli-nainen">
<input type="radio" name="sukupuoli" id="sukupuoli-nainen" value="sukupuoli-nainen">
Nainen
</label>
</div>
<div class="radio">
<label for="sukupuoli-mies">
<input type="radio" name="sukupuoli" id="sukupuoli-mies" value="sukupuoli-mies">
Mies
</label>
</div>
<div class="radio">
<label for="sukupuoli-muu">
<input type="radio" name="sukupuoli" id="sukupuoli-muu" value="sukupuoli-muu">
Muu tai en halua kertoa
</label>
</div>
</div>
</div>
<!-- Kätisyys -->
<div class="form-group">
<label class="col-md-4 control-label" for="katisyys">Kätisyys</label>
<div class="col-md-4">
<div class="radio">
<label for="katisyys-vasen">
<input type="radio" name="katisyys" id="katisyys-vasen" value="katisyys-vasen">
Vasen
</label>
</div>
<div class="radio">
<label for="katisyys-oikea">
<input type="radio" name="katisyys" id="katisyys-oikea" value="katisyys-oikea">
Oikea
</label>
</div>
<div class="radio">
<label for="katisyys-muu">
<input type="radio" name="katisyys" id="katisyys-muu" value="katisyys-muu">
Muu tai en halua kertoa
</label>
</div>
</div>
</div>
<!-- Koulutusaste -->
<div class="form-group">
<label class="col-md-4 control-label" for="koulutusaste">Koulutusaste</label>
<div class="col-md-4">
<div class="radio">
<label for="koulutusaste-peruskoulu-kansakoulu">
<input type="radio" name="koulutusaste" id="koulutusaste-peruskoulu-kansakoulu" value="koulutusaste-peruskoulu-kansakoulu">
Peruskoulu tai kansakoulu
</label>
</div>
<div class="radio">
<label for="koulutusaste-keskiaste">
<input type="radio" name="koulutusaste" id="koulutusaste-keskiaste" value="koulutusaste-keskiaste">
Keskiaste (lukio / ammatilliset oppilaitokset)
</label>
</div>
<div class="radio">
<label for="koulutusaste-ammattikorkeakoulu">
<input type="radio" name="koulutusaste" id="koulutusaste-ammattikorkeakoulu" value="koulutusaste-ammattikorkeakoulu">
Ammattikorkeakoulututkinto
</label>
</div>
<div class="radio">
<label for="koulutusaste-alempi-korkeakoulu">
<input type="radio" name="koulutusaste" id="koulutusaste-alempi-korkeakoulu" value="koulutusaste-alempi-korkeakoulu">
Alempi korkeakoulututkinto
</label>
</div>
<div class="radio">
<label for="koulutusaste-ylempi-korkeakoulu">
<input type="radio" name="koulutusaste" id="koulutusaste-ylempi-korkeakoulu" value="koulutusaste-ylempi-korkeakoulu">
Ylempi korkeakoulututkinto
</label>
</div>
<div class="radio">
<label for="koulutusaste-lisensiaatti-tai-tohtori">
<input type="radio" name="koulutusaste" id="koulutusaste-lisensiaatti-tai-tohtori" value="koulutusaste-lisensiaatti-tai-tohtori">
Lisensiaatin- tai tohtorintutkinto
</label>
</div>
</div>
</div>
<!-- Työssäkäynti -->
<div class="form-group">
<label class="col-md-4 control-label" for="tyossakaynti">Olen tällä hetkellä</label>
<div class="col-md-4">
<div class="radio">
<label for="tyossakaynti-opiskelija">
<input type="radio" name="tyossakaynti" id="tyossakaynti-opiskelija" value="tyossakaynti-opiskelija">
Opiskelija
</label>
</div>
<div class="radio">
<label for="tyossakaynti-opiskelija-ja-osaaikainen">
<input type="radio" name="tyossakaynti" id="tyossakaynti-opiskelija-ja-osaaikainen" value="tyossakaynti-opiskelija-ja-osaaikainen">
Opiskelija ja osa-aikaisesti töissä
</label>
</div>
<div class="radio">
<label for="tyossakaynti-opiskelija-ja-taysipaivaisesti-toissa">
<input type="radio" name="tyossakaynti" id="tyossakaynti-opiskelija-ja-taysipaivaisesti-toissa" value="tyossakaynti-opiskelija-ja-taysipaivaisesti-toissa">
Opiskelija ja täysipäiväisesti töissä
</label>
</div>
<div class="radio">
<label for="tyossakaynti-tyoelamassa">
<input type="radio" name="tyossakaynti" id="tyossakaynti-tyoelamassa" value="tyossakaynti-tyoelamassa">
Työelämässä
</label>
</div>
<div class="radio">
<label for="tyossakaynti-tyoton">
<input type="radio" name="tyossakaynti" id="tyossakaynti-tyoton" value="tyossakaynti-tyoton">
Työtön
</label>
</div>
<div class="radio">
<label for="tyossakaynti-vapaalla">
<input type="radio" name="tyossakaynti" id="tyossakaynti-vapaalla" value="tyossakaynti-vapaalla">
Vanhempainvapaalla tai muulla vastaavalla
</label>
</div>
<div class="radio">
<label for="tyossakaynti-elakkeella">
<input type="radio" name="tyossakaynti" id="tyossakaynti-elakkeella" value="tyossakaynti-elakkeella">
Eläkkeellä
</label>
</div>
</div>
</div>
<!-- tarkennus jos toissa -->
<div class="hidden" id="tyossakaynti-tarkennus">
<div class="form-group">
<label class="col-md-4 control-label" for="tyossakaynti-tunteja-viikossa">Kuinka monta tuntia teet töitä viikossa?</label>
<div class="col-md-4">
<input id="tyossakaynti-tunteja-viikossa" name="tyossakaynti-tunteja-viikossa" type="text" class="form-control input-md">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="tyossakaynti-tyonkuva">Kuvaile työnkuvaasi lyhyesti</label>
<div class="col-md-4">
<input id="tyossakaynti-tyonkuva" name="tyossakaynti-tyonkuva" type="text" class="form-control input-md">
</div>
</div>
</div>
<!-- tarkennus jos aiemmin toissa -->
<div class="hidden" id="tyossakaynti-aiempi-tarkennus">
<div class="form-group">
<label class="col-md-4 control-label" for="tyossakaynti-edellinen-tyonkuva">Jos olet ollut aiemmin töissä, kuvaile edellistä työnkuvaasi lyhyesti</label>
<div class="col-md-4">
<input id="tyossakaynti-edellinen-tyonkuva" name="tyossakaynti-edellinen-tyonkuva" type="text" class="form-control input-md">
</div>
</div>
</div>
<!-- ohjelmointikokemus -->
<div class="form-group">
<label class="col-md-4 control-label" for="ohjelmointikokemus">Aiempi ohjelmointikokemus</label>
<div class="col-md-4">
<div class="radio">
<label for="ohjelmointikokemus-ei-mitaan">
<input type="radio" name="ohjelmointikokemus" id="ohjelmointikokemus-ei-mitaan" value="ohjelmointikokemus-ei-mitaan">
En ole ohjelmoinut koskaan
</label>
</div>
<div class="radio">
<label for="ohjelmointikokemus-hieman">
<input type="radio" name="ohjelmointikokemus" id="ohjelmointikokemus-hieman" value="ohjelmointikokemus-hieman">
Olen osallistunut joskus ohjelmointikurssille
</label>
</div>
<div class="radio">
<label for="ohjelmointikokemus-itsekseni">
<input type="radio" name="ohjelmointikokemus" id="ohjelmointikokemus-itsekseni" value="ohjelmointikokemus-itsekseni">
Ohjelmoin silloin tällöin itsekseni
</label>
</div>
<div class="radio">
<label for="ohjelmointikokemus-ryhmassa-kurssi">
<input type="radio" name="ohjelmointikokemus" id="ohjelmointikokemus-ryhmassa-kurssi" value="ohjelmointikokemus-ryhmassa-kurssi">
Olen ohjelmoinut ryhmässä, esimerkiksi osana jotain kurssia
</label>
</div>
<div class="radio">
<label for="ohjelmointikokemus-ryhmassa-tyo">
<input type="radio" name="ohjelmointikokemus" id="ohjelmointikokemus-ryhmassa-tyo" value="ohjelmointikokemus-ryhmassa-tyo">
Olen osallistunut ohjelmistoprojektiin osana työtäni
</label>
</div>
<div class="radio">
<label for="ohjelmointikokemus-ryhmassa-tyo-useampi-projekti">
<input type="radio" name="ohjelmointikokemus" id="ohjelmointikokemus-ryhmassa-tyo-useampi-projekti" value="ohjelmointikokemus-ryhmassa-tyo-useampi-projekti">
Olen osallistunut useampaan ohjelmistoprojektiin osana työtäni
</label>
</div>
<div class="radio">
<label for="ohjelmointikokemus-muu">
<input type="radio" name="ohjelmointikokemus" id="ohjelmointikokemus-muu" value="ohjelmointikokemus-muu">
Muu, mikä?
</label>
<input id="ohjelmointikokemus-muu-teksti" name="ohjelmointikokemus-muu-teksti" type="text" class="form-control input-md hidden">
</div>
</div>
</div>
<div class="hidden" id="ohjelmointikokemus-tarkennus">
<div class="form-group">
<label class="col-md-4 control-label" for="ohjelmointikokemus-kaytetyt-ohjelmointikielet">Mitä ohjelmointikieliä olet käyttänyt?</label>
<div class="col-md-4">
<input id="ohjelmointikokemus-kaytetyt-ohjelmointikielet" name="ohjelmointikokemus-kaytetyt-ohjelmointikielet" type="text" class="form-control input-md">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="ohjelmointikokemus-tuntia-ohjelmoinut">Kuinka monta tuntia olet ohjelmoinut (arviolta)?</label>
<div class="col-md-4">
<input id="ohjelmointikokemus-tuntia-ohjelmoinut" name="ohjelmointikokemus-tuntia-ohjelmoinut" type="text" class="form-control input-md">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="ohjelmointikokemus-suurin-rivimaara">Kuinka monta riviä ohjelmakoodia suurimmassa kirjoittamassasi ohjelmassa on ollut (arviolta)?</label>
<div class="col-md-4">
<input id="ohjelmointikokemus-suurin-rivimaara" name="ohjelmointikokemus-suurin-rivimaara" type="text" class="form-control input-md">
</div>
</div>
</div>
<!-- Osallistuminen -->
<div class="form-group">
<label class="col-md-4 control-label" for="osallistuminen">Osallistun kurssille (valitse sopivat)</label>
<div class="form-inline col-md-8">
<div class="checkbox col-md-4">
<label for="osallistuminen-tutustuakseni-ohjelmointiin">
<input type="checkbox" name="osallistuminen" id="osallistuminen-tutustuakseni-ohjelmointiin" value="osallistuminen-tutustuakseni-ohjelmointiin">
Tutustuakseni ohjelmointiin
</label>
</div>
<div class="checkbox col-md-4">
<label for="osallistuminen-tutustuakseni-kurssin-toteutukseen">
<input type="checkbox" name="osallistuminen" id="osallistuminen-tutustuakseni-kurssin-toteutukseen" value="osallistuminen-tutustuakseni-kurssin-toteutukseen">
Tutustuakseni kurssin toteutustapaan
</label>
</div>
<div class="checkbox col-md-4">
<label for="osallistuminen-saadakseni-opintopisteita-tai-kurssisuorituksen">
<input type="checkbox" name="osallistuminen" id="osallistuminen-saadakseni-opintopisteita-tai-kurssisuorituksen" value="osallistuminen-saadakseni-opintopisteita-tai-kurssisuorituksen">
Saadakseni opintopisteitä tai kurssisuorituksen
</label>
</div>
<div class="checkbox col-md-4">
<label for="osallistuminen-oppiakseni-ohjelmointia-huvin-vuoksi">
<input type="checkbox" name="osallistuminen" id="osallistuminen-oppiakseni-ohjelmointia-huvin-vuoksi" value="osallistuminen-oppiakseni-ohjelmointia-huvin-vuoksi">
Oppiakseni ohjelmointia, huvin vuoksi
</label>
</div>
<div class="checkbox col-md-4">
<label for="osallistuminen-oppiakseni-ohjelmointia-opintoja-varten">
<input type="checkbox" name="osallistuminen" id="osallistuminen-oppiakseni-ohjelmointia-opintoja-varten" value="osallistuminen-oppiakseni-ohjelmointia-opintoja-varten">
Oppiakseni ohjelmointia, opintoja varten
</label>
</div>
<div class="checkbox col-md-4">
<label for="osallistuminen-oppiakseni-ohjelmointia-toita-varten">
<input type="checkbox" name="osallistuminen" id="osallistuminen-oppiakseni-ohjelmointia-toita-varten" value="osallistuminen-oppiakseni-ohjelmointia-toita-varten">
Oppiakseni ohjelmointia, töitä varten
</label>
</div>
<div class="checkbox col-md-4">
<label for="osallistuminen-paivittaakseni-ohjelmointitaitojani">
<input type="checkbox" name="osallistuminen" id="osallistuminen-paivittaakseni-ohjelmointitaitojani" value="osallistuminen-paivittaakseni-ohjelmointitaitojani">
Päivittääkseni ohjelmointitaitojani
</label>
</div>
<div class="checkbox col-md-4">
<label for="osallistuminen-koska-ilmainen">
<input type="checkbox" name="osallistuminen" id="osallistuminen-koska-ilmainen" value="osallistuminen-koska-ilmainen">
Koska se on ilmainen
</label>
</div>
<div class="checkbox col-md-4">
<label for="osallistuminen-koska-pidan-verkko-opiskelusta">
<input type="checkbox" name="osallistuminen" id="osallistuminen-koska-pidan-verkko-opiskelusta" value="osallistuminen-koska-pidan-verkko-opiskelusta">
Koska pidän verkko-opiskelusta
</label>
</div>
<div class="checkbox col-md-4">
<label for="osallistuminen-koska-kurssia-suositeltiin-minulle">
<input type="checkbox" name="osallistuminen" id="osallistuminen-koska-kurssia-suositeltiin-minulle" value="osallistuminen-koska-kurssia-suositeltiin-minulle">
Koska sitä suositeltiin minulle
</label>
</div>
<div class="checkbox col-md-8">
<label for="osallistuminen-muu">
<input type="checkbox" name="osallistuminen" id="osallistuminen-muu" value="osallistuminen-muu">
Muu, mikä?
</label>
<input id="osallistuminen-muu-teksti" name="osallistuminen-muu-teksti" type="text" class="form-control input-md hidden">
</div>
</div>
</div>
<input type="hidden" id="taustatiedot-form-started-entering" name="taustatiedot-form-started-entering" value="">
<div class="form-group">
<label class="col-md-4 control-label"> </label>
<div class="form-inline col-md-8">
<strong><label id="taustatiedot-form-validation-feedback" class="label label-danger hidden">Tarkistathan että vastasit kaikkiin kenttiin!</label></strong>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label"> </label>
<div class="form-inline col-md-8">
<button type="button" id="taustatiedot-form-submit" class="btn btn-primary">Lähetä lomake</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
<h2 id="koodia">Ohjelma ja lähdekoodi</h2>
<h3>Lähdekoodi</h3>
<p>Ohjelma muodostuu <em>lähdekoodista</em>. Tietokone suorittaa lähdekoodissa olevia <em>komentoja</em> pääsääntöisesti <em>ylhäältä alaspäin</em> ja <em>vasemmalta oikealle</em>. Lähdekoodi talletetaan tekstimuodossa ja <em>suoritetaan</em> jollakin tavalla.</p>
<h3>Komennot</h3>
<p>Varsinaisesti ohjelma muodostuu lähdekoodiin kirjoitetuista komennoista. Tietokone suorittaa eri <em>operaatioita</em>, eli toimintoja, komentojen perusteella. Esimerkiksi merkkijonon, eli tekstin, "Hei maailma"-tulostuksessa tärkein komento on <code>System.out.println</code>.</p>
<pre class="sh_java">
System.out.println("Hei maailma");
</pre>
<p>Komento <code>System.out.println</code> tulostaa sille sulkeiden sisällä annetun merkkijonon. Pääte <code>ln</code> on lyhenne sanasta <i>line</i>. Komento siis tulostaa rivin, eli kun annettu merkkijono on tulostettu, tulostaa komento myös rivinvaihdon.</p>
<h3>Kääntäjä ja tulkki</h3>
<p>Tietokone ei suoraan ymmärrä käyttämäämme ohjelmointikieltä. Tarvitsemme lähdekoodin ja tietokoneen väliin <em>kääntäjän</em>. Ohjelmoidessamme komentoriviä käyttäen, komento <code>javac Hei.java</code> kääntää <code>Hei.java</code>-tiedoston <i>tavukoodiksi</i>, jota voidaan ajaa java-tulkin avulla. Käännetty ohjelma ajetaan komentoriviltä komennolla <code>java Hei</code>, missä Hei on käännetyn java-lähdekooditiedoston nimi.</p>
<p>Käyttäessämme modernia ohjelmointiympäristöä (tästä myöhemmin lisää), ohjelmointiympäristö hoitaa lähdekoodin kääntämisen. Valitessamme ohjelman suorittamisen, ohjelmointiympäristö kääntää ja suorittaa ohjelman. Ohjelmointiympäristöt kääntävät ohjelmakoodia ohjelmoijan sitä kirjoittaessa, jolloin yksinkertaiset virheet huomataan ennen ohjelman suoritusta.</p>
<h3>Komennon osia</h3>
<h4>Puolipiste</h4>
<p>Puolipisteellä <code>;</code> erotetaan komennot toisistaan. Kääntäjä ja tulkki ei siis ole kiinnostunut lähdekoodissa olevista riveistä, vaan voimme kirjoittaa koko ohjelman yhdelle riville.</p>
<p>Alla olevassa esimerkissä käytetään komentoa <code>System.out.print</code>, joka on kuten komento <code>System.out.println</code> ilman rivinvaihtoa. <code>System.out.print</code>-komento ei siis tulosta rivinvaihtoa tekstin tulostamisen jälkeen.</p>
<p><b>Esimerkki puolipisteiden käytöstä</b></p>
<pre class="sh_java">
System.out.print("Hei "); System.out.print("maailma");
System.out.print("!");
</pre>
<pre>
Hei maailma!
</pre>
<p>Vaikka kääntäjä ja tulkki eivät tarvitse rivinvaihtoja, on niiden käyttö hyvin tärkeää muita ihmisiä ajatellen. Selkeä lähdekoodin osien erottelu vaatii rivinvaihtojen käyttöä. Tätä ja muita lähdekoodin luettavuuteen liittyviä seikkoja tullaan painottamaan tällä kurssilla.</p>
<h4>Komennoille lähdetettävät "tiedot" eli parametrit</h4>
<p>Komennon käsittelemä tieto eli <em>komennon parametrit</em> lähetetään komennolle lisäämällä ne komennon nimen perässä olevien sulkujen <code>()</code> sisään. Esimerkiksi <code>System.out.print</code> -komennon parametriksi annetaan teksti <em>hei</em> seuraavasti: <code>System.out.print("hei")</code>. </p>
<h4>Kommentit</h4>
<p>Lähdekoodin <code>kommentit</code> ovat kätevä tapa merkitä asioita itselle ja muille muistiin. Kommentti on mikä tahansa rivi, joka alkaa kahdella vinoviivalla <code>//</code>. Kaikki kommenttimerkkiä seuraava samalla rivillä oleva teksti tulkitaan kommentiksi.</p>
<h4>Esimerkki kommenttien käytöstä</h4><br>
<pre class="sh_java">
// Tulostamme tekstin "Hei maailma"
System.out.print("Hei maailma");
System.out.print(" ja kaikki sen ihmiset."); // Lisäämme samalle riville tekstiä.
// System.out.print("tätä riviä ei suoriteta koska se on kommentoitu ulos");
</pre>
<p>Esimerkissä alin rivi esittelee erityisen kätevän käyttökohteen kommenteille: kirjoitettua koodia ei tarvitse poistaa jos haluaa tilapäisesti kokeilla jotain.</p>
<h3>Lisää tulostamisesta</h3>
<p>Kuten aiemmin huomattiin, tulostamiseen on kaksi komentoa:</p>
<p>
<ul>
<li> <code>System.out.print</code> tulostaa tekstin ilman loppurivinvaihtoa</li>
<li> <code>System.out.println</code> tulostaa tekstin ja loppurivinvaihdon</li>
</ul>
</p>
<p>Tulostettavan tekstin osana voi olla erikoismerkkejä. Tärkein näistä on <code>\n</code>, joka vaihtaa riviä. Erikoismerkkejä on <a href="http://en.wikipedia.org/wiki/Escape_character" title="Escape character - Wikipedia, the free encyclopedia">muitakin</a>.</p>
<pre class="sh_java">
System.out.println("Ensimmäinen\nToinen\nKolmas");
</pre>
<p>Ylläoleva tulostaa suoritettaessa seuraavaa:</p>
<pre>
Ensimmäinen
Toinen
Kolmas
</pre>
<h3>Pääohjelmarunko</h3>
<p>Ohjelman "Esimerkki" runko on seuraavanlainen. </p>
<pre class="sh_java">
public class Esimerkki {
public static void main(String[] args) {
// ohjelmakoodi
}
}
</pre>
<p>Ohjelma sijaitsee samannimisessä <i>.java</i>-päätteisessä tiedostossa. Ohjelman <i>Esimerkki</i> täytyy siis sijaita tiedostossa,
jonka nimi on <code>Esimerkki.java</code>.</p>
<p>Ohjelmaa suoritettaessa suoritetaan alue, joka on rungossamme merkitty kommentilla <i>ohjelmakoodi</i>. Ohjelmoimme ensimmäisellä viikolla
vain tälle alueelle. Kun puhumme komennoista, esimerkiksi tulostamisesta, tulee komennot kirjoittaa ohjelmarungon sisälle. Esimerkiksi <code>System.out.print("Tulostettava teksti");</code></p>
<pre class="sh_java">
public class Esimerkki {
public static void main(String[] args) {
System.out.print("Tulostettava teksti");
}
}
</pre>
<p>Jatkossa esimerkeissä ei erikseen näytetä pääohjelmarunkoa.</p>
<h3>Tutustuminen ohjelmointiympäristöön</h3>
<div>
<p>Nykyaikainen ohjelmointi tapahtuu lähes poikkeuksetta ohjelmointiympäristössä. Ohjelmointiympäristö sisältää joukon ohjelmoijaa auttavia aputoimintoja. Ohjelmointiympäristö ei rakenna ohjelmaa ohjelmoijan puolesta, mutta se muunmuassa vinkkaa helpoista virheistä ohjelmakoodissa ja auttaa ohjelmoijaa hahmottamaan ohjelman rakennetta.</p>
<div class="help">
<p>Käytämme tällä kurssilla <a href="http://netbeans.org/">NetBeans</a>-nimistä ohjelmointiympäristöä.
Ohje ohjelmoinnin aloittamiseen NetBeans-ohjelmointiympäristössä löytyy <a class="hidden-in-hy" href=" http://mooc.cs.helsinki.fi/ohjelmointi/ohjeita#netbeans-ohjeita">täältä</a><a class="hidden-in-mooc" href="http://www.cs.helsinki.fi/group/java/k14/ohpe/nb-asennus.html">täältä</a>.
</p>
<p>Jatkossa julkaisemme pikkuhiljaa lisää ohjeita NetBeansin käyttöön. Vaikka ohjelma tuntuisi nyt sekavalta, älä hätäile. NetBeans on loppujenlopuksi hyvin helppokäyttöinen. Perusteet opit 5 minuutissa, ja kurssin myötä opit koko ajan hieman lisää ja kesäkuussa olet jo todellinen NetBeans "poweruser".</p>
</div>
<p>Ennen kuin tiedät mitä teet, <b>toimi täsmälleen ohjeen kuvaamalla tavalla</b>. Useimmat seuraavista tehtävänannoista näyttävät mitä pyydetyn tehtävän tulisi tulostaa ruudulle toimiakseen oikein. </p>
<p>HUOM: älä tee tehtäviä siten että pelkästään kirjoitat koodia ja painelet testinappia. Suorita myös koodia normaaliin tapaan (vihreällä nuolella) ja katso mitä ruudulle tulostuu. Erityisesti jos ohjelma ei meinaa mennä testeistä läpi, kannattaa varmistaa normaalisti suorittamalla että ohjelma toimii silmämääräisesti niinkuin sen pitäisi.</p>
</div>
<!-- syntax ok-->
<div class="tehtavat" id="viikko1">
<p class="help">Seuraavissa tehtävissä harjoitellaan tuntuman saamista NetBeansiin ja ruudulle tulostamista.</p>
<p><strong>Muista ensin katsoa ohje NetBeansin käyttöön</strong>
<!-- TODO fix links -->
<a href="http://mooc.cs.helsinki.fi/ohjelmointi/ohjeita#netbeans-ohjeita">täältä</a>
</p>
<p class="help"><strong>Jos et ole vielä käynyt vastaamassa taustatietokyselyyn, tee se NYT <a href="#1taustatiedot">klikkaamalla tästä</a></strong></p>
<p><strong>Näet tehtävänannon klikkaamalla tehtävän nimeä</strong></p>
<NEXTWEEK></NEXTWEEK>
<div class="tehtava">
<h3 class="req">Nimi</h3>
<p>Tee ohjelma, joka tulostaa nimesi.</p>
<p>Ohjelman tulostus voi olla seuraava:</p>
<pre>
Oskari Opiskelija
</pre>
</div>
<div class="tehtava">
<h3>Hei Maailma! (Ja Mualima!)</h3>
<p>Tee ohjelma, jonka tulostus on seuraava:</p>
<pre>
Hei Maailma!
(Ja Mualima!)
</pre>
</div>
<div class="tehtava">
<h3 class="req">Kuusi</h3>
<p>Tee ohjelma, jonka tulostus on seuraava:</p>
<pre>
*
***
*****
*******
*********
*
</pre>
<div class="help">
<p><b>HUOM:</b> kirjoitit todennäköisesti aika monta kertaa <code>System.out.println("...")</code>. Kokeile kirjoittaa NetBeans:iin (main:in sisään) tyhjälle riville <i>sout</i> ja paina tabulaatoria (näppäin q:n vasemmalla puolella). Mitä tapahtuu? Tämä pieni apuväline säästänee jatkossa runsaasti aikaasi.</p>
</div>
</div>
</div>
<h2 id="muuttuja_ja_sijoitus">Muuttuja ja sijoitus</h2>
<h3>Muuttujat ja tietotyypit</h3>
<p>Ohjelmoinnissa eräs keskeinen käsite on <em>muuttuja</em>. Muuttuja kannattaa ajatella lokerona, johon voi tallettaa tietoa. Muuttujaan talletettavalla tiedolla on aina tyyppi. Tyyppejä ovat esimerkiksi teksti eli merkkijono (<em>String</em>), kokonaisluku (<em>int</em>), liukuluku (<em>double</em>) ja totuusarvo (<em>boolean</em>). Muuttujaan asetetaan <em>arvo</em> yhtäsuuruusmerkillä (<code>=</code>). </p>
<pre class="sh_java">
int kuukausia = 12;
</pre>
<p>Yllä olevassa asetuslauseessa asetetaan kokonaisluku-tyyppiä (<em>int</em>) olevaan muuttujaan nimeltä <em>kuukausia</em> arvo 12. Asetuslause luetaan "muuttuja <em>kuukausia</em> saa <em>arvon</em> 12".</p>
<p>Muuttujan arvo voidaan yhdistää merkkijonoon <code>+</code>-merkillä
seuraavan esimerkin mukaisesti.</p>
<!--
<h4>Esimerkkejä muuttujista ja tietotyypeistä</h4>
-->
<pre class="sh_java">
String teksti = "sisältää tekstiä";
int kokonaisluku = 123;
double liukuluku = 3.141592653;
boolean onkoTotta = true;
System.out.println("Tekstimuuttujan arvo on " + teksti);
System.out.println("Kokonaislukumuuttujan arvo on " + kokonaisluku);
System.out.println("Liukulukumuuttujan arvo on " + liukuluku);
System.out.println("Totuusarvomuuttujan arvo on " + onkoTotta);
</pre>
<p>Tulostus:</p>
<pre>
Tekstimuuttujan arvo on sisältää tekstiä
Kokonaislukumuuttujan arvo on 123
Liukulukumuuttujan arvo on 3.141592653
Totuusarvomuuttujan arvo on true
</pre>
<p>Muuttuja säilyttää arvonsa kunnes siihen asetetaan toinen arvo. Huomaa että muuttujan tyyppi kirjoitetaan vain kun muuttuja esitellään ohjelmassa ensimmäistä kertaa.</p>
<pre class="sh_java">
int kokonaisluku = 123;
System.out.println("Kokonaislukumuuttujan arvo on " + kokonaisluku);
kokonaisluku = 42;
System.out.println("Kokonaislukumuuttujan arvo on " + kokonaisluku);
</pre>
<p>Tulostuu:</p>
<pre>
Kokonaislukumuuttujan arvo on 123
Kokonaislukumuuttujan arvo on 42
</pre>
<h3>Muuttujan tyyppi pysyy</h3>
<p>Kun muuttujan tyyppi on kertaalleen määritelty, ei se enää muutu. Esimerkiksi merkkijonomuuttuja ei voi muuttua kokonaislukumuuttujaksi, eikä siihen voi asettaa kokonaislukua.</p>
<pre class="sh_java">
String merkkijono = "tsuppadui!";
merkkijono = 42; // Ei onnistu! :(
</pre>
<p>Liukulukuun voi asettaa kokonaisluvun, sillä kokonaisluku on myös liukuluku</p>
<pre class="sh_java">
double liukuluku = 0.42;
liukuluku = 1; // Onnistuu! :)
</pre>
<div class="tehtavat">
<div class="tehtava">
<h3 class="req">Muuttuvat muuttujat</h3>
<p>Tehtäväpohja sisältää ohjelman, joka tulostaa seuraavaa.</p>
<pre>
Kanoja:
3
Pekonia (kg):
5.5
Traktori:
Ei ole!
Tässä vielä tiivistelmä:
3
5.5
Ei ole!
</pre>
<p>Muuta ohjelmaa annetuista kohdista niin että tuloste on:</p>
<pre>
Kanoja:
9000
Pekonia (kg):
0.1
Traktori:
Zetor
Tässä vielä tiivistelmä:
9000
0.1
Zetor
</pre>
</div>
</div>
<h3>Sallittu ja kuvaava muuttujan nimi</h3>
<p>Muuttujan nimeämistä rajoittavat tietyt ehdot. Vaikka muuttujan nimessä voidaan käyttää ääkkösiä, on parempi olla <em>kayttamatta niita</em>, sillä <a href="http://en.wikipedia.org/wiki/Character_encoding" title="Character encoding - Wikipedia, the free encyclopedia">merkistökoodauksesta</a> saattaa tulla ongelmia.</p>
<p>Muuttujan nimessä ei saa olla tiettyjä erikoismerkkejä, kuten huutomerkkejä (!). Välilyönti ei ole sallittu, sillä se erottaa komentojen osat toisistaan. Välilyönti kannattaa korvata <em><a href="http://fi.wikipedia.org/wiki/CamelCase" title="CamelCase – Wikipedia">camelCase</a></em>-tyylillä, jolloin nimi <code>muistuttaneeKamelia</code>. <b>Huom!</b> Muuttujien nimien ensimmäinen kirjain kirjoitetaan aina pienellä:</p>
<pre class="sh_java">
int camelCaseMuuttuja = 7;
</pre>
<p>Numeroita voidaan käyttää muuttujan nimessä, kunhan nimi ei ala numerolla. Nimi ei myöskään voi koostua pelkistä numeroista.</p>
<pre class="sh_java">
int 7muuttuja = 4; // Ei sallittu!
int muuttuja7 = 4; // Sallittu, mutta ei kuvaava muuttujan nimi
</pre>
<p>Muuttujan nimi ei myöskään saa olla jo entuudestaan käytössä. Tälläisiä nimiä ovat mm. aikaisemmin määritellyt muuttujat ja komennot, kuten <code>System.out.print</code> ja <code>System.out.println</code>.</p>
<pre class="sh_java">
int camelCase = 2;
int camelCase = 5; // Ei sallittu -- muuttuja camelCase on jo käytössä!
</pre>
<p>Muuttuja kannattaa nimetä siten, että sen käyttötarkoitus on selvää ilman kommentteja tai miettimistä. Tällä kurssilla muuttujat <strong>pitää</strong> nimetä kuvaavasti.</p>
<h4>Sallittuja muuttujien nimiä</h4>
<p>
<ul>
<li>kuukaudenViimeinenPaiva = 20</li>
<li>ensimmainenVuosi = 1952</li>
<li>nimi = "Matti"</li>
</ul>
</p>
<h4>Virheellisiä muuttujien nimiä</h4>
<p>
<ul>
<li>kuukauden viimeinen päivä = 20</li>
<li>1paiva = 1952</li>
<li>varo! = 1910</li>
<li>1920 = 1</li>
</ul>
</p>
<h2 id="laskentaa">Laskentaa</h2>
<p>Laskentaoperaatiot ovat varsin suoraviivaisia: <code>+</code>, <code>-</code>, <code>*</code> ja <code>/</code>. Erikoisempana operaationa on <code>%</code>, joka on jakojäännös, eli modulo. Laskentajärjestys on myös varsin suoraviivainen: operaatiot lasketaan vasemmalta oikealle sulut huomioon ottaen. Kuitenkin <code>*</code> ja <code>/</code> laskentaan ennen <code>+</code> ja <code>-</code> operaatioita. <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html">Tässä vielä tarkemmin laskujärjestyksestä javassa.</a> Linkin takana oleva materiaali ei ole kuitenkaan aloittelijan kannalta kovin oleellista.</p>
<pre class="sh_java">
int eka = 2;
System.out.println(eka); // tulostaa 2
int toka = 4;
System.out.println(toka); // tulostaa 4
int summa = eka + toka; // muuttujaan summa asetetaan muuttujien eka ja toka arvojen summa
System.out.println(summa); // tulostaa 6
</pre>
<pre class="sh_java">
int laskuSuluilla = (1 + 1) + 3 * (2 + 5);
System.out.println(laskuSuluilla); // tulostaa 23
int laskuSuluitta = 1 + 1 + 3 * 2 + 5;
System.out.println(laskuSuluitta); // tulostaa 13
</pre>
<p>Yllä olevan sulkuesimerkin voi suorittaa myös askeleittain.</p>
<pre class="sh_java">
int laskuSuluilla = (1 + 1);
System.out.println(laskuSuluilla); // tulostaa 2
laskuSuluilla = laskuSuluilla + 3 * (2 + 5);
System.out.println(laskuSuluilla); // tulostaa 23
int laskuSuluitta = 1 + 1;
laskuSuluitta = laskuSuluitta + 3 * 2;
laskuSuluitta = laskuSuluitta + 5;
System.out.println(laskuSuluitta); // tulostaa 13
</pre>
<p>Laskentaoperaatioita voidaan suorittaa lähes missä tahansa kohdassa ohjelmakoodia.</p>
<pre class="sh_java">
int eka = 2;
int toka = 4;
System.out.println(eka+toka); // tulostaa 6
System.out.println(2 + toka - eka - toka); // tulostaa 0
</pre>
<h3 id="liuku_eli_desimaali">Liukuluvut eli desimaaliluvut</h3>
<p>Kokonaislukujen jako ja jakojäännös ovat hieman hankalampia. Liukuluku ja kokonaisluku menevät helposti sekaisin. Jos kaikki laskuoperaatiossa olevat muuttujat ovat kokonaislukuja, on tulos myös kokonaisluku.</p>
<pre class="sh_java">
int tulos = 3 / 2;
System.out.println(tulos); // Huom! tulostaa 1 (kokonaisluku), sillä 3 ja 2 ovat myös kokonaislukuja
</pre>
<pre class="sh_java">
int eka = 3:
int toka = 2;
double tulos = eka / toka;
System.out.println(tulos); // nytkin tulostus on 1, sillä eka ja toka ovat kokonaislukuja
</pre>
<p>Jakojäännös-operaation (%) avulla saadaan selville jakojäännös. Esimerkiksi laskun <code>7 % 2</code> jakojäännös on 1.</p>
<pre class="sh_java">
int jakojaannos = 7 % 2;
System.out.println(jakojaannos); // tulostaa 1
</pre>
<p>Jos jakolaskun jakaja tai jaettava (tai molemmat!) ovat liukulukuja, tulee tulokseksi myös liukuluku</p>
<pre class="sh_java">
double kunJaettavaOnLiukuluku = 3.0 / 2;
System.out.println(kunJaettavaOnLiukuluku); // tulostaa 1.5
double kunJakajaOnLiukuluku = 3 / 2.0;
System.out.println(kunJakajaOnLiukuluku); // tulostaa 1.5
</pre>
<p>Kokonaisluku voidaan tarvittaessa muuttaa liukuluvuksi lisäämällä sen eteen tyyppimuunnosoperaatio <code>(double)</code>:</p>
<pre class="sh_java">
int eka = 3;
int toka = 2;
double tulos1 = (double)eka / toka;
System.out.println(tulos1); // tulostaa 1.5
double tulos2 = eka / (double)toka;
System.out.println(tulos2); // tulostaa 1.5
double tulos3 = (double)(eka / toka);
System.out.println(tulos3); // tulostaa 1
</pre>
<p>Jälkimmäisessä tulos pyöristyy väärin sillä laskuoperaatio kokonaisluvuilla suoritetaan ennen tyyppimuunnosta.</p>
<p>Jos jakolaskun tulos asetetaan kokonaislukutyyppiseen muuttujaan, on tulos automaattisesti kokonaisluku</p>
<pre class="sh_java">
int tulosKokonaislukuKoskaTyyppiKokonaisluku = 3.0 / 2; // tulos automaattisesti kokonaisluku: 1
</pre>
<p>Seuraava esimerkki tulostaa "1.5", sillä jaettavasta tehdään liukuluku kertomalla se liukuluvulla (1.0 * 3 = 3.0) ennen jakolaskua.</p>
<pre class="sh_java">
int jaettava = 3;
int jakaja = 2;
double tulos = 1.0 * jaettava / jakaja;
System.out.println(tulos);
</pre>
<p>Mitä seuraava tulostaa?</p>
<pre class="sh_java">
int jaettava = 3;
int jakaja = 2;
double tulos = jaettava / jakaja * 1.0;
System.out.println(tulos);
</pre>
<p> <big> Huomioi, että nimeät muuttujat nyt ja jatkossakin yllä esiteltyjen hyvien käytäntöjen mukaan. </big> </p>
<div class="tehtavat">
<div class="tehtava">
<h3 class="req">Sekunnit vuodessa</h3>
<p>Tee ohjelma, joka laskee, kuinka monta sekuntia on vuodessa.
Voit olettaa, että vuodessa on 365 päivää (eli ei ole karkausvuosi).</p>
<p>Ohjelman tulostus on seuraava:</p>
<pre>
Vuodessa on X sekuntia.
</pre>
<p>X:n kohdalle tulee ohjelmasi laskema tulos.</p>
</div>
</div>
<h2 id="katenointi">Katenointi eli merkkijonojen yhdistäminen</h2>
<p>Tarkastellaan vielä lähemmin merkkijonojen
yhdistämistä <code>+</code>-merkinnän avulla.</p>
<p>Jos operaatiota <code>+</code> sovelletaan kahden merkkijonon välille, syntyy uusi merkkijono, jossa kaksi merkkijonoa on yhdistetty. Huomaa nokkela välilyönnin käyttö lauseen "muuttujien" osana!</p>
<pre class="sh_java">
String tervehdys = "Hei ";
String nimi = "Matti";
String hyvästely = ", ja näkemiin!";
String lause = tervehdys + nimi + hyvästely;
System.out.println(lause);
</pre>
<pre>
Hei Matti, ja näkemiin!
</pre>
<p>Jos toinen operaation <code>+</code> kohteista on merkkijono, syntyy uusi merkkijono, jossa esimerkiksi kokonaisluku <code>2</code> on muutettu merkkijonoksi "2" ja tähän yhdistetty haluttu merkkijono.</p>
<pre class="sh_java">
System.out.println("tuossa on kokonaisluku --> " + 2);
System.out.println( 2 + " <-- tuossa on kokonaisluku");
</pre>
<p>Edellä esitellyt laskusäännöt pätevät täälläkin:</p>
<pre class="sh_java">
System.out.println("Neljä: " + (2+2));
System.out.println("Mutta! kaksikymmentäkaksi: " + 2 + 2);
</pre>
<pre>
Neljä: 4
Mutta! kaksikymmentäkaksi: 22
</pre>
<p>Edellisiä tietoja yhdistelemällä pystymme tulostamaan muuttujan arvoja ja tekstiä sekaisin:</p>
<pre class="sh_java">
int x = 10;
System.out.println("muuttujan x arvo on: " + x);
int y = 5;
int z = 6;
System.out.println("y on " + y + " ja z on " + z);
</pre>
<p>Tämä ohjelma tulostaa tietenkin:</p>
<pre>
muuttujan x arvo on: 10
y on 5 ja z on 6
</pre>
<div class="tehtavat">
<div class="tehtava">
<h3 class="req">Yhteenlasku</h3>
<p>Tee ohjelma, jonka avulla voidaan laskea kahden kokonaisluvun summa.
Ohjelman alussa määritellään kaksi muuttujaa,
jotka sisältävät summattavat luvut.
Voit tarvittaessa käyttää myös muita muuttujia.</p>
<p>Esimerkiksi jos muuttujissa on luvut 5 ja 4,
ohjelman tulostus on seuraava:</p>
<pre>
5 + 4 = 9
</pre>
<p>Jos taas muuttujissa on luvut 73457 ja 12888,
ohjelman tulostus on seuraava:</p>
<pre>
73457 + 12888 = 86345
</pre>
</div>
<div class="tehtava">
<h3 class="">Kertolasku</h3>
<p>Tee edellistä ohjelmaa vastaava ohjelma,
joka laskee kahden kokonaislukumuuttujaan sijoitetun arvon kertolaskun.</p>
<p>Esimerkiksi jos muuttujissa on luvut 2 ja 8,
ohjelman tulostus on seuraava:</p>
<pre>
2 * 8 = 16
</pre>
<p>Jos taas muuttujissa on luvut 277 ja 111,
ohjelman tulostus on seuraava:</p>
<pre>
277 * 111 = 30747
</pre>
<p>Kuinka suuren kertolaskun ohjelmasi
pystyy laskemaan?</p>
</div>
</div>
<h2 id="syötteen_lukeminen">Käyttäjän syötteen lukeminen</h2>
<p>Tähän asti ohjelmamme ovat olleet kovin yksipuolisia. Seuraavaksi luemme <em>syötettä</em> käyttäjältä. Käytämme syötteen lukemiseen erityistä <em>Scanner</em>-apuvälinettä.</p>
<p>Lisätään <em>Scanner</em> valmiiseen pääohjelmarunkoomme. Älä hätäile vaikka pääohjelmarunko saattaa näyttää vaikeaselkoiselta, jatkamme koodausta kuten ennenkin, eli kohtaan mikä on merkattu kommentilla <em>ohjelmakoodi</em>.</p>
<pre class="sh_java">
import java.util.Scanner;
public class OhjelmaRunko {
public static void main(String[] args) {
Scanner lukija = new Scanner(System.in);
// ohjelmakoodi
}
}
</pre>
<h3 id="syote_simppeli">Merkkijonon lukeminen</h3>
<p>Seuraava koodi lukee käyttäjän nimen ja tulostaa tervehdyksen:</p>
<pre class="sh_java">
System.out.print("Mikä on nimesi? ");
String nimi = lukija.nextLine(); // Luetaan käyttäjältä rivi tekstiä ja asetetaan se muuttujaan nimi
System.out.println("Hei, " + nimi);
</pre>
<pre>
Mikä on nimesi? <span class="red">Matti</span>
Hei, Matti
</pre>
<p>(Seuraavassa on yllä oleva ohjelma pääohjelmarungon kanssa. Ohjelman nimi on <em>Tervehdys</em>. Koska ohjelman nimi on Tervehdys, täytyy sen sijaita tiedostossa <code>Tervehdys.java</code> -- Käyttäessäsi NetBeans-ohjelmointiympäristöä, toimi kuten alussa olleessa
<!-- BEGIN mini MOOC SNIP-->
<a class="ahy" href="netbeansohje.html">ohjeessa</a>
<!-- END mini MOOC SNIP-->
<!-- BEGIN mini SNIP-->
<a class="amooc" href="http://mooc.cs.helsinki.fi/ohjelmointi/ilmoittautuminen-ja-alkutoimet">ohjeessa</a>
<!-- END mini SNIP-->
tehtiin, mutta muuta ohjelman nimi Heistä (<em>Hei</em>) Tervehdykseksi (<em>Tervehdys</em>)</p>
<pre class="sh_java">
import java.util.Scanner;
public class Tervehdys {
public static void main(String[] args) {
Scanner lukija = new Scanner(System.in);
System.out.print("Kenelle sanotaan hei: ");
String nimi = lukija.nextLine(); // Luetaan käyttäjältä rivi tekstiä ja asetetaan sen arvo muuttujaan nimi
System.out.print("Hei " + nimi);
}
}
</pre>
<p>Kun yllä oleva ohjelma ajetaan, pääset kirjoittamaan syötteen. NetBeansin tulostusvälilehti (alhaalla) näyttää ajetun ohjelman jälkeen seuraavalta (käyttäjä syöttää nimen "Matti").</p>
<pre>
run:
Kenelle sanotaan hei: Matti
Hei Matti
BUILD SUCCESSFUL (total time: 6 seconds)
</pre>
<h3 id="syote_kokonaisluku">Kokonaisluvun lukeminen</h3>
<p>Scanner-apuvälineemme ei ole hyvä kokonaislukujen lukemiseen, joten käytämme toista apuvälinettä merkkijonon kokonaisluvuksi muuttamisessa. Komento <code>Integer.parseInt</code> muuttaa sille annetussa tekstimuuttujassa olevan kokonaisluvun kokonaislukumuuttujaksi. Komennolle annetaan tekstimuuttuja sulkuihin, ja se palauttaa kokonaisluvun joka asetetaan kokonaislukumuuttujaan.</p>
<p>Käytännössä kytkemme kaksi komentoa yhteen. Ensin luemme käyttäjältä rivin, jonka annamme heti komennolle <code>Integer.parseInt</code>.</p>
<!--
<pre class="sh_java">
String kolmonenMerkkijonona = "3";
int kolme = Integer.parseInt(kolmonenMerkkijonona);