Repository navigation
Expand file tree
/
Copy pathcomplete_notes_exercises.py
More file actions
3839 lines (3133 loc) · 161 KB
/
Copy pathcomplete_notes_exercises.py
File metadata and controls
3839 lines (3133 loc) · 161 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
from pathlib import Path
WORKSPACE = Path(r"c:/Users/piyush kumar/OneDrive/Desktop/JAVA/Learn-Java-From-Zero")
NOTE_TOPIC_MAP = {
'RandomNumbers': {
'imports': 'import java.util.Random;\n',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of RandomNumbers
System.out.println("Learning: RandomNumbers");
System.out.println("Topic #8 in Module 2");
System.out.println("Random numbers are values your program chooses automatically.");
System.out.println("They are useful for games, selection, and decisions that need surprise.");
Random random = new Random(); // Create a Random object to generate random values
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use RandomNumbers");
int anyInt = random.nextInt(); // any int value
System.out.println("Random int (any value): " + anyInt);
int zeroToNine = random.nextInt(10); // 0 to 9
System.out.println("Random int 0-9: " + zeroToNine);
int oneToSix = random.nextInt(6) + 1; // 1 to 6
System.out.println("Random int 1-6 (dice roll): " + oneToSix);
int tenToThirty = random.nextInt(21) + 10; // 10 to 30
System.out.println("Random int 10-30: " + tenToThirty);
double randomDouble = random.nextDouble(); // 0.0 to less than 1.0
System.out.println("Random double 0.0-1.0: " + randomDouble);
boolean randomBoolean = random.nextBoolean(); // true or false
System.out.println("Random boolean true/false: " + randomBoolean);
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("RandomNumbers is used when building actual applications");
System.out.println("Examples include dice rolls, random selection, and simple game logic.");
System.out.println("Use nextInt(max) for 0-based ranges, then add an offset to shift the range.");
System.out.println("Example: random.nextInt(6) + 1 gives 1-6 for a die roll.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Use java.util.Random to generate random values");
System.out.println("2. nextInt() returns any int value");
System.out.println("3. nextInt(n) returns 0 to n-1");
System.out.println("4. Add a minimum value to shift the range");
System.out.println("5. nextDouble() returns a decimal from 0.0 up to 1.0");
System.out.println("6. nextBoolean() returns true or false");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_RandomNumbers.java!");
}
''',
},
'MathClass': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of MathClass
System.out.println("Learning: MathClass");
System.out.println("Topic #9 in Module 2");
System.out.println("Math class gives built-in methods for common math tasks.");
System.out.println("You use it to calculate, compare, round, and transform numbers.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use MathClass");
int a = -12;
int absValue = Math.abs(a);
System.out.println("Absolute value of " + a + " is " + absValue);
double x = 3.7;
System.out.println("Round " + x + " = " + Math.round(x));
System.out.println("Ceil " + x + " = " + Math.ceil(x));
System.out.println("Floor " + x + " = " + Math.floor(x));
int b = 5;
int c = 9;
System.out.println("Max of " + b + " and " + c + " = " + Math.max(b, c));
System.out.println("Min of " + b + " and " + c + " = " + Math.min(b, c));
double power = Math.pow(2, 5);
double root = Math.sqrt(36);
System.out.println("2 to the power of 5 = " + power);
System.out.println("Square root of 36 = " + root);
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("MathClass is used when building actual applications");
System.out.println("Use it for geometry, scores, physics, and any numeric logic.");
System.out.println("Math methods help keep calculations simple and readable.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Math class has useful static methods");
System.out.println("2. Use Math.abs, Math.max, Math.min, Math.round, Math.sqrt");
System.out.println("3. Use Math.pow for exponents");
System.out.println("4. Math.ceil and Math.floor help with decimals");
System.out.println("5. This is useful for many real-world calculations");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_MathClass.java!");
}
''',
},
'Printf': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of Printf
System.out.println("Learning: Printf");
System.out.println("Topic #10 in Module 2");
System.out.println("Printf lets you format output with placeholders.");
System.out.println("It is helpful when you want clean and aligned output.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use Printf");
int score = 95;
double gpa = 3.85;
String name = "Alex";
System.out.printf("Name: %s%n", name);
System.out.printf("Score: %d out of 100%n", score);
System.out.printf("GPA: %.2f\n", gpa);
System.out.printf("Result: %s, GPA: %.1f%%\n", "Passed", gpa * 25 / 4);
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("Printf is used when building actual applications");
System.out.println("It makes reports, tables, and logs easier to read.");
System.out.println("Use %%d for integers, %%f for decimals, and %%s for strings.");
System.out.println("Use %%.2f to round numbers to two decimal places.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. printf formats output with placeholders");
System.out.println("2. Use %d for integers, %f for floats, %s for strings");
System.out.println("3. Use %n or \n for a new line");
System.out.println("4. Precision like %.2f controls decimal places");
System.out.println("5. Printf helps create clean real-world output");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_Printf.java!");
}
''',
},
'NestedIf': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of NestedIf
System.out.println("Learning: NestedIf");
System.out.println("Topic #12 in Module 2");
System.out.println("Nested if statements let one decision happen inside another.");
System.out.println("You use them when a choice depends on a previous choice.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use NestedIf");
int age = 18;
boolean hasTicket = true;
if (age >= 18) {
if (hasTicket) {
System.out.println("You can enter the movie.");
} else {
System.out.println("You need a ticket to enter.");
}
} else {
System.out.println("You are too young to enter.");
}
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("NestedIf is used when building actual applications");
System.out.println("It helps check multiple conditions in order.");
System.out.println("For example, verify login, then verify permissions.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Nested ifs let one condition exist inside another");
System.out.println("2. The inner if only runs when the outer if is true");
System.out.println("3. Use else inside or after nested ifs for fallback logic");
System.out.println("4. Nested ifs are useful for multi-step decisions");
System.out.println("5. Keep the logic clear so it is easy to read");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_NestedIf.java!");
}
''',
},
'StringMethods': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of StringMethods
System.out.println("Learning: StringMethods");
System.out.println("Topic #13 in Module 3");
System.out.println("String methods let you work with text in Java.");
System.out.println("You use them to change, inspect, and compare strings.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use StringMethods");
String message = "Hello Java Learners";
System.out.println("Text: " + message);
System.out.println("Length: " + message.length());
System.out.println("Uppercase: " + message.toUpperCase());
System.out.println("Lowercase: " + message.toLowerCase());
System.out.println("Contains 'Java': " + message.contains("Java"));
System.out.println("Replace spaces: " + message.replace(" ", "_"));
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("StringMethods is used when building actual applications");
System.out.println("Text processing, user input, and messages all depend on string methods.");
System.out.println("Use startsWith and endsWith to validate input formats.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. String methods help inspect and modify text");
System.out.println("2. Use length(), toUpperCase(), toLowerCase(), contains()");
System.out.println("3. Use replace() to change parts of text");
System.out.println("4. Strings are immutable, methods return new strings");
System.out.println("5. String methods are essential for Java programs");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_StringMethods.java!");
}
''',
},
'Substrings': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of Substrings
System.out.println("Learning: Substrings");
System.out.println("Topic #14 in Module 3");
System.out.println("Substrings let you extract part of a string.");
System.out.println("You use them to get pieces of text by position.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use Substrings");
String word = "JavaProgramming";
System.out.println("Text: " + word);
System.out.println("First 4 letters: " + word.substring(0, 4));
System.out.println("Last 11 letters: " + word.substring(4));
System.out.println("Middle part: " + word.substring(4, 15));
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("Substrings is used when building actual applications");
System.out.println("Extract usernames, file types, or fixed-format text.");
System.out.println("Remember substring(start, end) includes start but excludes end.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Use substring(start, end) to get part of text");
System.out.println("2. The start index is inclusive, the end index is exclusive");
System.out.println("3. Use substring(start) to get text to the end");
System.out.println("4. Substrings work with String positions");
System.out.println("5. This is useful for parsing and text extraction");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_Substrings.java!");
}
''',
},
'TernaryOperator': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of TernaryOperator
System.out.println("Learning: TernaryOperator");
System.out.println("Topic #16 in Module 3");
System.out.println("Ternary operator is a shorter if-else for simple choices.");
System.out.println("It makes code compact when you select one of two values.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use TernaryOperator");
int score = 82;
String grade = score >= 75 ? "Pass" : "Fail";
System.out.println("Score: " + score + " => " + grade);
int number = 7;
String parity = number % 2 == 0 ? "even" : "odd";
System.out.println(number + " is " + parity);
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("TernaryOperator is used when building actual applications");
System.out.println("Use it for simple decisions when you want one of two values.");
System.out.println("It is common in assignments and output formatting.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Ternary operator is written condition ? value1 : value2");
System.out.println("2. It is shorter than if-else when choosing between two results");
System.out.println("3. Use it for simple decisions only");
System.out.println("4. The result can be assigned to a variable");
System.out.println("5. It keeps small conditional logic compact");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_TernaryOperator.java!");
}
''',
},
'EnhancedSwitches': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of EnhancedSwitches
System.out.println("Learning: EnhancedSwitches");
System.out.println("Topic #18 in Module 3");
System.out.println("Enhanced switch gives a cleaner way to compare values.");
System.out.println("You can use arrows and multiple labels in one case.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use EnhancedSwitches");
String day = "Saturday";
String type = switch (day) {
case "Saturday", "Sunday" -> "Weekend";
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> "Weekday";
default -> "Unknown";
};
System.out.println(day + " is a " + type);
int month = 12;
String season = switch (month) {
case 12, 1, 2 -> "Winter";
case 3, 4, 5 -> "Spring";
case 6, 7, 8 -> "Summer";
case 9, 10, 11 -> "Autumn";
default -> "Invalid month";
};
System.out.println("Month " + month + " is in " + season);
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("EnhancedSwitches is used when building actual applications");
System.out.println("It is cleaner than long if-else chains for fixed options.");
System.out.println("Use it for menus, commands, and value-based routing.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Enhanced switch supports arrow labels and multiple cases");
System.out.println("2. It can return values directly");
System.out.println("3. Use default for fallback cases");
System.out.println("4. It is easier to read than many if-else statements");
System.out.println("5. Works well for fixed sets of values");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_EnhancedSwitches.java!");
}
''',
},
'LogicalOperators': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of LogicalOperators
System.out.println("Learning: LogicalOperators");
System.out.println("Topic #20 in Module 3");
System.out.println("Logical operators combine conditions into one decision.");
System.out.println("You use them with &&, ||, and ! to build boolean logic.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use LogicalOperators");
boolean hasID = true;
boolean isAdult = false;
if (hasID && isAdult) {
System.out.println("You may enter.");
} else if (hasID && !isAdult) {
System.out.println("You need to be older to enter.");
} else {
System.out.println("You cannot enter without ID.");
}
boolean isSunny = true;
boolean isWarm = false;
if (isSunny || isWarm) {
System.out.println("The weather is nice enough to go outside.");
} else {
System.out.println("Stay indoors today.");
}
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("LogicalOperators is used when building actual applications");
System.out.println("They help combine several conditions into one decision.");
System.out.println("Use them for validation, filtering, and game rules.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. && means AND, || means OR, ! means NOT");
System.out.println("2. Use && when all conditions must be true");
System.out.println("3. Use || when any condition can be true");
System.out.println("4. Use ! to reverse a boolean value");
System.out.println("5. Logical operators are essential for decisions");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_LogicalOperators.java!");
}
''',
},
'WhileLoops': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of WhileLoops
System.out.println("Learning: WhileLoops");
System.out.println("Topic #21 in Module 4");
System.out.println("While loops repeat code while a condition is true.");
System.out.println("You use them when you do not know exactly how many times ahead.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use WhileLoops");
int count = 1;
while (count <= 5) {
System.out.println("Count: " + count);
count++;
}
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("WhileLoops is used when building actual applications");
System.out.println("It is useful for reading data until there is no more.");
System.out.println("Use a while loop for menus, input, and repeating tasks.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. while(condition) repeats code while the condition is true");
System.out.println("2. Change the loop variable so the loop eventually stops");
System.out.println("3. Use a while loop when you do not know the exact count");
System.out.println("4. Infinite loops happen if the condition never becomes false");
System.out.println("5. While loops are common in user interaction and streaming data");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_WhileLoops.java!");
}
''',
},
'ForLoops': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of ForLoops
System.out.println("Learning: ForLoops");
System.out.println("Topic #23 in Module 4");
System.out.println("For loops repeat code a fixed number of times.");
System.out.println("They are great when you know how many repetitions you need.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use ForLoops");
for (int i = 1; i <= 5; i++) {
System.out.println("Step " + i);
}
int[] numbers = {3, 6, 9, 12};
System.out.println("\nArray values:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("ForLoops is used when building actual applications");
System.out.println("It is useful for counting, iterating arrays, and processing lists.");
System.out.println("Use a for loop when the number of items is known.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. for(start; condition; update) repeats a fixed number of times");
System.out.println("2. Use for loops to iterate arrays and ranges");
System.out.println("3. For loops are easy to read and control");
System.out.println("4. The index variable changes each time through the loop");
System.out.println("5. For loops are common in Java programs");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_ForLoops.java!");
}
''',
},
'BreakContinue': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of BreakContinue
System.out.println("Learning: BreakContinue");
System.out.println("Topic #24 in Module 4");
System.out.println("Break and continue change loop behavior.");
System.out.println("Break exits the loop, continue skips to the next iteration.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use BreakContinue");
for (int i = 1; i <= 5; i++) {
if (i == 3) {
System.out.println("Skipping " + i);
continue; // skip this iteration
}
if (i == 5) {
System.out.println("Stopping at " + i);
break; // leave the loop early
}
System.out.println("Value: " + i);
}
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("BreakContinue is used when building actual applications");
System.out.println("Use continue to ignore bad input, break to stop early.");
System.out.println("These keywords make loop logic more flexible.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. break exits the loop immediately");
System.out.println("2. continue skips the current loop iteration");
System.out.println("3. use break to stop when a condition is met");
System.out.println("4. use continue to skip items you do not want to process");
System.out.println("5. both are useful in loops and should be used carefully");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_BreakContinue.java!");
}
''',
},
'NestedLoops': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of NestedLoops
System.out.println("Learning: NestedLoops");
System.out.println("Topic #25 in Module 4");
System.out.println("Nested loops let you put one loop inside another.");
System.out.println("They are useful for grids, tables, and matrices.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use NestedLoops");
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 4; col++) {
System.out.print("(" + row + "," + col + ") ");
}
System.out.println();
}
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("NestedLoops is used when building actual applications");
System.out.println("Use them for tables, boards, and two-dimensional data.");
System.out.println("They are also useful for comparing every item with every other item.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. One loop can run inside another loop");
System.out.println("2. Nested loops are useful for rows and columns");
System.out.println("3. The inner loop runs completely every time the outer loop runs");
System.out.println("4. Use nested loops for grids, matrices, and tables");
System.out.println("5. Keep the logic clear to avoid confusion");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_NestedLoops.java!");
}
''',
},
'Methods': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of Methods
System.out.println("Learning: Methods");
System.out.println("Topic #26 in Module 5");
System.out.println("Methods let you put code inside a reusable block.");
System.out.println("You use them to organize logic and avoid repetition.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use Methods");
sayHello();
System.out.println("Sum: " + addNumbers(5, 7));
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("Methods are used when building actual applications");
System.out.println("They help split programs into smaller, easier-to-read pieces.");
System.out.println("Use methods for tasks like printing menus, calculating values, or validating input.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Methods are reusable blocks of code");
System.out.println("2. Define methods outside main and call them inside main");
System.out.println("3. Methods can return values or perform actions");
System.out.println("4. Use methods to simplify and organize programs");
System.out.println("5. Methods are a building block of Java programs");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_Methods.java!");
}
public static void sayHello() {
System.out.println("Hello from a method!");
}
public static int addNumbers(int x, int y) {
return x + y;
}
''',
},
'OverloadedMethods': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of OverloadedMethods
System.out.println("Learning: OverloadedMethods");
System.out.println("Topic #27 in Module 5");
System.out.println("Overloaded methods have the same name but different parameters.");
System.out.println("You use them when the same action works with different inputs.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use OverloadedMethods");
System.out.println("Sum two numbers: " + add(2, 3));
System.out.println("Sum three numbers: " + add(2, 3, 4));
System.out.println("Sum decimals: " + add(1.5, 2.5));
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("OverloadedMethods is used when building actual applications");
System.out.println("It helps keep the method name consistent while supporting multiple inputs.");
System.out.println("Use overloaded methods for printing, calculations, and conversions.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Overloaded methods share a name but differ in parameters");
System.out.println("2. Java chooses the correct version based on arguments");
System.out.println("3. You can overload by changing parameter count or types");
System.out.println("4. Overloading makes code easier to use");
System.out.println("5. It is common in Java standard libraries");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_OverloadedMethods.java!");
}
public static int add(int x, int y) {
return x + y;
}
public static int add(int x, int y, int z) {
return x + y + z;
}
public static double add(double x, double y) {
return x + y;
}
''',
},
'VariableScope': {
'imports': '',
'body': ''' public static int classLevel = 10;
public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of VariableScope
System.out.println("Learning: VariableScope");
System.out.println("Topic #28 in Module 5");
System.out.println("Variable scope determines where a variable can be used.");
System.out.println("You use scope rules to keep values in the right place.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use VariableScope");
int localNumber = 5;
System.out.println("Local number: " + localNumber);
System.out.println("Class-level number: " + classLevel);
if (localNumber > 0) {
int innerValue = localNumber * 2;
System.out.println("Inner value: " + innerValue);
}
// The next line would be invalid because innerValue is out of scope
// System.out.println(innerValue);
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("VariableScope is used when building actual applications");
System.out.println("It helps prevent bugs by keeping variables confined to the right block.");
System.out.println("Use local variables for temporary data and class fields for shared state.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Variable scope defines where a variable is valid");
System.out.println("2. Local variables only work inside the block where they are declared");
System.out.println("3. Class-level variables are available to methods in the class");
System.out.println("4. Inner blocks can access outer variables but not the other way around");
System.out.println("5. Keep scope small to avoid confusion");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_VariableScope.java!");
}
''',
},
'Arrays': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of Arrays
System.out.println("Learning: Arrays");
System.out.println("Topic #31 in Module 6");
System.out.println("Arrays store multiple values in one variable.");
System.out.println("You use them when you need a simple list of items.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use Arrays");
int[] numbers = {5, 10, 15, 20};
System.out.println("Array length: " + numbers.length);
for (int i = 0; i < numbers.length; i++) {
System.out.println("Index " + i + ": " + numbers[i]);
}
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("Arrays is used when building actual applications");
System.out.println("Use arrays for fixed-size collections of values.");
System.out.println("They are useful for scores, days, and simple tables.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Arrays hold multiple values in one variable");
System.out.println("2. Use new Type[size] or {value1, value2} to create arrays");
System.out.println("3. Access items with index syntax like array[0]");
System.out.println("4. Use array.length to get the number of items");
System.out.println("5. Arrays are a basic collection in Java");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_Arrays.java!");
}
''',
},
'InputArray': {
'imports': 'import java.util.Scanner;\n',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of InputArray
System.out.println("Learning: InputArray");
System.out.println("Topic #32 in Module 6");
System.out.println("Input into arrays lets users fill an array with values.");
System.out.println("You use it when you want dynamic arrays from user input.");
Scanner scanner = new Scanner(System.in);
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
// Here we show practical examples you'll use
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use InputArray");
int[] numbers = new int[3];
System.out.println("Enter 3 numbers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
System.out.println("You entered:");
for (int number : numbers) {
System.out.println(number);
}
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
// See how this concept is used in real programs
System.out.println("\nReal World Use:");
System.out.println("InputArray is used when building actual applications");
System.out.println("Use it to collect scores, names, or settings from users.");
System.out.println("Remember to close Scanner when done to free resources.");
scanner.close();
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Use Scanner to read user values into an array");
System.out.println("2. Create the array with a fixed size before filling it");
System.out.println("3. Use a loop to store each user input");
System.out.println("4. Print the array values after input");
System.out.println("5. Close the scanner when finished");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_InputArray.java!");
}
''',
},
'SearchArray': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of SearchArray
System.out.println("Learning: SearchArray");
System.out.println("Topic #33 in Module 6");
System.out.println("Search an array to find a specific value.");
System.out.println("You use it when you need to check whether an item exists.");
int[] numbers = {5, 8, 12, 20};
int target = 12;
boolean found = false;
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use SearchArray");
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == target) {
System.out.println("Found " + target + " at index " + i);
found = true;
break;
}
}
if (!found) {
System.out.println(target + " was not found.");
}
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
System.out.println("\nReal World Use:");
System.out.println("SearchArray is used when building actual applications");
System.out.println("Use it to look up values, names, or IDs in a list.");
System.out.println("A simple loop can find the first matching item.");
// ── KEY TAKEAWAYS ───────────────────────────────────────
System.out.println("\n=== WHAT YOU LEARNED ===");
System.out.println("1. Search an array with a loop");
System.out.println("2. Compare each item to the target value");
System.out.println("3. Use break to stop when found");
System.out.println("4. Use a boolean flag to record the result");
System.out.println("5. Searching arrays is a common task");
// ── NEXT STEP ───────────────────────────────────────────
System.out.println("\n✓ Ready? Go to Exercise_SearchArray.java!");
}
''',
},
'Varargs': {
'imports': '',
'body': ''' public static void main(String[] args) {
// ── SECTION 1: BASIC CONCEPT ────────────────────────────
// This section introduces the core idea of Varargs
System.out.println("Learning: Varargs");
System.out.println("Topic #34 in Module 6");
System.out.println("Varargs lets a method accept a variable number of arguments.");
System.out.println("You use it when the number of inputs can change.");
// ── SECTION 2: EXAMPLE USAGE ────────────────────────────
System.out.println("\nBasic Example:");
System.out.println("This demonstrates how to use Varargs");
System.out.println("Sum of 2 numbers: " + sum(3, 5));
System.out.println("Sum of 4 numbers: " + sum(1, 2, 3, 4));
System.out.println("Sum of 0 numbers: " + sum());
// ── SECTION 3: REAL WORLD APPLICATION ───────────────────
System.out.println("\nReal World Use:");
System.out.println("Varargs is used when building actual applications");