Java
Java
Java
PROGRAM:-
public class CLG_01_Encapsulation {
public void printName() {
private String name;
name = maya;
System.out.println("Name: " + name);
}
public static void main(String[] args) {
CLG_01_Encapsulation namePrinter = new CLG_01_Encapsulation();
namePrinter.printName();
}
}
OUTPUT:-
1
Q2- WAP to demonstrate concept of function overloading of Polymorphism
PROGRAM:-
public class CLG_02_overloading {
OUTPUT:-
2
Q3- WAP to demonstrate concept of function overloading of Polymorphism
PROGRAM:-
public class CLG_03_ConstructorOverloadingExample {
public CLG_03_ConstructorOverloadingExample() {
this.value = 0;
}
OUTPUT:-
3
Q4- WAP to use Boolean data type and print the Prime Number series up to 50.
PROGRAM:-
public class CLG_04_PrimeNumberSeries {
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 {
do {
System.out.print(first + " ");
count++;
} while (count < n);
}
}
OUTPUT:-
5
Q6- WAP to check the given number is Armstrong or not.
PROGRAM:-
import java.util.Scanner;
originalNumber = number;
while (originalNumber != 0) {
originalNumber /= 10;
++n;
}
originalNumber = number;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += Math.pow(remainder, n);
originalNumber /= 10;
}
return result == number;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (isArmstrong(number)) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
scanner.close();
}
}
OUTPUT:-
6
Q7- WAP to find the factorial of any given number.
PROGRAM:-
import java.util.Scanner;
if (result == -1) {
System.out.println("Factorial is not defined for negative numbers.");
} else {
System.out.println("Factorial of " + number + " is: " + result);
}
scanner.close();
}
}
OUTPUT:-
7
Q8- WAP to sort the elements of One Dimensional Array in Ascending order.
PROGRAM:-
public class CLG_08_ArraySorting {
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);
}
sortArrayAscending(numbers);
OUTPUT:-
8
Q9- WAP for matrix multiplication using input/output stream.
PROGRAM:-
import java.io.*;
public class CLG_09_MatrixMultiplication {
return result;
}
10
Q10- WAP for matrix addition using input/output stream class.
PROGRAM:-
import java.io.*;
return result;
}
OUTPUT:-
12
Q11- WAP for matrix transposes using input/output stream class.
PROGRAM:-
import java.io.*;
public class CLG_11_MatrixTranspose {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter the number of rows: ");
int rows = Integer.parseInt(reader.readLine());
System.out.print("Enter the number of columns: ");
int cols = Integer.parseInt(reader.readLine());
int[][] matrix = new int[rows][cols];
System.out.println("Enter the matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Matrix[" + (i + 1) + "][" + (j + 1) + "]: ");
matrix[i][j] = Integer.parseInt(reader.readLine());
}}
System.out.println("\nOriginal Matrix:");
printMatrix(matrix);
int[][] transposeMatrix = transpose(matrix);
System.out.println("\nTranspose Matrix:");
printMatrix(transposeMatrix);
} catch (IOException | NumberFormatException e) {
e.printStackTrace();
}}
private static int[][] transpose(int[][] matrix) {
int rows = matrix.length;
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;
}
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 java.util.*;
OUTPUT:-
15
Q13- WAP that implements different methods available in vector class.
PROGRAM:-
import java.util.Vector;
v1.remove("rush");
OUTPUT:-
16
Q14- WAP to check that the given String is palindrome or not.
PROGRAM:-
import java.util.Scanner;
if (isPalindrome(input)) {
System.out.println("The given string is a palindrome.");
System.out.println("The given string is not a palindrome.");
}
}
OUTPUT:-
17
Q15- WAP to arrange the string in alphabetical order.
PROGRAM:-
import java.util.Scanner;
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");
System.out.println("StringBuffer: " + stringBuffer);
stringBuffer.append(" World");
System.out.println("StringBuffer: " + stringBuffer);
stringBuffer.delete(11, 16);
System.out.println("StringBuffer: " + stringBuffer);
stringBuffer.reverse();
System.out.println("StringBuffer: " + stringBuffer);
stringBuffer.setLength(5);
System.out.println("StringBuffer: " + stringBuffer);
stringBuffer.append("!");
System.out.println("StringBuffer: " + stringBuffer);
}
}
OUTPUT:-
19
Q17- WAP to calculate Simple Interest using the Wrapper Class.
PROGRAM:-
import java.util.Scanner;
OUTPUT:-
20
Q18- WAP to calculate Area of various geometrical figures using the abstract class.
PROGRAM:-
import java.util.Scanner;
Circle(double radius) {
this.radius = radius;
}
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
}
@Override
double calculateArea() {
return length * width;
}
}
@Override
double calculateArea() {
return 0.5 * base * height;
}}
21
public class CLG_17_geometrical {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose a shape to calculate area:");
System.out.println("1. Circle");
System.out.println("2. Rectangle");
System.out.println("3. Triangle");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
Circle circle = new Circle(radius);
System.out.println("Area of the circle: " + circle.calculateArea());
break;
case 2:
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();
Rectangle rectangle = new Rectangle(length, width);
System.out.println("Area of the rectangle: " + rectangle.calculateArea());
break;
case 3:
System.out.print("Enter the base of the triangle: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = scanner.nextDouble();
Triangle triangle = new Triangle(base, height);
System.out.println("Area of the triangle: " + triangle.calculateArea());
break;
default:
System.out.println("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);
}
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
@Override
public void setColor(String color) {
this.color = color;
System.out.println("Setting color to " + color);
}
((Circle2) color).displayInfo();
}
}
OUTPUT:-
23
Q20- WAP that use the multiple catch statements within the try-catch mechanism.
PROGRAM:-
import java.util.Scanner;
try {
System.out.print("Enter a number: ");
int numerator = scanner.nextInt();
OUTPUT:-
24
Q21- WAP where user will create a self-Exception using the “throw” keyword.
PROGRAM:-
import java.util.Scanner;
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() {
System.out.println("Thread A=");
for (int i = 0; i <= 5; i++) {
System.out.println("Thread one is running A=" + i);}}}
class Bb extends Thread {
public synchronized void run() {
System.out.println("Thread B=");
for (int j = 0; j <= 5; j++) {
System.out.println("Thread two is running B=" + j);}}}
public class CLG_21_MyThread {
public static void main(String arg[]) {
Ab a1 = new Ab();
a1.start();
Bb b1 = new Bb();
b1.start();
if (a1.isAlive()) {
System.out.println("Thread one is alive");
} else {
System.out.println("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;
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
// Check if suspended
synchronized (this) {
while (suspended) {
wait();
}
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.err.println("Thread interrupted");
}
}
}
try {
Thread.sleep(2000);
myThread.mySuspend();
System.out.println("Thread suspended for 2 seconds");
Thread.sleep(2000);
27
myThread.myResume();
System.out.println("Thread resumed");
} catch (InterruptedException e) {
System.err.println("Main thread interrupted");
}
try {
myThread.join();
} catch (InterruptedException e) {
System.err.println("Main thread interrupted");
}
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(){
System.out.println("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();
i.fun();
}
}
OUTPUT:-
29
Q25- WAP for JDBC to display the records from the existing table.
STEPS:-
OUTPUT:-
30
Q26- WAP for demonstration of switch statement, continue and break.
PROGRAM:-
import java.util.Scanner;
public class CLG_26_Breck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose an option:");
System.out.println("1. Print Hello");
System.out.println("2. Print World");
System.out.println("3. Print Hello World");
System.out.println("4. Exit");
while (true) {
System.out.print("Enter your choice (1-4): ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Hello");
break;
case 2:
System.out.println("World");
break;
case 3:
System.out.println("Hello World");
continue;
case 4:
System.out.println("Exiting the program.");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a number between 1 and 4.");
}
System.out.println("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 java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
public class CLG_27_readwrite {
public static void main(String[] args) {
String inputFile = "input.txt";
String outputFile = "output.txt";
writeToFile(outputFile, "Hello, this is a sample text.\n");
String content = readFromFile(inputFile);
System.out.println("Content read from file:\n" + content);
}
32