Skip to content

Commit 446f42d

Browse files
committed
Add example code for Method Calls, Variable Scope, and Parameters
1 parent cffe737 commit 446f42d

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

0 commit comments

Comments
 (0)