Practical JAVA V sem
Practical JAVA V sem
System.out.println("Hello World");
Output-
Hello World
Que No 2. Implementing basic control structure such as if else, switch and loops.
import java.util.Scanner;
Output –
System.out.println(i);
Output—
5
Que No 3. Creating and using arrays in Java.
import java.util.Scanner;
scanner.close();
}
}
Enter the number of elements in the array: 5
Enter 5 integers:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Que No 4 Implementation of Inheritance in Java
@Override
System.out.println("Person Information:");
System.out.println("\n------------------------\n");
System.out.println("Student Information:");
System.out.println("\n------------------------\n");
}
Output-
Person Information:
Name: John
Age: 30
------------------------
Student Information:
Name: Alice
Age: 18
Alice is studying.
------------------------
Name: Bob
Age: 20
System.out.println("Animal:");
System.out.println("\nCat:");
Output-
Animal:
Dog:
Dog barks
Cat:
Cat meows
Dog barks
Cat meows
Que No 7 Implementing Exception handling in Java with try catch throw and finally.
}
catch (ArithmeticException e) // Handling ArithmeticException
{
System.out.println("Error: Division by zero is not allowed!");
}
try {
// Example 2: ArrayIndexOutOfBoundsException
int[] numbers = {1, 2, 3};
System.out.println("Element at index 5: " + numbers[5]); // This will
throw ArrayIndexOutOfBoundsException
}
catch (ArrayIndexOutOfBoundsException e)
{
// Handling ArrayIndexOutOfBoundsException
System.out.println("Error: Array index is out of bounds!");
}
Output
// Defining an interface
interface Animal
{
// Abstract method (no body)
void sound();
Dog's sound:
Dog barks
Cat's sound:
Cat meows
Dog sleep:
This animal is sleeping.
Cat sleep:
This animal is sleeping.
Que No 9 String operations in java concat , substring , length.
// Substring Example
String original = "Java Programming";
Output—
interface Animal
{
void sound();
interface Pet
{
void play();
}
Output-
Woof
Dog is playing
Dog is sleeping
Que No 11 Implement multi-threading Program in Java.
// Java code for thread creation by extending
// the Thread class
class MultithreadingDemo extends Thread
{
public void run()
{
try {
// Displaying the thread that is running
System.out.println("Thread " + Thread.currentThread().getId()+ " is running");
}
catch (Exception e)
{
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
// Main Class
public class Multithread
{
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++)
{
MultithreadingDemo object = new MultithreadingDemo();
object.start();
}
}
}
Output-
Thread 13 is running
Thread 12 is running
Thread 14 is running
Thread 15 is running
Thread 16 is running
Thread 17 is running
Thread 18 is running
Thread 19 is running
Que no 12 Implementing Thread-Synchronization in Java.
class Counter
{
private int count = 0;
ThreadDemo(Counter counter)
{
this.counter = counter;
}
t1.start();
t2.start();
try
{
t1.join();
t2.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Output—
class Person
{
String name;
int age;
// Default constructor
public Person()
{
// Initializing default values
name = "Unknown";
age = 0;
}
Output-
Name: Unknown
Age: 0
Que No 14 Program to implement string and string buffer class in java.
// Extracting a substring
System.out.println("Substring (7 to 12): " + result.substring(7, 12));
Output—
Output—