Java
Java
JAVA
BCA203C
Practical File
(2023-2024)
System.out.println("Hello, World!");
Output:
2.) Program to find Prime Number between two
numbers.
import java.util.Scanner;
if (num <= 1) {
return false;
if (num % i == 0) {
return false;
return true;
int n1 = scanner.nextInt();
int n2 = scanner.nextInt();
} else {
if (isPrime(i)) {
found = true;
if (!found) {
} scanner.close();
}
Output:
3.) Program to Convert String to Integer and
Integer to String.
import java.util.Scanner;
try {
} catch (NumberFormatException e) {
scanner.close();
Output:
4.) Program to display Input in Variables of
different data type.
import java.util.Scanner;
System.out.println("\nYou entered:");
scanner.close();
Output:
5.) Program to concatenate two Strings.
import java.util.Scanner;
scanner.close();
Output:
6.) Program to check whether a string is empty,
length of string, remove the white spaces, compare
two string, convert the upper case string to lower
case.
import java.util.Scanner;
if (inputString.isEmpty()) {
} else {
if (firstString.equals(secondString)) {
} else {
Output:
7.) Program to find Largest number in an Array.
import java.util.Scanner;
if (size <= 0) {
return;
array[i] = scanner.nextInt();
largest = array[i];
scanner.close();
}
Output:
8.) Program to find 2nd largest number in an Array.
import java.util.Scanner;
if (size < 2) {
return;
secondMax = max;
max = num;
secondMax = num;
if (secondMax == Integer.MIN_VALUE) {
} else {
System.out.println("The second maximum number in the array is: " +
secondMax);
scanner.close();
Output:
9.) Write a program to multiply two matrix of 3 X 3
and print the result in matrix format.
import java.util.Scanner;
matrix1[i][j] = scanner.nextInt();
}
}
matrix2[i][j] = scanner.nextInt();
System.out.print(resultMatrix[i][j] + "\t");
System.out.println();
scanner.close();
}
Output:
10.) Program to calculate Area of different Shapes.
(Method Overloading)
class CalculateArea {
Output:
11.) Write a program to initialize the private integer
variable of a class.
import java.util.Scanner;
class MyClass {
this.number = number;
return number;
// Set the value of the private variable using the setter method
obj.setNumber(userInput);
// Get and print the value of the private variable using the getter method
scanner.close();
Output:
12.) Program to initialize the private variable of a
class without the use of public functions.
(Constructor)
import java.util.Scanner;
class MyClass {
// Private variable
MyClass(int number) {
this.number = number;
void displayNumber() {
int a = sc.nextInt();
obj.displayNumber();
Output:
13.) Program with a Test class containing a
display method that prints private variables i and
j.
class Test {
Test(int i, int j) {
this.i = i;
this.j = j;
void display() {
// Object obj1: i = 0, j = 0
obj2.display();
obj3.display();
Output:
14.) Program to count the number of object
created of a class.
class Test {
Test() {
objectCount++;
return objectCount;
Output:
15.) Program to add 2 objects and store the result
in third object where all objects in same class.
class Test {
// Private variables
private int i;
private int j;
Test(int i, int j) {
this.i = i;
this.j = j;
// Method to add the values of two objects and return a new object
System.out.println("Values in obj1:");
obj1.display();
System.out.println("Values in obj2:");
obj2.display();
Output:
16.) Program having Access modifiers which store
and print the values by making objects of 2 classes.
class A {
A() {
defaultVar = 10;
privateVar = 20;
protectedVar = 30;
publicVar = 40;
return privateVar;
class B extends A {
// Object of Class A
objA.AccessAll();
// Object of Class B
objB.accessFromB();
}
Output:
17.) Program for division of 2 numbers using
Abstract Function and multiplication of 2
numbers using derived functions.
// Abstract class
abstract class Abst {
// Abstract method
abstract int calculate(int a, int b);
}
// Main class
public class Calculator {
public static void main(String[] args) {
Abst calculator;
// Perform division
calculator = new Divide();
System.out.println("Division Result: " +
calculator.calculate(20, 4));
// Perform multiplication
calculator = new Multiply();
System.out.println("Multiplication Result: " +
calculator.calculate(20, 4));
}
}
Output:
18.) Program using Super keyword to show hidden
Variables of Base class.
// Base class
class Base {
// Derived class
void displayMessages() {
// Main class
obj.displayMessages();
Output:
19.) Program of using Super keyword to show
methods of base class.
// Base class
class Base {
void display() {
System.out.println("This is the Base class method.");
}
}
// Derived class
class Derived extends Base {
@Override
void display() {
System.out.println("This is the Derived class method.");
}
void showMethods() {
System.out.println("Calling Derived class method:");
this.display(); // Calls the Derived class method
// Main class
public class SuperMethodExample {
public static void main(String[] args) {
Derived obj = new Derived();
obj.showMethods();
}
}
Output:
20.) Program of Exception handling with Checked
and Unchecked Exception.
import java.io.File;
import java.io.FileReader;
try {
} catch (ArithmeticException e) {
try {
} catch (java.io.FileNotFoundException e) {
System.out.println("Checked Exception Caught: " + e.getMessage());
Output:
21.) Program for Multithreading using Thread class.
class MyThread extends Thread {
try {
} catch (InterruptedException e) {
System.out.println(e.getMessage());
thread1.setName("Thread 1");
thread2.setName("Thread 2");
thread1.start();
thread2.start();
Output:
22.) Program for Multithreading using Runnable
Interface.
class MyRunnable implements Runnable {
try {
} catch (InterruptedException e) {
System.out.println(e.getMessage());
thread2.start();
Output: