File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Driver {
2
+ static int classVarResult ;
3
+
4
+ public static void main (String [] args ) {
5
+ // Using a class varaible. All methods can change this.
6
+ add1 (5 , 3 );
7
+ System .out .println (classVarResult );
8
+
9
+ // Returing a varaible from a method directly
10
+ int methodVarResult = add2 (23 , 4 );
11
+ System .out .println (methodVarResult );
12
+
13
+ // Doing so means I can use it inline with other code.
14
+ System .out .println ( "5 + 2 + 8 = " + (add2 (5 , 2 ) + 8 ) );
15
+
16
+ // Returning different types
17
+ System .out .println ( divide (22 , 4 ) );
18
+
19
+ // Making a method that tests a condition
20
+ if (isEven (5461 * 6 )) {
21
+ System .out .println ("result is even" );
22
+ } else {
23
+ System .out .println ("result is odd" );
24
+ }
25
+ }
26
+
27
+ public static void add1 (int n1 , int n2 ) {
28
+ classVarResult = n1 + n2 ;
29
+ }
30
+
31
+ public static int add2 (int n1 , int n2 ) {
32
+ int result = n1 + n2 ;
33
+ return result ;
34
+ }
35
+
36
+ public static double divide (double n1 , double n2 ) {
37
+ return n1 / n2 ;
38
+ }
39
+
40
+ public static boolean isEven (int num ) {
41
+ if (num % 2 == 0 ) {
42
+ // if this happens it will exit out of the method. return false will not happen
43
+ return true ;
44
+ }
45
+ return false ;
46
+ }
47
+ }
You can’t perform that action at this time.
0 commit comments