Skip to content

Commit 18d0eaf

Browse files
authored
Add files via upload
1 parent e2d42ed commit 18d0eaf

32 files changed

Lines changed: 1018 additions & 0 deletions
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// -- Abstract Class -- //
2+
3+
// Parent abstract class
4+
abstract class ParentClass {
5+
String parentName = "Parent";
6+
abstract void department();
7+
void show() {
8+
System.out.println("Parent class: " + parentName);
9+
}
10+
}
11+
12+
// Subclass that extends the abstract class
13+
class SubClass1 extends ParentClass {
14+
// Override the abstract method
15+
@Override
16+
void department() {
17+
System.out.println("Department: Science");
18+
}
19+
}
20+
21+
// Another subclass that extends the abstract class
22+
class SubClass2 extends ParentClass {
23+
// Override the abstract method
24+
@Override
25+
void department() {
26+
System.out.println("Department: Commerce");
27+
}
28+
}
29+
30+
// Main class - tests the abstract class and its subclasses
31+
public class Abstract_Class {
32+
// MAIN Method Class
33+
public static void main(String[] args) {
34+
SubClass1 obj1 = new SubClass1();
35+
36+
System.out.println("--- SubClass1 Details ---");
37+
obj1.show(); // Inherited method
38+
obj1.department(); // Overridden method
39+
40+
SubClass2 obj2 = new SubClass2();
41+
42+
System.out.println("\n--- SubClass2 Details ---");
43+
obj2.show(); // Inherited method
44+
obj2.department(); // Overridden method
45+
}
46+
}
47+
48+
49+
50+
51+
52+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// -- Constructors in Abstract Class -- //
2+
3+
// Abstract class with a constructor
4+
abstract class ParentClass {
5+
protected final String P;
6+
7+
// Constructor
8+
ParentClass(String P) {
9+
this.P = P;
10+
}
11+
}
12+
13+
// Subclass that extends the abstract class
14+
public class Constructors_In_Abstract_Class extends ParentClass {
15+
public void display(String str) {
16+
System.out.println("Parent Name: " + P);
17+
System.out.println("Message: " + str);
18+
}
19+
20+
// Constructor for the Main class
21+
Constructors_In_Abstract_Class(String P) {
22+
super(P); // Call the constructor of the abstract class
23+
}
24+
25+
// MAIN Method Class
26+
public static void main(String[] args) {
27+
Constructors_In_Abstract_Class obj = new Constructors_In_Abstract_Class("Parent");
28+
obj.display("Hello from the Main class!");
29+
}
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// -- Copy Constructor -- //
2+
3+
// Copy Constructor to create a new object as a copy of an existing object
4+
class Copy_Constructor {
5+
int id;
6+
String name;
7+
8+
// Parameterized Constructor to initialize the object
9+
Copy_Constructor(int id, String name) {
10+
this.id = id;
11+
this.name = name;
12+
}
13+
14+
// Copy Constructor to create a new object as a copy of an existing object
15+
Copy_Constructor(Copy_Constructor obj) {
16+
this.id = obj.id;
17+
this.name = obj.name;
18+
}
19+
20+
// Method to display object details
21+
void display() {
22+
System.out.println("ID: " + id + " | Name: " + name);
23+
}
24+
25+
// MAIN Method
26+
public static void main(String[] args) {
27+
// Original Object
28+
Copy_Constructor obj1 = new Copy_Constructor(1, "Alice");
29+
30+
// New Object initialized using the copy constructor
31+
Copy_Constructor obj2 = new Copy_Constructor(obj1);
32+
33+
System.out.println("Original Object:");
34+
obj1.display();
35+
36+
System.out.println("Copied Object:");
37+
obj2.display();
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// -- Default Constructor -- //
2+
3+
// Default Constructor - initializes object with default values when no parameters are provided
4+
class Default_Constructor {
5+
int id;
6+
String name;
7+
8+
// Default Constructor (No-argument constructor)
9+
Default_Constructor() {
10+
this.id = 0;
11+
this.name = "Unknown";
12+
System.out.println("Default Constructor called!");
13+
}
14+
15+
// Method to display object details
16+
void display() {
17+
System.out.println("ID: " + id + " | Name: " + name);
18+
}
19+
20+
// MAIN Method
21+
public static void main(String[] args) {
22+
// Creating an object using the default constructor
23+
Default_Constructor obj1 = new Default_Constructor();
24+
Default_Constructor obj2 = new Default_Constructor();
25+
26+
System.out.println("\nObject 1 Details:");
27+
obj1.display();
28+
29+
System.out.println("\nObject 2 Details:");
30+
obj2.display();
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// -- Parameterized Constructor -- //
2+
3+
// Parameterized Constructor - initializes object with provided values when parameters are passed
4+
class Parameterized_Constructor {
5+
int id;
6+
String name;
7+
8+
// Constructor to initialize the object
9+
Parameterized_Constructor(int i, String n) {
10+
id = i;
11+
name = n;
12+
}
13+
14+
// Method to display object details
15+
void display() {
16+
System.out.println("ID: " + id);
17+
System.out.println("Name: " + name);
18+
}
19+
20+
// MAIN Method
21+
public static void main(String[] args) {
22+
Parameterized_Constructor obj1 = new Parameterized_Constructor(1, "Jack");
23+
Parameterized_Constructor obj2 = new Parameterized_Constructor(2, "Jill");
24+
obj1.display();
25+
obj2.display();
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// -- Array out of bounds exception using exception handling -- //
2+
3+
public class Array_Out_Of_Bounds_Exception {
4+
public static void main(String[] args) {
5+
6+
try {
7+
// Define an array with 3 elements (indices 0, 1, 2)
8+
int[] numbers = {10, 20, 30};
9+
10+
// Attempting to access index 5, which doesn't exist
11+
System.out.println("Accessing element at index 5: " + numbers[5]);
12+
13+
} catch (ArrayIndexOutOfBoundsException e) {
14+
// This block runs if the error occurs
15+
System.out.println("Error: The array index you requested is out of range!");
16+
System.out.println("Exception message: " + e.getMessage());
17+
}
18+
19+
System.out.println("Program execution continues smoothly...");
20+
}
21+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// -- Exception handling with multiple try statements -- //
2+
3+
class Exception_With_Multiple_try_Statements {
4+
public static void main(String args[]) {
5+
System.out.println("Handling Exceptions with Multiple try Statements\n");
6+
7+
// 1st try block
8+
try {
9+
// code should raise the exception
10+
int a = 10;
11+
int b = 0;
12+
int result = a / b;
13+
System.out.println("Result: " + result);
14+
// 2nd try block
15+
try {
16+
// the code should raise an exception
17+
String str = null;
18+
System.out.println(str.length());
19+
// 3rd try block
20+
try {
21+
String num = "abc";
22+
int number = Integer.parseInt(num);
23+
System.out.println("Number: " + number);
24+
}
25+
26+
// Catch block for 3rd try block
27+
catch (NumberFormatException e) {
28+
System.out.println("NumberFormatException: 3rd try block");
29+
}
30+
}
31+
// Catch block for 2nd try block
32+
catch (NullPointerException e) {
33+
System.out.println("NullPointerException: 2nd try block");
34+
}
35+
}
36+
// Catch block for 1st try block
37+
catch (ArithmeticException e) {
38+
System.out.println("ArithmeticException: 1st try block");
39+
}
40+
41+
// Program continues after handling exceptions
42+
System.out.println("Program continues after handling exceptions.");
43+
}
44+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// -- Multiple Exceptions -- //
2+
3+
public class Multiple_Exceptions {
4+
public static void main(String[] args) {
5+
6+
System.out.println("Handling Multiple Exceptions in Java\n");
7+
8+
// ArithmeticException
9+
try {
10+
int a = 10;
11+
int b = 0;
12+
int result = a / b;
13+
System.out.println("Result: " + result);
14+
} catch (ArithmeticException e) {
15+
System.out.println("ArithmeticException: Cannot divide by zero");
16+
}
17+
18+
// NullPointerException
19+
try {
20+
String str = null;
21+
System.out.println(str.length());
22+
} catch (NullPointerException e) {
23+
System.out.println("NullPointerException: String is null");
24+
}
25+
26+
// NumberFormatException
27+
try {
28+
String num = "abc";
29+
int number = Integer.parseInt(num);
30+
System.out.println("Number: " + number);
31+
} catch (NumberFormatException e) {
32+
System.out.println("NumberFormatException: Invalid number format");
33+
}
34+
35+
// ArrayIndexOutOfBoundsException
36+
try {
37+
int arr[] = {1, 2, 3};
38+
System.out.println(arr[5]);
39+
} catch (ArrayIndexOutOfBoundsException e) {
40+
System.out.println("ArrayIndexOutOfBoundsException: Invalid array index");
41+
}
42+
43+
// StringIndexOutOfBoundsException
44+
try {
45+
String s = "Java";
46+
System.out.println(s.charAt(10));
47+
} catch (StringIndexOutOfBoundsException e) {
48+
System.out.println("StringIndexOutOfBoundsException: Invalid string index");
49+
}
50+
51+
System.out.println("Program continues after handling all exceptions.");
52+
}
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// -- Null Pointer Exception using throw keyword -- //
2+
3+
public class Null_Pointer_Exception_using_throw {
4+
static void checkString(String str) {
5+
if (str == null) {
6+
throw new NullPointerException("Error: The string is null!");
7+
} else {
8+
System.out.println("The string is: " + str);
9+
}
10+
}
11+
12+
// MAIN Method Class
13+
public static void main(String[] args) {
14+
checkString("Hello, World!"); // This will not throw an exception
15+
checkString(null); // This will throw a NullPointerException
16+
}
17+
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// -- throw keyword -- //
2+
3+
public class throw_Keyword {
4+
static void checkAge(int age) {
5+
if (age < 18) {
6+
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
7+
} else {
8+
System.out.println("Access granted - You are eligible for voting.");
9+
}
10+
}
11+
12+
// MAIN Method Class
13+
public static void main(String[] args) {
14+
checkAge(22); // This will not throw an exception
15+
checkAge(14); // This will throw an exception
16+
}
17+
}

0 commit comments

Comments
 (0)