Q1- WAP that implements the concept of Encapsulation.
PROGRAM:-
public class CLG_01_Encapsulation {
public void printName() {
private String name;
name = maya;
[Link]("Name: " + name);
}
public static void main(String[] args) {
CLG_01_Encapsulation namePrinter = new CLG_01_Encapsulation();
[Link]();
}
}
OUTPUT:-
1
Q2- WAP to demonstrate concept of function overloading of Polymorphism
PROGRAM:-
public class CLG_02_overloading {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public String add(String str1, String str2) {
return str1 + str2;
}
public static void main(String[] args) {
CLG_02_overloading example = new CLG_02_overloading();
int sumInt = [Link](5, 10);
[Link]("Sum of integers: " + sumInt);
double sumDouble = [Link](3.5, 2.7);
[Link]("Sum of doubles: " + sumDouble);
String concatenatedString = [Link]("Hello, ", "World!");
[Link]("Concatenated string: " + concatenatedString);
}
}
OUTPUT:-
2
Q3- WAP to demonstrate concept of function overloading of Polymorphism
PROGRAM:-
public class CLG_03_ConstructorOverloadingExample {
private int value;
public CLG_03_ConstructorOverloadingExample() {
[Link] = 0;
}
public CLG_03_ConstructorOverloadingExample(int value) {
[Link] = value;
}
public CLG_03_ConstructorOverloadingExample(int a, int b) {
[Link] = a + b;
}
public void displayValue() {
[Link]("Value: " + value);
}
public static void main(String[] args) {
CLG_03_ConstructorOverloadingExample example1 = new
CLG_03_ConstructorOverloadingExample();
[Link]();
CLG_03_ConstructorOverloadingExample example2 = new
CLG_03_ConstructorOverloadingExample(10);
[Link]();
CLG_03_ConstructorOverloadingExample example3 = new
CLG_03_ConstructorOverloadingExample(5, 8);
[Link]();
}
}
OUTPUT:-
3
Q4- WAP to use Boolean data type and print the Prime Number series up to 50.
PROGRAM:-
public class CLG_04_PrimeNumberSeries {
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
[Link]("Prime Numbers up to 50:");
for (int i = 2; i <= 50; i++) {
if (isPrime(i)) {
[Link](i + " ");
}
}
}
}
OUTPUT:-
4
Q5- WAP to print first 10 number of the following series using do while loop
0,1,1,2,3,5,8,11.
PROGRAM:-
public class CLG_05_FibonacciSeries {
public static void main(String[] args) {
int n = 10; // Number of terms to print
int count = 0;
int first = 0;
int second = 1;
[Link]("First " + n + " numbers of the series:");
do {
[Link](first + " ");
int next = first + second;
first = second;
second = next;
count++;
} while (count < n);
}
}
OUTPUT:-
5
Q6- WAP to check the given number is Armstrong or not.
PROGRAM:-
import [Link];
public class CLG_06_ArmstrongNumberCheck {
public static boolean isArmstrong(int number) {
int originalNumber, remainder, result = 0, n = 0;
originalNumber = number;
while (originalNumber != 0) {
originalNumber /= 10;
++n;
}
originalNumber = number;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += [Link](remainder, n);
originalNumber /= 10;
}
return result == number;
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number to check if it's an Armstrong number: ");
int number = [Link]();
if (isArmstrong(number)) {
[Link](number + " is an Armstrong number.");
} else {
[Link](number + " is not an Armstrong number.");
}
[Link]();
}
}
OUTPUT:-
6
Q7- WAP to find the factorial of any given number.
PROGRAM:-
import [Link];
public class CLG_07_FactorialCalculator {
public static long calculateFactorial(int number) {
if (number < 0) {
return -1;
} else if (number == 0 || number == 1) {
return 1;
} else {
long factorial = 1;
for (int i = 2; i <= number; i++) {
factorial *= i;
}
return factorial;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number to calculate its factorial: ");
int number = [Link]();
long result = calculateFactorial(number);
if (result == -1) {
[Link]("Factorial is not defined for negative numbers.");
} else {
[Link]("Factorial of " + number + " is: " + result);
}
[Link]();
}
}
OUTPUT:-
7
Q8- WAP to sort the elements of One Dimensional Array in Ascending order.
PROGRAM:-
public class CLG_08_ArraySorting {
public static void sortArrayAscending(int[] array) {
int n = [Link];
boolean swapped;
do {
swapped = false;
for (int i = 1; i < n; i++) {
if (array[i - 1] > array[i]) {
int temp = array[i - 1];
array[i - 1] = array[i];
array[i] = temp;
swapped = true;
}
}
n--;
} while (swapped);
}
public static void main(String[] args) {
int[] numbers = {5, 2, 9, 1, 5, 6};
[Link]("Original array: ");
for (int number : numbers) {
[Link](number);
}
sortArrayAscending(numbers);
[Link]("\nSorted array in ascending order: ");
for (int number : numbers) {
[Link](number);
}
}
}
OUTPUT:-
8
Q9- WAP for matrix multiplication using input/output stream.
PROGRAM:-
import [Link].*;
public class CLG_09_MatrixMultiplication {
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = [Link];
int cols1 = matrix1[0].length;
int cols2 = matrix2[0].length;
int[][] result = new int[rows1][cols2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return result;
}
public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int value : row) {
[Link](value + " ");
}
[Link]();
}
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the dimensions of the first matrix:");
[Link]("Rows: ");
int rows1 = [Link]([Link]());
[Link]("Columns: ");
int cols1 = [Link]([Link]());
int[][] matrix1 = new int[rows1][cols1];
[Link]("Enter the elements of the first matrix:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
[Link]("Element at position [" + (i + 1) + "][" + (j + 1) + "]: ");
matrix1[i][j] = [Link]([Link]());
}
9
}
[Link]("Enter the dimensions of the second matrix:");
[Link]("Rows: ");
int rows2 = [Link]([Link]());
[Link]("Columns: ");
int cols2 = [Link]([Link]());
int[][] matrix2 = new int[rows2][cols2];
[Link]("Enter the elements of the second matrix:");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
[Link]("Element at position [" + (i + 1) + "][" + (j + 1) + "]: ");
matrix2[i][j] = [Link]([Link]());}}
if (cols1 != rows2) {
[Link]("Matrix multiplication is not possible due to incompatible
dimensions.");
} else {
int[][] resultMatrix = multiplyMatrices(matrix1, matrix2);
[Link]("Resultant matrix after multiplication:");
printMatrix(resultMatrix);}}}
OUTPUT:-
10
Q10- WAP for matrix addition using input/output stream class.
PROGRAM:-
import [Link].*;
public class CLG_10_MatrixMultiplication {
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = [Link];
int cols1 = matrix1[0].length;
int cols2 = matrix2[0].length;
int[][] result = new int[rows1][cols2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] + matrix2[k][j];
}
}
}
return result;
}
public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int value : row) {
[Link](value + " ");}
[Link]();
}}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the dimensions of the first matrix:");
[Link]("Rows: ");
int rows1 = [Link]([Link]());
[Link]("Columns: ");
int cols1 = [Link]([Link]());
int[][] matrix1 = new int[rows1][cols1];
[Link]("Enter the elements of the first matrix:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
[Link]("Element at position [" + (i + 1) + "][" + (j + 1) + "]: ");
matrix1[i][j] = [Link]([Link]());
}}
[Link]("Enter the dimensions of the second matrix:");
[Link]("Rows: ");
int rows2 = [Link]([Link]());
[Link]("Columns: ");
int cols2 = [Link]([Link]());
int[][] matrix2 = new int[rows2][cols2];
11
[Link]("Enter the elements of the second matrix:");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
[Link]("Element at position [" + (i + 1) + "][" + (j + 1) + "]: ");
matrix2[i][j] = [Link]([Link]());
}}
if (cols1 != rows2) {
[Link]("Matrix multiplication is not possible due to incompatible
dimensions.");
} else {
int[][] resultMatrix = multiplyMatrices(matrix1, matrix2);
[Link]("Resultant matrix after multiplication:");
printMatrix(resultMatrix);
}}}
OUTPUT:-
12
Q11- WAP for matrix transposes using input/output stream class.
PROGRAM:-
import [Link].*;
public class CLG_11_MatrixTranspose {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader([Link]));
try {
[Link]("Enter the number of rows: ");
int rows = [Link]([Link]());
[Link]("Enter the number of columns: ");
int cols = [Link]([Link]());
int[][] matrix = new int[rows][cols];
[Link]("Enter the matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link]("Matrix[" + (i + 1) + "][" + (j + 1) + "]: ");
matrix[i][j] = [Link]([Link]());
}}
[Link]("\nOriginal Matrix:");
printMatrix(matrix);
int[][] transposeMatrix = transpose(matrix);
[Link]("\nTranspose Matrix:");
printMatrix(transposeMatrix);
} catch (IOException | NumberFormatException e) {
[Link]();
}}
private static int[][] transpose(int[][] matrix) {
int rows = [Link];
int cols = matrix[0].length;
int[][] transposeMatrix = new int[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
transposeMatrix[i][j] = matrix[j][i];
}}
return transposeMatrix;
}
private static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
[Link](element + "\t");
}
[Link]();
}}}
13
OUTPUT:-
14
Q12- WAP to add the elements of vector as arguments of main method(Run time) and
rearrange them and copy it into an array
PROGRAM:-
import [Link].*;
public class CLG_12_VectorToArrayExample {
public static void main(String[] args) {
Vector v1= new Vector();
[Link]("Kullu");
[Link]("Lav");
[Link]("rush");
[Link]("he");
[Link]("Sachme");
[Link]("mohan", 5);
for(int i=0;i<[Link]();i++){
[Link]([Link](i) + " ");
}
String a[]= new String[6];
[Link](a);
for(int j=0;j<6;j++){
[Link](a[j]+ " ");
}
}
}
OUTPUT:-
15
Q13- WAP that implements different methods available in vector class.
PROGRAM:-
import [Link];
public class CLG_23_Vector {
public static void main(String[] args) {
Vector v1= new Vector();
[Link]("Kullu");
[Link]("Lav");
[Link]("rush");
[Link]("he");
[Link]("Sachme");
[Link]("mohan", 5);
int size = [Link]();
[Link]("Size is: " + size);
int capacity = [Link]();
[Link]("Default capacity is: " + capacity);
String element = ([Link](1)).toString();
[Link]("Vector element is: " + element);
String firstElement = ([Link]()).toString();
[Link]("The first later of the vector is: " + firstElement);
String lastElement = ([Link]()).toString();
[Link]("The last later of the vector is: " + lastElement);
[Link]("rush");
[Link]("Elements are: " + v1);
}
}
OUTPUT:-
16
Q14- WAP to check that the given String is palindrome or not.
PROGRAM:-
import [Link];
public class CLG_13_PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
if (isPalindrome(input)) {
[Link]("The given string is a palindrome.");
[Link]("The given string is not a palindrome.");
}
}
static boolean isPalindrome(String s) {
s = [Link]("\\s", "").toLowerCase();
int length = [Link]();
for (int i = 0; i < length / 2; i++) {
if ([Link](i) != [Link](length - i - 1)) {
return false;
}
}
return true;
}
}
OUTPUT:-
17
Q15- WAP to arrange the string in alphabetical order.
PROGRAM:-
import [Link];
public class CLG_14_AlphabeticalOrder {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
String result = arrangeAlphabetically(input);
[Link]("String in alphabetical order: " + result);
}
static String arrangeAlphabetically(String s) {
char[] charArray = [Link]();
[Link](charArray);
return new String(charArray);
}
}
OUTPUT:-
18
Q16- WAP forStringBuffer class which perform the all the methods of that class.
PROGRAM:-
public class CLG_15_StringBufferExample {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("Hello");
[Link]("StringBuffer: " + stringBuffer);
[Link](" World");
[Link]("StringBuffer: " + stringBuffer);
[Link](5, " Java");
[Link]("StringBuffer: " + stringBuffer);
[Link](11, 16);
[Link]("StringBuffer: " + stringBuffer);
[Link]();
[Link]("StringBuffer: " + stringBuffer);
[Link](5);
[Link]("StringBuffer: " + stringBuffer);
[Link]("!");
[Link]("StringBuffer: " + stringBuffer);
int capacity = [Link]();
int length = [Link]();
}
}
OUTPUT:-
19
Q17- WAP to calculate Simple Interest using the Wrapper Class.
PROGRAM:-
import [Link];
public class CLG_16_SimpleInterestCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter principal amount: ");
double principal = [Link]();
[Link]("Enter rate of interest: ");
double rate = [Link]();
[Link]("Enter time in years: ");
double time = [Link]();
Double simpleInterest = (principal * rate * time) / 100;
[Link]("Principal Amount: " + principal);
[Link]("Rate of Interest: " + rate);
[Link]("Time in Years: " + time);
[Link]("Simple Interest: " + simpleInterest);
}
}
OUTPUT:-
20
Q18- WAP to calculate Area of various geometrical figures using the abstract class.
PROGRAM:-
import [Link];
abstract class Shape {
abstract double calculateArea();
}
class Circle extends Shape {
double radius;
Circle(double radius) {
[Link] = radius;
}
@Override
double calculateArea() {
return [Link] * radius * radius;
}
}
class Rectangle extends Shape {
double length;
double width;
Rectangle(double length, double width) {
[Link] = length;
[Link] = width;
}
@Override
double calculateArea() {
return length * width;
}
}
class Triangle extends Shape {
double base;
double height;
Triangle(double base, double height) {
[Link] = base;
[Link] = height;
}
@Override
double calculateArea() {
return 0.5 * base * height;
}}
21
public class CLG_17_geometrical {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Choose a shape to calculate area:");
[Link]("1. Circle");
[Link]("2. Rectangle");
[Link]("3. Triangle");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter the radius of the circle: ");
double radius = [Link]();
Circle circle = new Circle(radius);
[Link]("Area of the circle: " + [Link]());
break;
case 2:
[Link]("Enter the length of the rectangle: ");
double length = [Link]();
[Link]("Enter the width of the rectangle: ");
double width = [Link]();
Rectangle rectangle = new Rectangle(length, width);
[Link]("Area of the rectangle: " + [Link]());
break;
case 3:
[Link]("Enter the base of the triangle: ");
double base = [Link]();
[Link]("Enter the height of the triangle: ");
double height = [Link]();
Triangle triangle = new Triangle(base, height);
[Link]("Area of the triangle: " + [Link]());
break;
default:
[Link]("Invalid choice");}}}
OUTPUT:-
22
Q19- WAP where Single class implements more than one interfaces and with help of
interface reference variable user call the methods.
PROGRAM:-
interface Shape2 {
void draw();
}
interface Color {
void setColor(String color);
}
class Circle2 implements Shape2, Color {
private String color;
@Override
public void draw() {
[Link]("Drawing a circle.");
}
@Override
public void setColor(String color) {
[Link] = color;
[Link]("Setting color to " + color);
}
public void displayInfo() {
[Link]("Circle with color: " + color);
}
}
public class CLG_18_main {
public static void main(String[] args) {
Shape2 shape = new Circle2();
[Link]();
Color color = new Circle2();
[Link]("Blue");
((Circle2) color).displayInfo();
}
}
OUTPUT:-
23
Q20- WAP that use the multiple catch statements within the try-catch mechanism.
PROGRAM:-
import [Link];
public class CLG_19_MultipleCatchExample {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
try {
[Link]("Enter a number: ");
int numerator = [Link]();
[Link]("Enter another number: ");
int denominator = [Link]();
int result = numerator / denominator;
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("Error: " + [Link]());
} catch ([Link] e) {
[Link]("Error: Invalid input. " + [Link]());
} catch (Exception e) {
[Link]("An unexpected error occurred: " + [Link]());
}
[Link]("Program continues after the try-catch block.");
}
}
OUTPUT:-
24
Q21- WAP where user will create a self-Exception using the “throw” keyword.
PROGRAM:-
import [Link];
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class CLG_20_CustomExceptionExample {
public static void main(String[] args) {
Scanner sc= new Scanner([Link]);
[Link]("Enter a age: ");
try {
int age = [Link]();
if (age < 0) {
throw new CustomException("Age cannot be negative");
} else {
[Link]("Age is: " + age);
}
} catch (CustomException e) {
[Link]("Custom Exception: " + [Link]());
}
}
}
OUTPUT:-
25
Q22- WAP for multithread using the isAlive() and synchronized() methods of Thread
class
PROGRAM:-
package clg;
class Ab extends Thread {
public synchronized void run() {
[Link]("Thread A=");
for (int i = 0; i <= 5; i++) {
[Link]("Thread one is running A=" + i);}}}
class Bb extends Thread {
public synchronized void run() {
[Link]("Thread B=");
for (int j = 0; j <= 5; j++) {
[Link]("Thread two is running B=" + j);}}}
public class CLG_21_MyThread {
public static void main(String arg[]) {
Ab a1 = new Ab();
[Link]();
Bb b1 = new Bb();
[Link]();
if ([Link]()) {
[Link]("Thread one is alive");
} else {
[Link]("Thread one is not alive");}}}
OUTPUT:-
26
Q23- WAP for multithreading using the suspend(), resume(), sleep(t) method of Thread
class.
PROGRAM:-9i
class MyThread extends Thread {
private boolean suspended = false;
public synchronized void mySuspend() {
suspended = true;
}
public synchronized void myResume() {
suspended = false;
notify();
}
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
[Link]("Count: " + i);
// Check if suspended
synchronized (this) {
while (suspended) {
wait();
}
}
[Link](1000);
}
} catch (InterruptedException e) {
[Link]("Thread interrupted");
}
}
}
public class CLG_24_sleep {
public static void main(String[] args) {
MyThread myThread = new MyThread();
[Link]();
try {
[Link](2000);
[Link]();
[Link]("Thread suspended for 2 seconds");
[Link](2000);
27
[Link]();
[Link]("Thread resumed");
} catch (InterruptedException e) {
[Link]("Main thread interrupted");
}
try {
[Link]();
} catch (InterruptedException e) {
[Link]("Main thread interrupted");
}
[Link]("Main thread exiting");
}
}
OUTPUT:-
28
Q24- WAP to create a package and one package will import another package.
PROGRAM:-
1) First package program.
package newpack;
public class inputclass {
public void fun(){
[Link]("hi it's me mariyo");
}
}
2) Second package program.
package new2pack;
import newpack.*;
public class output {
public static void main(String[] args) {
inputclass i= new inputclass();
[Link]();
}
}
OUTPUT:-
29
Q25- WAP for JDBC to display the records from the existing table.
STEPS:-
1. Create a table in MS Access.
2. Save the table in a folder.
3. Open control penal in windows.
4. Open Administrative Tools.
5. Open ODBC Data Sources.
6. Add your database.
7. Run the program.
PROGRAM:-
import [Link].*;
public class CLG_22_conne {
public static void main(String[] args) {
try{
[Link]("[Link]");
Connection con = [Link]("jdbc:odbc:db");
Statement s1 = [Link]();
ResultSet r = [Link]("select * from student");
while([Link]()) {
[Link]([Link](1) + " " + [Link](2) + " " + [Link](3));
}
}
catch(Exception e){
[Link](e);
}
}
}
OUTPUT:-
30
Q26- WAP for demonstration of switch statement, continue and break.
PROGRAM:-
import [Link];
public class CLG_26_Breck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Choose an option:");
[Link]("1. Print Hello");
[Link]("2. Print World");
[Link]("3. Print Hello World");
[Link]("4. Exit");
while (true) {
[Link]("Enter your choice (1-4): ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Hello");
break;
case 2:
[Link]("World");
break;
case 3:
[Link]("Hello World");
continue;
case 4:
[Link]("Exiting the program.");
[Link]();
[Link](0);
default:
[Link]("Invalid choice. Please enter a number between 1 and 4.");
}
[Link]("This is after the switch statement.");
}}}
OUTPUT:-
31
Q27- WAP a program to read and write on file using File Reader and file Writer class.
PROGRAM:-
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class CLG_27_readwrite {
public static void main(String[] args) {
String inputFile = "[Link]";
String outputFile = "[Link]";
writeToFile(outputFile, "Hello, this is a sample text.\n");
String content = readFromFile(inputFile);
[Link]("Content read from file:\n" + content);
}
private static void writeToFile(String filePath, String content) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
[Link](content);
[Link]("Content written to file successfully.");
} catch (IOException e) {
[Link]("Error writing to file: " + [Link]());
}}
private static String readFromFile(String filePath) {
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = [Link]()) != null) {
[Link](line).append("\n");
}} catch (IOException e) {
[Link]("Error reading from file: " + [Link]());
}
return [Link]();
}}
OUTPUT:-
32