-
Notifications
You must be signed in to change notification settings - Fork 0
/
combined.txt
1300 lines (1013 loc) · 40.4 KB
/
combined.txt
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
---
File: ./ReadME.md
---
Welcome to the **Java Zero to Hero Guide**, a comprehensive resource designed for beginners to learn Java from scratch. Each folder in this repository contains examples and exercises that build upon the previous concepts.
By following this structured learning path, you'll gain a deep understanding of Java, from basics to advanced topics like multithreading, networking, and GUI development.
---
## How to Use This Repository
1. **Clone this Repository**: Use `git clone` or download the zip file.
2. **Follow the Learning Path**: Start with the first folder and gradually work your way through each folder.
3. **Run the Examples**: Each folder contains example code with explanations in the `README` file.
4. **Try the Exercises**: Attempt the exercises provided in each folder before checking the solutions.
5. **Build your Own Projects**: Once you feel confident, try creating your own projects.
---
## Table of Contents
### Beginner
1. [Java Basics](Concepts/01_Basics)
2. [Arrays & Strings](Concepts/02_Arrays_Strings)
### Intermediate
3. [Classes & Objects](Concepts/03_Classes_Objects)
4. [Inheritance & Polymorphism](Concepts/04_Inheritance)
5. [Exception Handling](Concepts/05_Exception_Handling)
### Advanced
6. [Multithreading](Concepts/06_Multithreading)
7. [Networking](Concepts/07_Networking)
8. [JDBC](Concepts/08_JDBC)
9. [AWT & Swing](Concepts/09_AWT_Swing)
---
### Requirements
- JDK installed: [Install JDK](https://www.oracle.com/java/technologies/javase-jdk11-downloads.html)
- IDE: You can use any Java IDE, but I recommend [VS Code](https://code.visualstudio.com/) or [IntelliJ IDEA](https://www.jetbrains.com/idea/).
- Basic knowledge of programming (optional but helpful).
---
### Cheatsheets
- [Princeton University Java Cheatsheet](https://introcs.cs.princeton.edu/java/11cheatsheet/)
- [Code with Harry Java Cheatsheet](https://www.codewithharry.com/blogpost/java-cheatsheet/)
---
### Roadmap
Below is the roadmap image to guide you through the learning path:
[![Java Learning Roadmap](Assets/roadmap.sh_java.png)](https://roadmap.sh/java)
---
### Additional Resources
For a more detailed roadmap on learning Java, you can visit the [Java Roadmap](https://roadmap.sh/java).
---
Start with the [Java Basics](Concepts/01_Basics) to begin your journey!
---
File: ./Concepts/05_Exception_Handling/README.md
---
# Packages & Interfaces
This section teaches you how to organize code using packages and how to work with interfaces to achieve abstraction.
## Topics Covered:
1. **Packages** - Grouping related classes together.
2. **Access Protection** - How access modifiers affect package visibility.
3. **Interfaces** - Defining contracts without implementation.
## Example Programs:
- `PackageDemo.java` - Show how to create and use a package.
- `StackInterface.java` - Implement stack using an interface.
## Exercises:
1. Write a program showing the usage of a package.
2. Write a program to implement a stack using an interface.
Now, move on to **Exception Handling**.
---
## Files
- **TryCatchDemo.java**: Demonstrates try, catch, throw, throws, and finally.
- **UserDefinedException.java**: Demonstrates user-defined exceptions.
- **VoterAgeCheck.java**: Throws exception if age is less than 18.
- **StackException.java**: Throws exception if stack is full or empty.
- **BankBalanceException.java**: Throws exception if bank balance drops below 1000.
- **SavingsAccount.java**: Demonstrates exception handling in banking operations.
---
File: ./Concepts/05_Exception_Handling/SavingsAccount.java
---
// Class representing a savings account with basic operations and exception handling.
// This file may show some errors but it is intentional to demonstrate exception handling.
public class SavingsAccount {
private double balance;
public SavingsAccount(double initialBalance) {
if (initialBalance < 0) {
throw new IllegalArgumentException("Initial balance cannot be negative.");
}
this.balance = initialBalance;
}
public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Deposit amount must be positive.");
}
balance += amount;
}
public void withdraw(double amount) throws BankBalanceException {
if (amount <= 0) {
throw new IllegalArgumentException("Withdrawal amount must be positive.");
}
if (balance - amount < 1000) {
throw new BankBalanceException("Balance cannot drop below 1000 after withdrawal.");
}
balance -= amount;
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
try {
SavingsAccount account = new SavingsAccount(1500);
account.deposit(500);
account.withdraw(2000); // This will throw BankBalanceException
} catch (BankBalanceException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
---
File: ./Concepts/05_Exception_Handling/VoterAgeCheck.java
---
// Program to check voter eligibility based on age.
import java.util.Scanner;
public class VoterAgeCheck {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = input.nextInt();
try {
checkAge(age);
System.out.println("You are eligible to vote.");
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
public static void checkAge(int age) throws Exception {
if (age < 18) {
throw new Exception("Age must be at least 18 to vote.");
}
}
}
---
File: ./Concepts/05_Exception_Handling/UserDefinedException.java
---
// Custom exception class for demonstrating user-defined exceptions.
public class UserDefinedException extends Exception {
public UserDefinedException(String message) {
super(message); // Call the constructor of the superclass (Exception) with the error message.
}
}
---
File: ./Concepts/05_Exception_Handling/BankBalanceException.java
---
// Custom exception class for handling low bank balance scenarios.
public class BankBalanceException extends Exception {
public BankBalanceException(String message) {
super(message); // Call the constructor of the superclass (Exception) with the error message.
}
}
---
File: ./Concepts/05_Exception_Handling/TryCatchDemo.java
---
// Demonstrates basic exception handling using try, catch, and finally blocks.
public class TryCatchDemo {
public static void main(String[] args) {
try {
int[] numbers = { 1, 2, 3 };
System.out.println(numbers[5]); // This will throw ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught: " + e.getMessage());
} finally {
System.out.println("This block is always executed.");
}
}
}
---
File: ./Concepts/05_Exception_Handling/StackException.java
---
// Custom exception class for handling stack overflow and underflow scenarios.
public class StackException extends Exception {
public StackException(String message) {
super(message); // Call the constructor of the superclass (Exception) with the error message.
}
}
---
File: ./Concepts/06_Multithreading/README.md
---
# Exception Handling
In this section, you will learn how to handle runtime errors in Java using exception handling mechanisms.
## Topics Covered:
1. **Try-Catch Blocks** - Handling exceptions using `try`, `catch`, and `finally`.
2. **Throwing Exceptions** - How to throw exceptions using `throw` and `throws`.
3. **User-Defined Exceptions** - Creating your own exception classes.
## Example Programs:
- `TryCatchDemo.java` - Demonstrate basic exception handling with `try` and `catch`.
- `UserDefinedException.java` - Create a custom exception class.
- `SavingsAccount.java` - Throw an exception if withdrawal amount exceeds balance.
## Exercises:
1. Write a program to handle an invalid age exception (age < 18).
2. Create a program to simulate a bank transaction and throw an exception if the balance drops below 1000.
Move on to **Multithreading** once you've mastered exception handling.
---
## Files
- **ThreadDemo.java**: Demonstrates creating threads using Thread class and Runnable interface.
- **Multiples.java**: Prints multiples of 2, 3, and 4 simultaneously.
- **ThreadPriority.java**: Demonstrates thread priority.
- **ThreadSync.java**: Demonstrates thread synchronization and inter-thread communication.
---
File: ./Concepts/06_Multithreading/ThreadSync.java
---
---
File: ./Concepts/06_Multithreading/ThreadPriority.java
---
---
File: ./Concepts/06_Multithreading/ThreadDemo.java
---
---
File: ./Concepts/06_Multithreading/Multiples.java
---
---
File: ./Concepts/07_Networking/HostNameToIP.java
---
---
File: ./Concepts/07_Networking/EchoServer.java
---
---
File: ./Concepts/07_Networking/README.md
---
# Networking in Java
Welcome to the **Networking** section! This section focuses on how Java allows computers to communicate over a network using various protocols.
## Topics Covered:
1. **IP Address Lookup** - How to retrieve the IP address of a host.
2. **Sockets** - Using TCP/IP sockets to build a client-server application.
3. **Echo Server/Client** - Build simple echo server-client programs.
## Example Programs:
1. **`HostNameToIP.java`** - A program that accepts a hostname and displays its IP address.
- **Concepts**: Working with `InetAddress` to resolve IP addresses.
2. **`EchoServer.java` & `EchoClient.java`** - A pair of programs where the client sends a message to the server, and the server echoes it back.
- **Concepts**: Working with `ServerSocket`, `Socket`, and streams.
3. **`TCPClientServer.java`** - A simple client-server program using TCP/IP, where the client sends messages to the server.
- **Concepts**: Using TCP/IP sockets to exchange messages between client and server.
## Exercises:
1. Write a program to display the IP address of any website by accepting the hostname from the user.
2. Write a simple echo server-client program where the server echoes whatever the client sends.
3. Create a TCP client-server application that sends a message from the client to the server and logs the message on both ends.
---
### Moving Forward:
After understanding the basics of networking, you can dive deeper into more advanced network programming topics like multithreaded servers, file transfer protocols, and web services.
---
## Files
- **HostNameToIP.java**: Displays IP address of a given host name.
- **EchoServer.java**: Echo server using sockets.
- **EchoClient.java**: Echo client using sockets.
- **TCPClientServer.java**: TCP/IP client-server program.
---
File: ./Concepts/07_Networking/TCPClientServer.java
---
---
File: ./Concepts/07_Networking/EchoClient.java
---
---
File: ./Concepts/03_Classes_Objects/UseStatic.java
---
// Demonstrates the use of static members.
public class UseStatic {
static int staticVar = 10; // Static variable
int instanceVar = 20; // Instance variable
// Static method to print static variable
static void staticMethod() {
System.out.println("Static variable: " + staticVar);
}
// Instance method to print instance variable
void instanceMethod() {
System.out.println("Instance variable: " + instanceVar);
}
// Static block to initialize static variables
static {
System.out.println("Static block executed.");
staticVar = 30;
}
public static void main(String[] args) {
// Call static method
UseStatic.staticMethod();
// Create an instance of UseStatic
UseStatic obj = new UseStatic();
// Call instance method
obj.instanceMethod();
}
}
---
File: ./Concepts/03_Classes_Objects/README.md
---
# Classes & Objects
Now that you have a solid foundation in Java, let's dive into Object-Oriented Programming (OOP). In this section, you'll learn how to create classes and objects, and how to use methods and static variables.
## Topics Covered:
1. **Classes & Objects** - Defining and using classes and objects.
2. **Constructors** - Default and parameterized constructors.
3. **Methods** - Creating methods and method overloading.
4. **Static Keyword** - Using static variables and static blocks.
## Example Programs:
- `Box.java` - Create a class to calculate the volume of a box.
- `UseStatic.java` - Demonstrate the use of static methods, variables, and blocks.
- `TestReference.java` - Illustrate passing objects as parameters.
## Exercises:
1. Create a class `Box` and calculate its volume.
2. Create a class `Triangle` and calculate its area using a parameterized constructor.
3. Write a program to demonstrate static variables and methods.
Once you grasp OOP basics, move on to **Inheritance & Polymorphism**.
---
## Files
- **Box.java**: Demonstrates the creation and use of a simple class.
- **TestReference.java**: Tests object references and their behavior.
- **UseStatic.java**: Demonstrates the use of static members.
---
File: ./Concepts/03_Classes_Objects/TestReference.java
---
// Tests object references and their behavior.
public class TestReference {
int value;
// Constructor to initialize the value
TestReference(int value) {
this.value = value;
}
// Method to modify the value
void modifyValue(TestReference obj) {
obj.value += 10;
}
public static void main(String[] args) {
// Create an instance of TestReference
TestReference ref = new TestReference(20);
System.out.println("Before modification: " + ref.value);
// Modify the value using the method
ref.modifyValue(ref);
System.out.println("After modification: " + ref.value);
}
}
---
File: ./Concepts/03_Classes_Objects/Box.java
---
// Demonstrates the creation and use of a simple class.
public class Box {
double width;
double height;
double depth;
// Constructor to initialize the dimensions of the box
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// Method to calculate the volume of the box
double volume() {
return width * height * depth;
}
public static void main(String[] args) {
// Create an instance of Box
Box myBox = new Box(10, 20, 15);
// Calculate and print the volume of the box
System.out.println("Volume of the box is: " + myBox.volume());
}
}
---
File: ./Concepts/02_Arrays_Strings/MatrixOperations.java
---
// Perform operations on matrices.
public class MatrixOperations {
public static void main(String[] args) {
// Declare and initialize two matrices
int[][] matrix1 = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
int[][] matrix2 = {
{ 9, 8, 7 },
{ 6, 5, 4 },
{ 3, 2, 1 }
};
// Add the matrices
int[][] sum = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Print the sum of the matrices
System.out.println("Sum of the matrices:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
// Multiply the matrices
int[][] product = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
product[i][j] = 0;
for (int k = 0; k < 3; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Print the product of the matrices
System.out.println("Product of the matrices:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(product[i][j] + " ");
}
System.out.println();
}
}
}
---
File: ./Concepts/02_Arrays_Strings/ArraysDemo.java
---
// Demonstrates basic array operations.
public class ArraysDemo {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = { 1, 2, 3, 4, 5 };
// Print the elements of the array
System.out.println("Array elements:");
for (int number : numbers) {
System.out.println(number);
}
// Calculate the sum of the array elements
int sum = 0;
for (int number : numbers) {
sum += number;
}
System.out.println("Sum of array elements: " + sum);
// Find the largest element in the array
int max = numbers[0];
for (int number : numbers) {
if (number > max) {
max = number;
}
}
System.out.println("Largest element in the array: " + max);
}
}
---
File: ./Concepts/02_Arrays_Strings/README.md
---
This section focuses on Arrays and Strings, two key components of Java. You'll learn how to store, manipulate, and search through data in arrays and perform various string operations.
## Topics Covered:
1. **Arrays** - Declaration, initialization, sorting, and searching.
2. **Strings** - String manipulation, functions like `length()`, `charAt()`, and `equals()`.
## Example Programs:
- `ArraysDemo.java` - Demonstrates how to initialize and print elements of an array.
- `Sorting.java` - Sort an array in ascending and descending order.
- `BinarySearch.java` - Perform binary search on an array of integers.
- `LinearSearch.java` - Perform linear search on an array of integers.
- `MatrixOperations.java` - Perform addition and multiplication on matrices.
- `SmallestElement.java` - Find the smallest element in an array.
- `StringDemo.java` - Show usage of basic String methods.
## Exercises:
1. Write a program to initialize and print elements of an array.
2. Write a program to perform a linear search on an array.
3. Write a program to demonstrate any 10 String class functions.
4. Write a program to sort an array of integers in ascending and descending order.
5. Write a program to perform addition and multiplication on matrices.
6. Write a program to find the smallest element in an array.
Once you've understood arrays and strings, proceed to **Classes & Objects**.
---
## Files
- **ArraysDemo.java**: Demonstrates basic array operations.
- **BinarySearch.java**: Implements binary search algorithm.
- **LinearSearch.java**: Implements linear search algorithm.
- **MatrixOperations.java**: Perform operations on matrices.
- **SmallestElement.java**: Find the smallest element in an array.
- **Sorting.java**: Demonstrates sorting algorithms.
---
File: ./Concepts/02_Arrays_Strings/LinearSearch.java
---
// Implements linear search algorithm.
import java.util.Scanner;
public class LinearSearch {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Declare and initialize an array
int[] numbers = { 10, 20, 30, 40, 50 };
// Read the target value from the user
System.out.print("Enter the number to search: ");
int target = input.nextInt();
// Perform linear search
boolean found = false;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == target) {
found = true;
System.out.println("Number found at index: " + i);
break;
}
}
if (!found) {
System.out.println("Number not found in the array.");
}
}
}
---
File: ./Concepts/02_Arrays_Strings/SmallestElement.java
---
// Find the smallest element in an array.
public class SmallestElement {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = { 5, 3, 8, 1, 2 };
// Find the smallest element
int smallest = numbers[0];
for (int number : numbers) {
if (number < smallest) {
smallest = number;
}
}
System.out.println("Smallest element in the array: " + smallest);
}
}
---
File: ./Concepts/02_Arrays_Strings/BinarySearch.java
---
// Implements binary search algorithm.
import java.util.Arrays;
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Declare and initialize a sorted array
int[] numbers = { 10, 20, 30, 40, 50 };
// Read the target value from the user
System.out.print("Enter the number to search: ");
int target = input.nextInt();
// Perform binary search
int result = Arrays.binarySearch(numbers, target);
if (result >= 0) {
System.out.println("Number found at index: " + result);
} else {
System.out.println("Number not found in the array.");
}
}
}
---
File: ./Concepts/02_Arrays_Strings/Sorting.java
---
// Demonstrates sorting algorithms.
import java.util.Arrays;
public class Sorting {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = { 5, 3, 8, 1, 2 };
// Sort the array in ascending order
Arrays.sort(numbers);
System.out.println("Array sorted in ascending order: " + Arrays.toString(numbers));
// Sort the array in descending order
Integer[] numbersDesc = { 5, 3, 8, 1, 2 };
Arrays.sort(numbersDesc, (a, b) -> b - a);
System.out.println("Array sorted in descending order: " + Arrays.toString(numbersDesc));
}
}
---
File: ./Concepts/04_Inheritance/AbstractFigure.java
---
// Demonstrates the use of abstract classes and methods.
abstract class Figure {
double dimension1;
double dimension2;
// Constructor to initialize dimensions
Figure(double d1, double d2) {
dimension1 = d1;
dimension2 = d2;
}
// Abstract method to calculate area
abstract double area();
}
class Rectangle extends Figure {
// Constructor to initialize dimensions
Rectangle(double d1, double d2) {
super(d1, d2);
}
// Implement the abstract method to calculate area
double area() {
return dimension1 * dimension2;
}
}
class Triangle extends Figure {
// Constructor to initialize dimensions
Triangle(double d1, double d2) {
super(d1, d2);
}
// Implement the abstract method to calculate area
double area() {
return 0.5 * dimension1 * dimension2;
}
}
public class AbstractFigure {
public static void main(String[] args) {
// Create instances of Rectangle and Triangle
Figure rectangle = new Rectangle(10, 20);
Figure triangle = new Triangle(10, 20);
// Print the area of the rectangle and triangle
System.out.println("Area of Rectangle: " + rectangle.area());
System.out.println("Area of Triangle: " + triangle.area());
}
}
---
File: ./Concepts/04_Inheritance/DynamicDispatch.java
---
// Demonstrates dynamic method dispatch (runtime polymorphism).
class A {
// Method to be overridden
void display() {
System.out.println("Inside A's display method");
}
}
class B extends A {
// Override the display method
void display() {
System.out.println("Inside B's display method");
}
}
class C extends A {
// Override the display method
void display() {
System.out.println("Inside C's display method");
}
}
public class DynamicDispatch {
public static void main(String[] args) {
A a; // Declare a reference of type A
// Create instances of B and C
B b = new B();
C c = new C();
// Point the reference to an instance of B
a = b;
a.display(); // Calls B's display method
// Point the reference to an instance of C
a = c;
a.display(); // Calls C's display method
}
}
---
File: ./Concepts/04_Inheritance/SuperDemo.java
---
// Demonstrates the use of the super keyword to call the parent class constructor.
class Parent {
int value;
// Constructor to initialize value
Parent(int value) {
this.value = value;
System.out.println("Parent class constructor called. Value: " + value);
}
}
class Child extends Parent {
// Constructor to initialize value and call the parent class constructor
Child(int value) {
super(value); // Call the parent class constructor
System.out.println("Child class constructor called. Value: " + value);
}
}
public class SuperDemo {
public static void main(String[] args) {
// Create an instance of Child
Child child = new Child(10);
}
}
---
File: ./Concepts/04_Inheritance/README.md
---
# Inheritance & Polymorphism
Inheritance allows you to define a new class based on an existing class. In this section, you'll explore inheritance, polymorphism, and abstract classes.
## Topics Covered:
1. **Inheritance** - Reusing code from existing classes.
2. **Polymorphism** - Using a single interface to represent different types.
3. **Super Keyword** - Accessing parent class members.
4. **Abstract Classes** - Working with abstract classes and methods.
## Example Programs:
- `BoxWeight.java` - Inherit class `Box` and add the `weight` variable.
- `SuperDemo.java` - Use the `super` keyword to call the parent class constructor.
- `AbstractFigure.java` - Demonstrate the use of abstract classes and methods.
- `DynamicDispatch.java` - Demonstrate dynamic method dispatch (runtime polymorphism).
## Exercises:
1. Create a class `BoxWeight` that inherits from `Box` and adds a `weight` attribute.
2. Write a program to implement polymorphism using dynamic method dispatch.
3. Create an abstract class `Figure` and implement `Rectangle` and `Triangle` subclasses.
Continue to **Packages & Interfaces** after completing these.
---
## Files
- **AbstractFigure.java**: Demonstrates the use of abstract classes and methods.
- **BoxWeight.java**: Inherit class `Box` and add the `weight` variable.
- **DynamicDispatch.java**: Demonstrates dynamic method dispatch (runtime polymorphism).
- **SuperDemo.java**: Demonstrates the use of the `super` keyword to call the parent class constructor.
---
File: ./Concepts/04_Inheritance/BoxWeightDemo.java
---
// Inherit class Box and add the weight variable.
class Box {
double width;
double height;
double depth;
// Constructor to initialize dimensions
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// Method to calculate volume
double volume() {
return width * height * depth;
}
}
class BoxWeight extends Box {
double weight;
// Constructor to initialize dimensions and weight
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // Call the parent class constructor
weight = m;
}
}
public class BoxWeightDemo {
public static void main(String[] args) {
// Create an instance of BoxWeight
BoxWeight myBox = new BoxWeight(10, 20, 15, 34.3);
// Print the volume and weight of the box
System.out.println("Volume of the box is: " + myBox.volume());
System.out.println("Weight of the box is: " + myBox.weight);
}
}
---
File: ./Concepts/09_AWT_Swing/SwingRGB.java
---
---
File: ./Concepts/09_AWT_Swing/SwingControls.java
---
---
File: ./Concepts/09_AWT_Swing/BorderLayoutApplet.java
---
---
File: ./Concepts/09_AWT_Swing/FontApplet.java
---
---
File: ./Concepts/09_AWT_Swing/CalculatorApplet.java
---
---
File: ./Concepts/09_AWT_Swing/SwingFont.java
---
---
File: ./Concepts/09_AWT_Swing/SwingLogin.java
---
---
File: ./Concepts/09_AWT_Swing/README.md
---
# AWT & Swing in Java
In this section, you'll learn how to create graphical user interfaces (GUIs) using **AWT (Abstract Window Toolkit)** and **Swing**. AWT provides basic building blocks for creating windows, while Swing extends AWT to provide more advanced features and components.
## Topics Covered:
1. **AWT Basics** - Creating simple applets and handling user input.
2. **Swing Basics** - Creating windows, adding controls, and handling events.
3. **Layouts** - Managing component positioning with different layout managers like `BorderLayout`.
4. **RGB Colors** - Working with colors using scroll bars.
## Example Programs:
### AWT:
1. **`LoginApplet.java`** - An applet that creates a simple login window with username and password fields.
- **Concepts**: Using `TextField` and `Button` in an applet.
2. **`FontApplet.java`** - A program that lets the user change the font of text using control boxes for font name, size, and style.
- **Concepts**: Working with `Choice` boxes and font manipulation.
3. **`CalculatorApplet.java`** - A 3-function calculator (addition, subtraction, multiplication) implemented as an applet.
- **Concepts**: Handling button click events in applets.
4. **`RGBApplet.java`** - A program that uses scrollbars to adjust RGB color combinations and change the background color.
- **Concepts**: Working with `Scrollbar` and `Color` in applets.
### Swing:
1. **`SwingLogin.java`** - A Swing version of the login window with username and password fields.
- **Concepts**: Using `JTextField` and `JButton` in Swing.
2. **`SwingFont.java`** - A Swing application that lets the user change the font of the text using control boxes.
- **Concepts**: Handling events and changing styles in Swing.
3. **`SwingCalculator.java`** - A 3-function calculator created using Swing components.
- **Concepts**: Handling basic arithmetic operations with `JButton` and `JTextField`.
4. **`SwingRGB.java`** - A program that uses scrollbars in Swing to display color combinations of RGB values.
- **Concepts**: Handling `JScrollBar` events in Swing.
5. **`SwingBorderLayout.java`** - A demonstration of using the `BorderLayout` layout manager in Swing.
- **Concepts**: Arranging components with `BorderLayout`.
## Exercises:
1. Create a simple login applet that checks if the username and password match predefined values.
2. Write an applet that uses scrollbars to change the background color of the window.
3. Create a Swing application with three control boxes to select a font, size, and style, and apply the selected settings to a text label.
4. Write a Swing application that uses at least five different controls, such as buttons, text fields, checkboxes, etc.
---
### Moving Forward:
After learning AWT and Swing basics, you can dive into more advanced topics like custom painting, event-driven programming, and integrating GUI with backend data using JDBC.
---