0% found this document useful (0 votes)
20 views10 pages

Java 7,8

Java manual

Uploaded by

20harish08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Java 7,8

Java manual

Uploaded by

20harish08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

AIM:

To implement the concept of threading by extending the thread class.

SOFTWARE REQUIRED:

ALGORITHM:
STEP 1: Create a class named MyThread that extends the Thread class.
STEP 2: Override the run() method inside the MyThread class.
STEP 3: Inside the run() method, print the message "Thread is running.".
STEP 4: In the main method of the ThreadExample class, create an instance of the MyThread
class.
STEP 5: Call the start() method on the created MyThread instance to start the thread
execution.

PROGRAM:
// Define a class that extends the Thread class
class MyThread extends Thread {
// Override the run() method to specify the thread's task
public void run() {
System.out.println("Thread is running.");
}
}
// Main class to start the thread
public class ThreadExample {
public static void main(String[] args) {
// Create an instance of MyThread
MyThread thread = new MyThread();

// Start the thread, which calls the run() method in a new thread
thread.start();
}
}

Sample Output:

RESULT:
Thus, the concept of threading by extending the thread class is implemented and
executed successfully.
AIM:
To implement the concept of threading by implementing runnable interface.

SOFTWARE REQUIRED:

Algorithm
STEP 1: Create a class named MyRunnable that implements the Runnable interface.
STEP 2: Implement the run() method inside the MyRunnable class.
STEP 3: Inside the run() method, print the message "Thread is running."
STEP 4: Create an instance of the MyRunnable class.
STEP 5: Create a new Thread object, passing the created instance of MyRunnable as an
argument to the Thread constructor. Then, call the start() method on the Thread object to start
the execution of the thread.

PROGRAM:
class CounterRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 3; i++) {
System.out.println("Count: " + i);
try {
Thread.sleep(300); // Pause for 300 milliseconds
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
}
}
}
}

public class RunnableExample2 {


public static void main(String[] args) {
Thread thread = new Thread(new CounterRunnable());
thread.start();
}
}

Sample Output:

RESULT:
Thus, the concept of threading by implementing runnable interface is
implemented and executed successfully.
AIM:
To implement the concept of exception handling using predefined exception.

SOFTWARE REQUIRED:

ALGORITHM:
1. Start
2. Create an integer array of size 5.
3. Assign values to some valid positions of the array.
4. Try block:
 Attempt to assign a value to index 10 (invalid index).
 If successful, print "Array element assigned successfully."
5. Catch block:
 If ArrayIndexOutOfBoundsException occurs, print "ARRAY INDEX IS OUT OF
RANGE...".
6. Finally block:
 Print "I AM FROM FINALLY...".
7. Stop

PROGRAM:
class ArrayExceptionDemo {
public static void main(String[] args) {
try {
// Create an array of size 5
int[] arr = new int[5];

// Assign values
arr[0] = 10;
arr[1] = 20;

// Intentionally accessing invalid index


arr[10] = 100;

System.out.println("Array element assigned successfully.");


}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ARRAY INDEX IS OUT OF RANGE...");
}
finally {
System.out.println("I AM FROM FINALLY...");
}
}
}
Sample Output:

RESULT:
Thus, the concept of exception handling using predefined exception is
implemented and executed successfully.
AIM:
To implement the concept of exception handling by creating user defined exception.

SOFTWARE REQUIRED:

ALGORITHM:
1. Start
2. Create a user-defined exception class InvalidMarksException extending Exception.
3. In main(), read student name and marks from the user.
4. Check condition:
o If marks < 0 or marks > 100, then throw InvalidMarksException with an error
message.
o Else, print student details (name and marks).
5. Catch the exception and display the error message.
6. Execute the finally block to print "Program execution completed.".
7. Stop

PROGRAM:
import java.util.Scanner;
// User-defined exception
class InvalidMarksException extends Exception {
InvalidMarksException(String msg) {
super(msg);
}
}
public class StudentDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
// Input student details
System.out.print("Enter Student Name: ");
String name = sc.nextLine();
System.out.print("Enter Marks (0 - 100): ");
int marks = sc.nextInt();

// Validate marks
if (marks < 0 || marks > 100) {
throw new InvalidMarksException("Marks must be between 0 and 100!");
}
// Print result if valid
System.out.println("Student: " + name + " | Marks: " + marks);
}
catch (InvalidMarksException e) {
System.out.println("Exception Caught: " + e.getMessage());
}
finally {
System.out.println("Program execution completed.");
}

sc.close();
}
}
Sample Output:

RESULT:
Thus, the concept of exception handling by creating user defined exception is
implemented and executed successfully.

You might also like