File tree Expand file tree Collapse file tree 8 files changed +146
-0
lines changed
src/com/codefortomorrow/intermediate/chapter11/examples Expand file tree Collapse file tree 8 files changed +146
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
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 2
2
3
3
public class Methods {
4
4
public static void main (String [] args ) {
5
+ // prints "Hello World" 5 times
5
6
for (int i = 0 ; i < 5 ; i ++) {
6
7
printMessage ();
7
8
}
8
9
}
9
10
11
+ /** Prints "Hello World" */
10
12
public static void printMessage () {
11
13
System .out .println ("Hello World" );
12
14
}
Original file line number Diff line number Diff line change
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
+ }
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 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
+ }
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments