|
15 | 15 | </p> |
16 | 16 | <program language="java" interactive="activecode" line-numbers="yes"> |
17 | 17 | <code> |
18 | | - import java.util.ArrayList; // Line 1: Import necessary class |
| 18 | + import java.util.ArrayList; // Import necessary class |
19 | 19 |
|
20 | | - public class Main { // Line 3: Class declaration |
21 | | - public static void main(String[] args) { // Line 4: Main method declaration |
22 | | - |
23 | | - count = new ArrayList<Integer<(10); // Line 6: Attempt to use 'count' without declaration |
| 20 | + public class Main { // Class declaration |
| 21 | + |
| 22 | + // Main method declaration |
| 23 | + public static void main(String[] args) { |
24 | 24 |
|
25 | | - System.out.println("ArrayList created."); // Line 8: This line won't be reached due to the error. |
| 25 | + // Attempt to use 'count' without declaration |
| 26 | + count = new ArrayList<Integer<(10); |
26 | 27 |
|
27 | | - } // Line 10: End of main method |
28 | | - } // Line 11: End of class |
| 28 | + // This line won't be reached due to the error. |
| 29 | + System.out.println("ArrayList created."); |
| 30 | + |
| 31 | + } // End of main method |
| 32 | + } // End of class |
29 | 33 | </code> |
30 | 34 | </program> |
31 | 35 | <p> |
|
42 | 46 | </p> |
43 | 47 | <code> |
44 | 48 | // Histo.java |
45 | | - public class Histo { // Line 2: Class declaration |
| 49 | + public class Histo { // Class declaration |
46 | 50 |
|
47 | | - public static void main(String[] args) { // Line 4: Main method declaration |
48 | | - |
49 | | - Scanner data = null; // Line 6: This line will cause the "cannot find symbol" error |
| 51 | + public static void main(String[] args) { // Main method declaration |
| 52 | + // This line will cause the "cannot find symbol" error |
| 53 | + Scanner data = null; |
50 | 54 |
|
51 | | - } // Line 8: End of main method |
52 | | - } // Line 9: End of class |
| 55 | + } // End of main method |
| 56 | + } // End of class |
53 | 57 | </code> |
54 | 58 | </program> |
55 | 59 | <p> |
|
65 | 69 | </p> |
66 | 70 | <program language="java" interactive="activecode" line-numbers="yes"> |
67 | 71 | <code> |
68 | | - // Histo.java // Line 1: The filename for this example |
69 | | - import java.io.File; // Line 2: Import the File class, needed for file operations |
70 | | - import java.util.Scanner; // Line 3: Import the Scanner class, now correctly imported |
| 72 | + // Histo.java // The filename for this example |
| 73 | + import java.io.File; // Import the File class, needed for file operations |
| 74 | + import java.util.Scanner; // Import the Scanner class, now correctly imported |
71 | 75 |
|
72 | | - public class Histo { // Line 5: Class declaration |
| 76 | + public class Histo { // Class declaration |
73 | 77 |
|
74 | | - public static void main(String[] args) { // Line 7: Main method declaration |
| 78 | + public static void main(String[] args) { // Main method declaration |
75 | 79 |
|
76 | | - Scanner data = Scanner(new File("test.dat")); // Line 9: This Line will cause an error |
| 80 | + Scanner data = Scanner(new File("test.dat")); // This Line will cause an error |
77 | 81 |
|
78 | | - } // Line 12: End of main method |
79 | | - } // Line 13: End of class |
| 82 | + } // End of main method |
| 83 | + } // End of class |
80 | 84 | </code> |
81 | 85 | </program> |
82 | 86 | <p> |
|
99 | 103 | <program language="java" interactive="activecode" line-numbers="yes"> |
100 | 104 | <code> |
101 | 105 | // Histo.java |
102 | | - import java.util.Scanner; // Line 1 |
103 | | - // Line 2 |
104 | | - public class Histo { // Line 3 |
105 | | - // Line 4 |
106 | | - public static void main(String[] args) { // Line 5 |
107 | | - Scanner data = null // Line 7: The error will point here |
108 | | - System.out.println("This line will not compile."); // Line 8 |
109 | | - } |
110 | | - } |
| 106 | + import java.util.Scanner; // Imports Scanner |
| 107 | + |
| 108 | + public class Histo { // Class declaration |
| 109 | + |
| 110 | + public static void main(String[] args) { // Main method declaration |
| 111 | + Scanner data = null // The error will point here |
| 112 | + System.out.println("This line will not compile."); |
| 113 | + }// End of main method |
| 114 | + }//End of class |
111 | 115 | </code> |
112 | 116 | </program> |
113 | 117 | <p> |
|
124 | 128 | <program language="java" interactive="activecode" line-numbers="yes"> |
125 | 129 | <code> |
126 | 130 | // UncheckedWarningDemo.java |
127 | | - import java.util.ArrayList; // Line 2: Import the ArrayList class |
| 131 | + import java.util.ArrayList; // Import the ArrayList class |
128 | 132 |
|
129 | | - public class UncheckedWarningDemo { // Line 4: Class declaration |
| 133 | + public class UncheckedWarningDemo { // Class declaration |
130 | 134 |
|
131 | | - public static void main(String[] args) { // Line 6: Main method declaration |
| 135 | + public static void main(String[] args) { // Main method declaration |
132 | 136 |
|
133 | 137 | // This is where the potential for unchecked operations begins. |
134 | | - ArrayList rawList = new ArrayList();//Line 9 |
135 | | - |
136 | | - rawList.add("Hello");//Line 11: This is the line that will throw an error |
| 138 | + ArrayList rawList = new ArrayList(); |
137 | 139 |
|
| 140 | + rawList.add("Hello"); |
| 141 | + //This is the line that will throw an error |
138 | 142 | System.out.println("Element added: " + rawList.get(0)); |
139 | | - } // Line 14: End of main method |
140 | | - } // Line 15: End of class |
| 143 | + } // End of main method |
| 144 | + } // End of class |
141 | 145 | </code> |
142 | 146 | </program> |
143 | 147 | <p> |
|
0 commit comments