KaiquanMah commited on
Commit
b3aac10
·
verified ·
1 Parent(s): a760458

Create 2A. Other procedure-typed methods

Browse files
Week 2: Methods, strings and lists/2A. Other procedure-typed methods ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ In addition to the method main, other methods can of course be defined in the program.
2
+ The methods are written inside the class (but, of course, OUTSIDE the MAIN METHOD).
3
+ In Java, the order of methods within a class is not syntactically significant: other methods can be written either before or after the main method (or both-and).
4
+ In the examples in the course, the main method is usually written first in the class.
5
+
6
+
7
+ As a first example, consider a method that prints numbers from one to ten:
8
+ public static void onetoTen() {
9
+ for (int i=1; i<=10; i++) {
10
+ System.out.println(i);
11
+ }
12
+ }
13
+
14
+
15
+
16
+
17
+
18
+ ANOTHER METHOD can be CALLED from the main method by its name.
19
+ The mechanism is exactly the same as in Python.
20
+ In Java, of course, you have to remember the semicolon after the call statement.
21
+
22
+ public static void main(String[] args) {
23
+ onetoTen();
24
+ }
25
+ Program outputs:
26
+
27
+ 1
28
+ 2
29
+ 3
30
+ 4
31
+ 5
32
+ 6
33
+ 7
34
+ 8
35
+ 9
36
+ 10