Spaces:
Running
Running
Create Week 2: Methods, strings and lists/1. Write the main method
Browse files
Week 2: Methods, strings and lists/1. Write the main method
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In Java, all subprograms are called methods.
|
| 2 |
+
In programming, a method is generally a subprogram bound to a class.
|
| 3 |
+
In Java, all subprograms are methods because all program code is always written inside a class.
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
methods of procedure type, that is, methods that have NO RETURN VALUE.
|
| 8 |
+
In this case, we are actually talking about an empty return value - the type of return value is VOID.
|
| 9 |
+
|
| 10 |
+
public static void methodName(<list of parameters>) {
|
| 11 |
+
<method structure>
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
public class Example {
|
| 16 |
+
public static void main(String[] args) {
|
| 17 |
+
System.out.println("Hello everyone!");
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
The main method has a special meaning in Java programs:
|
| 24 |
+
if a class has a MAIN METHOD, it is AUTOMATICALLY EXECUTED 1ST when the class is executed.
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
=================================
|
| 30 |
+
|
| 31 |
+
1. Write the main method
|
| 32 |
+
|
| 33 |
+
Write the main method in the program. The method should print a table of six multiples exactly as shown in the example output below.
|
| 34 |
+
|
| 35 |
+
Output:
|
| 36 |
+
Table of six multiples
|
| 37 |
+
1 x 6 = 6
|
| 38 |
+
2 x 6 = 12
|
| 39 |
+
3 x 6 = 18
|
| 40 |
+
4 x 6 = 24
|
| 41 |
+
5 x 6 = 30
|
| 42 |
+
6 x 6 = 36
|
| 43 |
+
7 x 6 = 42
|
| 44 |
+
8 x 6 = 48
|
| 45 |
+
9 x 6 = 54
|
| 46 |
+
10 x 6 = 60
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
import java.util.Random;
|
| 52 |
+
|
| 53 |
+
public class Test{
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
public static void main(String[] args){
|
| 57 |
+
System.out.println("Table of six multiples");
|
| 58 |
+
for (int i = 1; i <= 10; i++) {
|
| 59 |
+
System.out.println(i + " x 6 = " + (i * 6));
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
Table of six multiples
|
| 70 |
+
1 x 6 = 6
|
| 71 |
+
2 x 6 = 12
|
| 72 |
+
3 x 6 = 18
|
| 73 |
+
4 x 6 = 24
|
| 74 |
+
5 x 6 = 30
|
| 75 |
+
6 x 6 = 36
|
| 76 |
+
7 x 6 = 42
|
| 77 |
+
8 x 6 = 48
|
| 78 |
+
9 x 6 = 54
|
| 79 |
+
10 x 6 = 60
|
| 80 |
+
|