File tree Expand file tree Collapse file tree 3 files changed +59
-0
lines changed
src/com/codefortomorrow/intermediate/chapter11/examples Expand file tree Collapse file tree 3 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .codefortomorrow .intermediate .chapter11 .examples ;
2
+
3
+ public class MethodCalls {
4
+ public static void main (String [] args ) {
5
+ sum (5 , 4 );
6
+ }
7
+
8
+ public static void sum (int a , int b ) {
9
+ System .out .println (a + b );
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ package com .codefortomorrow .intermediate .chapter11 .examples ;
2
+
3
+ public class Parameters {
4
+ public static void main (String [] args ) {
5
+ // scoopIceCream with 1 parameter
6
+ scoopIceCream ("chocolate" );
7
+ scoopIceCream ("vanilla" );
8
+ scoopIceCream ("strawberry" );
9
+
10
+ // scoopIceCream with multiple parameters
11
+ scoopIceCream ("chocolate" , true );
12
+ scoopIceCream ("vanilla" , false );
13
+ scoopIceCream ("strawberry" , true );
14
+ }
15
+
16
+ public static void scoopIceCream (String flavor ) {
17
+ System .out .println ("Here's a scoop of " + flavor + " ice cream!" );
18
+ }
19
+
20
+ public static void scoopIceCream (String flavor , boolean wantCone ) {
21
+ if (wantCone ) {
22
+ System .out .println ("Here's a scoop of " + flavor +
23
+ " ice cream on a cone!" );
24
+ } else {
25
+ System .out .println ("Here's a scoop of " + flavor + " ice cream!" );
26
+ }
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ package com .codefortomorrow .intermediate .chapter11 .examples ;
2
+
3
+ public class VariableScope {
4
+ public static void main (String [] args ) {
5
+ method1 (); // prints 5.0
6
+ method2 (); // prints 2.0
7
+ // System.out.println(a); throws an exception
8
+ }
9
+
10
+ public static void method1 () {
11
+ double a = 5 ;
12
+ System .out .println (a );
13
+ }
14
+
15
+ public static void method2 () {
16
+ // both variables are called a
17
+ double a = 2 ;
18
+ System .out .println (a );
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments