Skip to content

Commit 92aba1d

Browse files
authored
Create tutorial_25.java
1 parent 7770904 commit 92aba1d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

code/tutorial_25.java

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy