Skip to content

Commit c930c96

Browse files
authored
Merge pull request #7 from code-for-tomorrow/ch11-rebecca
Add Ch. 11 example code
2 parents 92d4dee + 4bbbc3e commit c930c96

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.codefortomorrow.intermediate.chapter11.examples;
2+
3+
public class JavaDocs {
4+
/**
5+
* Returns the average of two doubles
6+
* @param a first double
7+
* @param b second double
8+
* @return the average of two doubles
9+
*/
10+
public static double average(double a, double b) {
11+
return (a + b) / 2;
12+
}
13+
}
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+
}

src/com/codefortomorrow/intermediate/chapter11/examples/Methods.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
public class Methods {
44
public static void main(String[] args) {
5+
// prints "Hello World" 5 times
56
for (int i = 0; i < 5; i++) {
67
printMessage();
78
}
89
}
910

11+
/** Prints "Hello World" */
1012
public static void printMessage() {
1113
System.out.println("Hello World");
1214
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codefortomorrow.intermediate.chapter11.examples;
2+
3+
public class Overloading {
4+
public static void main(String[] args) {
5+
// call 2-arg sum
6+
System.out.println(sum(2, 3)); // prints 5
7+
8+
// call 3-arg sum
9+
System.out.println(sum(1, 4, 7)); // prints 12
10+
}
11+
12+
public static int sum(int a, int b) {
13+
return a + b;
14+
}
15+
16+
public static int sum(int a, int b, int c) {
17+
return sum(a, b) + c;
18+
}
19+
}
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codefortomorrow.intermediate.chapter11.examples;
2+
3+
public class ReturnTypes {
4+
public static void main(String[] args) {
5+
printSum(5, 4);
6+
int myNumber = sum(5, 4) / 2;
7+
System.out.println(myNumber);
8+
}
9+
10+
public static void printSum(int a, int b) {
11+
System.out.println(a + b);
12+
}
13+
14+
public static int sum(int a, int b) {
15+
return a + b;
16+
}
17+
}
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.codefortomorrow.intermediate.chapter11.examples;
2+
3+
import java.util.Scanner;
4+
5+
public class WhyMethods {
6+
public static void main(String[] args) {
7+
// cleaner code because methods group actions
8+
printMessages();
9+
printSum();
10+
}
11+
12+
/** Prints 3 messages */
13+
public static void printMessages() {
14+
System.out.println("May the Force be with you");
15+
System.out.println("Tahiti, it's a magical place");
16+
System.out.println("Live long and prosper");
17+
}
18+
19+
/** Prints the sum of 2 numbers */
20+
public static void printSum() {
21+
Scanner input = new Scanner(System.in);
22+
23+
// prompt user to enter 2 numbers
24+
System.out.print("Enter number 1: ");
25+
double num1 = input.nextDouble();
26+
System.out.print("Enter number 2: ");
27+
double num2 = input.nextDouble();
28+
29+
double sum = num1 + num2;
30+
31+
// print the sum of those numbers
32+
System.out.println("Sum: " + sum);
33+
34+
input.close();
35+
}
36+
}

0 commit comments

Comments
 (0)