forked from gdiDFW/gdi-intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass1.html
More file actions
1084 lines (1063 loc) · 34.9 KB
/
class1.html
File metadata and controls
1084 lines (1063 loc) · 34.9 KB
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>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GDI Introduction to Java and OOP Concepts</title>
<meta name="description" content="Intro to Object Oriented Programming with Java curriculum was developed by Ashley Price for Girl Develop It. The course is meant to be taught in 2 4-hour sections. The slides are customizable according to the needs of a given class or audience.">
<meta name="author" content="Ashley Price">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="icon" href="images/favicon.ico">
<link rel="stylesheet" href="reveal/css/reveal.css">
<link rel="stylesheet" href="reveal/css/theme/serif.css" id="theme">
<link rel="stylesheet" href="css/gdi.css">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="reveal/plugin/highlight/styles/zenburn.css">
<!-- For the slides -->
<link rel="stylesheet" href="css/slides.css">
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="reveal/css/print/pdf.css" type="text/css" media="print">
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Opening slide -->
<section>
<img src="images/gdi_logo_badge.png" alt="" />
<h3>Intro to Java and OOP Concepts</h3>
<h4>Class 1</h4>
<h5 class='title-page'>Developed for GDI by Ashley Price</h5>
</section>
<!-- Welcome-->
<section>
<section>
<h3>Welcome!</h3>
<p>Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.</p>
</section>
<section>
<h3>Some "Rules"</h3>
<ul>
<li>We are here for you!</li>
<li>Every question is important</li>
<li>Help each other</li>
<li>Be Respectful</li>
<li>Have fun!</li>
</ul>
</section>
<section>
<h3>Introductions</h3>
<ul>
<li>Who are you?</li>
<li>What spurred you into learning about development?</li>
<li>What do you hope to get out of the class?</li>
</ul>
</section>
</section>
<!-- Creating a new project -->
<section>
<section>
<h3>Creating a new Java Project</h3>
<ol>
<li>
In Eclipse, navigate to File > New > Java Project
</li>
<li>
Project Name: com.gdi.oop
</li>
<li>
We'll be keeping the defaults. So, hit finish!
</li>
</ol>
</section>
<section>
<h3> Adding our First Class</h3>
<ol>
<li>File > New > Class</li>
<li>Name: HelloWorld <em>(make sure there are no spaces)</em></li>
<li>Under <em>which methods would you like to create?</em>, check "public static void main(String[] args)"</li>
<li>Click finish</li>
</ol>
</section>
<section>
<h3>Default Code</h3>
<pre><code class='java'>
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
//TODO Auto-generated method stub
}
}
</code></pre>
</section>
</section>
<section>
<section>
<aside class='notes'>
public static void main(String[] args) {<br>
// TODO Auto-generated method stub<br>
<strong>System.out.println("Hello, World!");</strong><br>
}
</aside>
<h3>Writing our first line </h3>
<ol>
<li>Inside the main method type:
<pre><code class='java'>System.out.println("Hello, World!");</code></pre></li>
<li>Hit the play button in your toolbar</li>
</ol>
</section>
<section>
<h3>The Code </h3>
<pre><code class='java'>
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
//TODO Auto-generated method stub
System.out.println("Hello, World!");
}
}
</code></pre>
</section>
</section>
<!-- Break it Down and look at the example -->
<section>
<section>
<h3>Main Method</h3>
<p>In Java, every application contains a main method that acts as an entry point for the application</p>
</section>
<section>
<h3>Comments</h3>
<p>Comments in Java let you write notes in the code.</p>
<p>Comments can be formatted in two ways: </p>
<ol>
<li>A single line that starts with //</li>
<li>One or more lines between /* and */</li>
</ol>
</section>
<section>
<pre><code class='java'>
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
//TODO Auto-generated method stub
System.out.println("Hello, World!");
}
}
</code></pre>
</section>
</section>
<section>
<h3>Part 1: Language Fundamentals</h3>
<ul>
<li>Primitive Data Types (Variables)</li>
<li>Operators</li>
<li>Expressions & Statements</li>
<li>Flow Control (Blocks)</li>
<li>Exceptions</li>
</ul>
</section>
<section>
<section>
<h3>Variables</h3>
<p>What are variables?</p>
<p>Java is a statically typed language</p>
<p>You must declare all variables before you use them. The declaration must include the variable type and the variable name.</p>
</section>
<section>
<h3>Statically Typed Variables Illustrated</h3>
<p>You can think of variables as little boxes that hold a value</p>
<p>In strongly typed langauges, variables only hold the "kind of value" they were declared to: </p>
<img src="images/variables_illustrated.png">
</section>
<section>
<aside class='notes'>
There are 8 total, but these are three we care about at the moment
</aside>
<h3>Primitive Variables</h3>
<p>A primitive variable is the most basic type of value in a language.</p>
<p>Primitive types we'll be looking at include: </p>
<ul>
<li>int</li>
<li>double</li>
<li>boolean</li>
</ul>
</section>
<section>
<aside class='notes'>
public static void main(String[] args) {<br>
// TODO Auto-generated method stub<br>
System.out.println("Hello World");<br>
<strong>
int numberPassengers;<br>
int maxNumberPassengers;<br>
boolean isFull;<br>
</strong>
}
</aside>
<h3>Code It: Creating Variables</h3>
<p>Declare a variable with the following format: </p>
<pre><code class='java'>type name;</code></pre>
<p>We're going to create the following three variables: </p>
<ul>
<li>int numberPassengers</li>
<li>int maxNumberPassengers</li>
<li>boolean isFull</li>
</ul>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World!");
int numberPassengers;
int maxNumberPassengers;
boolean isFull;
}
</code></pre>
</section>
</section>
<section>
<section>
<h3>Operators</h3>
<ul>
<li>Assignment</li>
<li>Arithmetic</li>
<li>Equality and Relationship</li>
<li>Conditional</li>
<li>Precedence</li>
</ul>
</section>
</section>
<section>
<section>
<aside class='notes'>
public static void main(String[] args) {<br>
// TODO Auto-generated method stub<br>
System.out.println("Hello World");<br>
int numberPassengers;<br>
int maxNumberPassengers;<br>
boolean isFull;<br>
<strong>numberPassengers=1;</strong><br>
}<br>
"Variable Name" "Operator" "Value"
</aside>
<h3>Assignment Operators</h3>
<p>To assign a value to a variable we use the assignment operator: </p>
<pre><code class='java'>=</code></pre>
<p>Let's try it out: </p>
<pre><code class='java'>numberPassgeners = 1;</code></pre>
</section>
<section>
<aside class='notes'>
public static void main(String[] args) {<br>
// TODO Auto-generated method stub<br>
System.out.println("Hello, World!");<br>
int numberPassengers;<br>
<strong>int maxNumberPassengers=2;</strong><br>
boolean isFull;<br>
numberPassengers=1;<br>
}
</aside>
<h3>Initialization</h3>
<p>We just <em>initialized</em> numberPassgeners</p>
<p>Initilization is the first (initial) assignment of a value to a variable</p>
<p>You should always initialize your variables before using them in your program</p>
</section>
<section>
<p>You can initialize your variables when you declare them: </p>
<pre><code class='java'>int maxNumberPassengers = 2</code></pre>
</section>
<section>
<aside class='notes'>
public static void main(String[] args) {<br>
// TODO Auto-generated method stub<br>
System.out.println("Hello World");<br>
int numberPassengers;<br>
int maxNumberPassengers=2;<br>
<strong>boolean isFull=false;</strong><br>
numberPassengers=1;<br>
}
</aside>
<h3>Code it!</h3>
<p>Boolean variables have two values:</p>
<ul>
<li>true</li>
<li>false</li>
</ul>
<p><span class='individually'>On your own,</span> initialize the variable <em>isFull</em> with the value <em>false</em></p>
</section>
<section>
<h3>The Code: </h3>
<pre><code class='java'>
public static void main(String[] args) {
//TODO Auto-generated method stub
System.out.println("Hello, World");
int numberPassengers;
int maxNumberPassgeners = 2;
boolean isFull = false;
numberPassgeners = 1;
}
</code></pre>
</section>
</section>
<section>
<section>
<aside class='notes'>
Dropping % don't need that with beginners.
</aside>
<h3>Arithmetic Operators</h3>
<p>Just like in math class, we can have arithmetic operators in programming:</p>
<pre><code class='java'>+, -, *, /</code></pre>
<p>Add, subtract, multiply and divide</p>
</section>
<section>
<aside class='notes'>
System.out.println("Hello, World!");<br>
int numberPassengers;<br>
int maxNumberPassengers=2;<br>
boolean isFull=false;<br>
numberPassengers=1;<br>
<strong>maxNumberPassengers = maxNumberPassengers + 3; //we added a backseat!</strong><br>
</aside>
<h3>Code It!</h3>
<pre><code class='java'>
maxNumberPassengers = maxNumberPassgeners + 3; //we added a backseat!
</code></pre>
</section>
<section>
<aside class='notes'>
System.out.println("Hello, World!");<br>
int numberPassengers;<br>
int maxNumberPassengers=2;<br>
boolean isFull=false;<br>
numberPassengers=1;<br>
maxNumberPassengers = maxNumberPassengers + 3; //we added a backseat!<br>
<strong>numberPassengers=numberPassengers+1;<br>
System.out.println("Number of Passengers: " + numberPassengers);</strong><br>
</aside>
<h3>Code it!</h3>
<p class='individually'>On your own: </p>
<ul>
<li>Increment the numberPassengers value by one</li>
<li>Then use the System.out.println method to show the result:
<pre><code class='java'>
System.out.println("Number of Passengers: " + numberPassengers);
</code></pre>
</li>
</ul>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
numberPassengers = numberPassengers + 1;
System.out.println("Number of Passengers: " + numberPassengers);
</code></pre>
<p>You can write this in shorthand using the ++ operator: </p>
<pre><code class='java'>
numberPassengers++;
</code></pre>
<p>Output: </p>
<pre><code class='no-highlight'>
Hello World
Number of Passengers: 2
</code></pre>
</section>
</section>
<section>
<section>
<aside class='notes'>
int currentSpeed = 0;<br>
currentSpeed = currentSpeed + 20;<br>
System.out.println("Current Speed: "+currentSpeed);<br>
currentSpeed = currentSpeed/2;<br>
System.out.println("Current Speed: "+currentSpeed);<br>
currentSpeed = currentSpeed *3;<br>
System.out.println("Current Speed: "+currentSpeed);<br>
</aside>
<h3>Code It: Playing with Arithmetic Operators</h3>
<p><span class='individually'>On your own:</span></p>
<ol>
<li>Create an integer called 'currentSpeed'</li>
<li>Initialize it with the value 0</li>
<li>Increase the value by 20 mph</li>
<li>Print the new current speed</li>
<li>Now reduce your speed in half</li>
<li>Print the new current speed</li>
<li>Increase your speed times three</li>
<li>Print the new current speed</li>
</ol>
</section>
<section>
<h3>Expected Output: </h3>
<pre><code class='no-highlight'>
Hello World
Number of Passengers: 2
Current Speed: 20
Current Speed: 10
Current Speed: 30
</code></pre>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
int currentSpeed = 0;
currentSpeed = currentSpeed + 20;
System.out.println("Current Speed: "+currentSpeed);
currentSpeed = currentSpeed / 2;
System.out.println("Current Speed: "+currentSpeed);
currentSpeed = currentSpeed * 3;
System.out.println("Current Speed: "+currentSpeed);
</code></pre>
</section>
</section>
<section>
<section>
<h3>Equality and Relationship Operators</h3>
<p>These operatorse evaluate to true or false</p>
<table>
<tr>
<td>==</td><td>Equal</td>
</tr>
<tr>
<td>!=</td><td>Not Equal</td>
</tr>
<tr>
<td><</td><td>Less than</td>
</tr>
<tr>
<td>></td><td>Greater than</td>
</tr>
<tr>
<td><=</td><td>Less than or Equal to</td>
</tr>
<tr>
<td>>=</td><td>Greater than or Equal to</td>
</tr>
</table>
</section>
<section>
<h3>Code it!</h3>
<pre><code class='java'>
isFull = (numberPassengers == maxNumberPassengers);
System.out.println("The car is Full: " + isFull);
</code></pre>
<p>Output:</p>
<pre><code class='no-highlight'>
The car is full: false
</code></pre>
</section>
<section>
<aside class='notes'>
Example:<br>
isFull = numberPassengers < maxNumberPassengers;<br>
System.out.println("We have less than the number of Max Passengers: "+isFull);<br>
boolean noMore = numberPassengers >= maxNumberPassengers;<br>
System.out.println("I'm full and/or overbooked! "+noMore);<br>
We have less than the number of Max Passengers: true<br>
I'm full and/or overbooked! false<br>
</aside>
<h3>Code It - <span class='individually'>Individually</span></h3>
<ol>
<li>In a boolean "hasSpace," assign the resulting value of the question "numberPassengers is less than maxNumberPassengers?"</li>
<li>Print the result</li>
<li>Test if the numberPassengers is greater than or equal to maxNumberPassengers and store it in isFull</li>
<li>Print the result</li>
</ol>
</section>
<section>
<h3>Example Answer</h3>
<pre><code class='java'>
isFull = numberPassengers < maxNumberPassengers;
System.out.println("We have less than the number of Max Passengers: "
+ isFull);
boolean noMore = numberPassengers > maxNumberPassengers;
System.out.println("I'm full and/or overbooked! " + noMore);
</code></pre>
<p>Output:</p>
<pre><code class='no-highlight'>
We have less thatn the number of Max Passengers: true
I'm full and/or overbooked! false
</code></pre>
</section>
</section>
<section>
<section>
<aside class='notes'>
So evaluate x>0 <br>
now evaluate y> 0<br>
now evaluate if both of those things were true
</aside>
<h3>Conditional Operators</h3>
<table>
<tr>
<td>&&</td><td>And</td>
</tr>
<tr>
<td>||</td><td>Or</td>
</tr>
</table>
<p>We can check if two (x and y) values are true</p>
<pre><code class='java'>
positiveNumbers = x>0 && y>0;
</code></pre>
</section>
<section>
<aside class='notes'>
I'm only endangering myself false<br>
Mention Short Circuiting? If class is doing well with concepts.
</aside>
<h3>Code It!</h3>
<pre><code class='java'>
boolean dangerToSelfOnly = numberPassengers == 1 && currentSpeed > 55;
System.out.println("I'm only endangering myself " + dangerToSelfOnly);
</code></pre>
</section>
</section>
<section>
<section>
<h3>Precedence</h3>
<p>Just like in math class where you multiply and divide before you add and subtract all our operators have an order of precedence. Check out the
<a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html" target="_blank">Official Documentation</a></p>
</section>
<section>
<h3>Precedence</h3>
<p>Our previous example: </p>
<pre><code class='java'>positiveNumbers = x>0 && y>0;</code></pre>
<p>According to the precedence chart evaluates:</p>
<table>
<tr>
<td>x>0, y>0</td><td>relational</td>
</tr>
<tr>
<td>&&</td><td>logical and</td>
</tr>
<tr>
<td>=</td><td>assignment</td>
</tr>
</table>
</section>
</section>
<section>
<h3>Expressions</h3>
<p>An expression is a piece of code that evaluates to a value.</p>
<p>Example:</p>
<pre><code class='java'>
numberPassengers < maxNumberPassengers
</code></pre>
<p>This expression could be true or false depending on the values of the varaibles, but it is not a complete statement of execution</p>
</section>
<section>
<aside class='notes'>
There is no semi-colon and the expression on the previous page returns a value but nothing happens.
</aside>
<h3>Statement</h3>
<p>A statement is a complete unit of execution. It's a sentence in the Java Language. An instruction that can be carried out from start to end.</p>
<p>Let's take a look at one of the statements we've written:</p>
<pre><code class='java'>
boolean dangerToSelfOnly = numberPassengers == 1 && currentSpeed > 55;
</code></pre>
</section>
<section>
<h3>Checkpoint</h3>
<p>Reviewing Variabels & Operators</p>
</section>
<section>
<h3>Flow Control</h3>
<p>In our example program, each statement is executed one after another.</p>
<p>What if we wanted to make a choice about whether or not a statement was executed?</p>
<p>If the number of passengers is less than the max allowed, offer a ride home else apologize?</p>
</section>
<section>
<section>
<h3>If/Else</h3>
<p>The If/Else statement allows us to make a choice</p>
<pre><code class='java'>
if (x < 5) {
System.out.println("Yay!");
} else {
System.out.println("Boo!");
}
</code></pre>
</section>
<section>
<aside class='notes'>
if(numberPassengers < maxNumberPassengers) {<br>
System.out.println("Would you like a lift?");<br>
}<br>
else {<br>
System.out.println("Sorry, we're too full");<br>
}<br>
<br>
Expected output: <br>
Would you like a lift?
</aside>
<h3>Code it!</h3>
<p>If the number of passengers is less than the max number of passengers allowed, offer a ride</p>
<p>Otherwise, apologize</p>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
if (numberPassengers < maxNumberPassengers) {
System.out.println("Would you like a lift?");
} else {
System.out.println("Sorry, we're too full!");
}
</code></pre>
<p>Output: </p>
<pre><code class='no-highlight'>
Would you like a lift?
</code></pre>
</section>
</section>
<section>
<section>
<aside class='notes'>
Highlight the order<br>
if goes at the top<br>
else if goes in the middle (as many as you want)<br>
Else goes at the bottom
</aside>
<h3>else if</h3>
<p>You also have an else if statement at your disposal:</p>
<pre><code class='java'>
if (x < 5) {
System.out.println("too small");
} else if ( x == 5) {
System.out.println("Just right");
} else {
System.out.println("too big!");
}
</code></pre>
</section>
<section>
<aside class='notes'>
if(numberPassengers < maxNumberPassengers) {<br>
System.out.println("Would you like a lift?");<br>
}<br>
else if(numberPassengers == maxNumberPassengers-1){<br>
System.out.println("Room for one more!");<br>
}<br>
else {<br>
System.out.println("Sorry, we're too full");<br>
}
</aside>
<h3>Code it - <span class='individually'>Individually</span></h3>
<p>Add an else if statement to your current if/else block that prints out "We have room for one more" if the number of passengers is equal to the number of Max passengers minus 1.</p>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
if (numberPassengers < maxNumberPassengers) {
System.out.println("Would you like a lift?");
} else if (numberPassengers == maxNumberPassengers-1) {
System.out.println("We have room for one more!");
} else {
System.out.println("Sorry, we're too full!");
}
</code></pre>
</section>
</section>
<section>
<section>
<h3>Switch Statement</h3>
<p>A switch statement evaluates the value of an integer or String and executes all the statements following the matching "case" block.</p>
<p>In other words, it evaluates the conditional once and starts execution from there until it is told to stop or hits the end of the block</p>
</section>
<section>
<h3>Switch Statement Example</h3>
<pre><code class='java'>
switch (holiday) {
case "new years":
System.out.println("Happy New Year!");
break;
case "fourth of july":
System.out.println("Happy Independence Day");
break;
default:
System.out.println("Happy Holidays!");
}
</code></pre>
</section>
<section>
<aside class='notes'>
switch(numberPassengers){<br>
case 1: System.out.println("I love driving by myself");<br>
case 2: System.out.println("They always call shotgun");<br>
case 3: System.out.println("third wheel");<br>
case 4: System.out.println("Full house!");<br>
default: System.out.println("over the top");<br>
}<br>
<br>
<strong>Expected output:</strong><br>
they always call shotgun<br>
third wheel<br>
Fullhouse!<br>
Over the Top
</aside>
<h3>Code It!</h3>
<p>Let's create a switch statement based on the numberPassengers variable</p>
<p>Print the following sayings for the corresponding value: </p>
<ol>
<li>I love driving by myself</li>
<li>They always call shotgun</li>
<li>Third Wheel</li>
<li>Full House </li>
</ol>
<p>Default :Over the top!</p>
</section>
<section>
<aside class='notes'>
In our case numberPassengers is 2<br>
so everthing (including) after "They always call shotgun" prints
</aside>
<h3>Wait a Minute! Take a break</h3>
<p>Without any break statements the switch blocks "falls through" and executes everything after the matching value too!</p>
</section>
<section>
<h3>Code it!</h3>
<p><span class='individually'>On your own,</span> add a break statement to all of the cases</p>
<p>Example:</p>
<pre><code class='java'>
case 1: System.out.println("I love driving by myself");
break;
</code></pre>
</section>
<section>
<aside class='notes'>
switch(numberPassengers){<br>
case 1: System.out.println("I love driving by myself");<br>
break;<br>
case 2: System.out.println("They always call shotgun");<br>
break;<br>
case 3: System.out.println("third wheel");<br>
break;<br>
case 4: System.out.println("Full house!");<br>
break;<br>
default: System.out.println("over the top");<br>
}<br>
<br>
The default block is at the bottom of the switch block<br>
it cannot fall through
</aside>
<h3>The Code</h3>
<pre><code class='java'>
switch (numberPassengers) {
case 1:
System.out.println("I love driving by myself");
break;
case 2:
System.out.println("They always call shotgun");
break;
case 3:
System.out.println("Third Wheel");
break;
case 4:
System.out.println("Full House");
break;
default:
System.out.println("Over the top!");
}
</code></pre>
<p>Why isn't there a break statement on the default block?</p>
</section>
</section>
<section>
<h3>Loops</h3>
<p>With loops, we can write code once and have it executed multiple times in a row.</p>
</section>
<section>
<section>
<h3>For Loop</h3>
<p>In cases where we know how many times we wish to execute the loop, we can use a <em>for loop</em></p>
<p>The for loop structure calls for a variable initialized to a value, the termination condition, and the increment</p>
<pre><code class='java'>
for (initial variable; test condition; increment) { }
</code></pre>
</section>
<section>
<aside class='notes'>
for(int mySpeed=0; mySpeed<55; mySpeed+=10){<br>
System.out.println("I'm going "+mySpeed+" mph!");<br>
}
</aside>
<h3>Code it!</h3>
<pre><code class='java'>
for (int mySpeed = 0; mySpeed < 55; mySpeed +=10) {
System.out.println("I'm going " + mySpeed + "mph!");
}
</code></pre>
</section>
<section>
<h3>Output</h3>
<pre><code class='no-highlight'>
I'm going 0 mph!
I'm going 10 mph!
I'm going 20 mph!
I'm going 30 mph!
I'm going 40 mph!
I'm going 50 mph!
</code></pre>
</section>
</section>
<section>
<section>
<h3>While Loop</h3>
<p>At other times, we want to execute oru code for an unknown number of iterations</p>
<p>It simply needs to be executed until some condition is true regardless of how many iterations it takes.</p>
<pre><code class='java'>
while (condition) {
...
}
</code></pre>
</section>
<section>
<aside class='notes'>
isFull = numberPassengers == maxNumberPassengers;<br>
while(hasSpace){<br>
numberPassengers++;<br>
System.out.println("I'm Carrying: "+numberPassengers);<br>
hasSpace = numberPassengers < maxNumberPassengers;<br>
}<br>
<br>
<strong>Output:</strong> <br>
I'm Carrying: 3<br>
I'm Carrying: 4<br>
I'm Carrying: 5
</aside>
<h3>Code It!</h3>
<p>While hasSpace is true, add a passengers then update the value of hasSpace</p>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
isFull = numberPassengers == maxNumberPassengers;
while (!isFull) {
/* We'll add a passenger */
numberPassengers ++;
/* We'll notifying people how many passengers we have */
System.out.println("I'm carrying: " + numberPassengers);
/* We'll update whether or not we're full */
isFull = numberPassengers == maxNumberPassengers;
}
</code></pre>
</section>
</section>
<section>
<h3>Checkpoint!</h3>
<p>Flow Control Constructs</p>
</section>
<section>
<section>
<h3>Part 2: Introduction to Objects</h3>
<p>All real-world obejcts have a <em>state</em> and a <em>behavior</em></p>
<p>We'll use this approach to looking at objects to describe objects in our programming</p>
</section>
<section>
<h3>Describing Objects in this room</h3>
<p>Let's take a minute to identify objects in this room and talk about them by identifying their state and behavior</p>
</section>
</section>
<section>
<section>
<h3>Array Objects</h3>
<ul>
<li>Arrays are containers for values.</li>
<li>Arrays hold a single type of value.</li>
<li>Arrays are a fixed length.</li>
</ul>
</section>
<section>
<h3>Real World Array</h3>
<ul>
<li>7-day pill box - every slot holds a pill</li>
</ul>
</section>
<section>
<h3>Syntax - Creating an Array</h3>
<pre><code class='java'>
String[] garage = new String[3];
garage[0] = "Chevy";
garage[1] = "Ford";
garage[2] = "Pontiac";
</code></pre>
</section>
<section>
<aside class='notes'>
It's size/length is 3<br>
It's currently holding Chevy, Ford and Pontiac
</aside>
<h3>Array - States</h3>
<p>The state of an array can be described by: </p>
<ul>
<li>How big it is or how many values it can hold (also called its length)</li>
<li>The values it currently holds</li>
</ul>
<p>**Describe our Array in these terms</p>
</section>
<section>
<aside class='notes'>
Highlight Making use of .length state/property
</aside>
<h3>Arrays and For loops are BFFs</h3>
<pre><code class='java'>
for (int i=0; i<garage.length; i++) {
System.out.println("Garage Slot " + i + "holds " + garage[i]);
}
</code></pre>
</section>
</section>
<section>
<h3>String Object</h3>
<p>We created an Array of Strings</p>
<p><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html">Strings</a> are a type of Object</p>
<p>We can create a single String object by declaring a variable of type String and assigning it a new String object with some value</p>
<pre><code class='java'>
String dreamCar = new String("Corvette");
</code></pre>
</section>
<section>
<section>
<h3>Methods</h3>
<p>An object's behavior is defined in its methods.</p>
<p>A method is a block of code that will execute when called upon.</p>
</section>
<section>
<h3>String Methods (behaviors)</h3>
<p>Some common things String can do: </p>
<ul>
<li>A String can tell you its length</li>
<li>A String can tell you whether or not it contains a certain value</li>
<li>It can convert itself to lowercase or uppercase</li>
</ul>
</section>
<section>
<h3>An example in Code</h3>
<pre><code class='java'>
String dreamCar = new String("Corvette");
System.out.println("Length: " + dreamCar.length());
System.out.println("Does the word contain 'vette'?" +
dreamCar.contains("vette"));
System.out.println("It's all lower " + dreamCar.toLowerCase());
</code></pre>
</section>
<section>
<aside class='notes'>
System.out.println("It's all upper " + dreamCar.toUpperCase());
</aside>
<h3 class='individually'>On your own</h3>
<p>Call and print the result of the "toUpperCase()" method</p>
</section>
<section>
<h3>The Code</h3>
<pre><code class='java'>
System.out.println("It's all upper " + dreamCar.toUpperCase());
</code></pre>
</section>
</section>
<section>
<section>
<h3>Methods Continued</h3>
<p>Methods have a return type and a parameters list</p>
</section>
<section>
<h3>Methods Return Type</h3>
<p>Return Type - If the method returns a value, what type is it?</p>
<p>Let's consider the <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#length()" target="_blank">length() method</a> of a String</p>
</section>
<section>
<h3>Methods Parameters</h3>
<p>Parameters- A list of values the methods expects as input when you call on it</p>
<pre><code class='java'>
.contains("vette");
</code></pre>