1a.
Sum of 2D array
Program:
import [Link];
class matrix {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int[][] arr = new int[3][3];
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
[Link]("enter for "+i+" "+j+": ");
arr[i][j] = [Link]();
}
}
int sum = 0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
sum += arr[i][j];
}
}
[Link]("Sum of matrix elements = " + sum);
[Link]("Matrix is:");
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
[Link](arr[i][j] + " ");
}
[Link]();
}
}
}
Output:
enter for 0 0: 1
enter for 0 1: 2
enter for 0 2: 3
enter for 1 0: 4
enter for 1 1: 5
enter for 1 2: 6
enter for 2 0: 7
enter for 2 1: 8
enter for 2 2: 9
Sum of matrix elements = 45
Result:
[Link] of matrix
Program:
import [Link];
class tran {
public static void main(String args[])
{ Scanner sc = new Scanner([Link]); int[]
[] arr = new int[3][3];
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
[Link]("enter for " + i + " " + j); arr[i][j] =
[Link]();
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
[Link](arr[j][i] + " "); }
[Link]();
}
}
}
Output:
enter for 0 0: 1
enter for 0 1: 2
enter for 0 2: 3
enter for 1 0: 4
enter for 1 1: 5
enter for 1 2: 6
enter for 2 0: 7
enter for 2 1: 8
enter for 2 2: 9
123
456
789
147
258
369
Result:
[Link] of 2 matrix
Program:
import [Link];
class sumof2{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
int[][] arr1=new int[3][3];
int[][] arr2=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link]("enter for "+i+" "+j);
arr1[i][j]=[Link]();
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link]("enter for "+i+" "+j);
arr2[i][j]=[Link]();
}
}
int[][] sum=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
sum[i][j]=arr1[i][j]+arr2[i][j];
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link](sum[j][i]+" "); // <--- Notice indices swapped here }
[Link]();
}
}
}
Output:
123
456
789
987
654
321
10 10 10
10 10 10
10 10 10
Result:
[Link] of Number
Program:
import [Link];
class fact{
public static void main(String args[])
{ Scanner sc=new Scanner([Link]);
[Link]("enter n:"); int num =
[Link]();
int fact = 1;
for (int i = 1; i <= num; i++)
{ fact *= i;
}
[Link](fact);
}
}
Output:
enter n:5
120
Result:
[Link] Search
Program:
import [Link];
class linear {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("enter no of elements:"); int
num = [Link]();
int[] arr = new int[num];
for(int i = 0; i < [Link]; i++) {
[Link]("enter for " + i + " "); arr[i]
= [Link]();
}
int found = 0;
[Link]("enter the element to search:"); int n =
[Link]();
for(int i = 0; i < [Link]; i++) {
if(arr[i] == n) {
found = 1;
[Link]("element found at: " + (i + 1)); break;
}
}
if(found == 0) {
[Link]("element not found"); }
}
}
Output:
enter no of elements:5
enter for 0 10
enter for 1 20
enter for 2 30
enter for 3 40
enter for 4 50
enter the element to search:
30
Result:
[Link] Series:
Program:
import [Link];
class fibo {
public static void main(String args[])
{ Scanner sc = new Scanner([Link]);
[Link]("enter n:");
int num = [Link]();
int a = 0, b = 1, temp;
for (int i = 0; i <= num; i++) {
[Link](a + " "); temp
= a;
a = a + b;
b = temp;
}
}
}
Output:
enter n:5
0
1
1
2
3
5
Result:
[Link]
Program:
import [Link];
class sort {
public static void main(String args[])
{ Scanner sc = new Scanner([Link]);
[Link]("enter the size:"); int n =
[Link]();
int[] arr = new int[n];
for (int i = 0; i < [Link]; i++) {
[Link]("enter for " + i + " :"); arr[i]
= [Link]();
}
for (int i = 0; i < [Link]; i++) {
for (int j = i; j < n; j++) {
if (arr[j] < arr[i]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
[Link]("the sorted elements are"); for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}
}
Output:
enter the size:5
enter for 0 :5
enter for 1 :1
enter for 2 :4
enter for 3 :2
enter for 4 :3
the sorted elements are
12345
Result:
2.a
Program:
import [Link];
public class Stud {
private String stud_name;
private int roll_num;
private float tot_marks;
public void getStud(String s_name, int r_num)
{ stud_name = s_name;
roll_num = r_num;
}
public void tot_mark(float t_mark)
{ tot_marks = t_mark;
}
public void dispStud() {
[Link]("Name: " + stud_name);
[Link]("Roll Num: " + roll_num);
[Link]("Tot Mark: " + tot_marks); }
public static void main(String[] args)
{ Stud s1 = new Stud();
[Link]("Nitesh", 11);
s1.tot_mark(90);
[Link]();
}
}
Output:
Name: Nitesh
Roll Num: 11
Tot Mark: 90.0
Result:
2.b
Program:
import [Link];
public class Stock {
private String product_name;
private String productID;
private int productNumber;
private int stock;
private double price;
public Stock(String prodName, String prodID, int stock)
{ this.product_name = prodName;
[Link] = prodID;
[Link] = stock;
}
public void price(double double_price) {
if (double_price > 0) {
[Link] = double_price;
} else {
[Link]("Price can't be negative."); }
}
public void purchase(int quantity) {
if (quantity > 0) {
if (stock >= quantity) {
stock = stock - quantity;
[Link]("Purchased " + quantity + " units."); } else {
[Link]("Not enough stock available."); }
} else {
[Link]("Invalid quantity.");
}
}
public void disp() {
[Link]("Product ID: " + productID);
[Link]("Product Name: " + product_name);
[Link]("Stock: " + stock);
[Link]("Price: " + price);
}
public static void main(String[] args) { Stock
s1 = new Stock("Mad", "Air", 900);
[Link](40.50);
[Link](10);
[Link]();
}
}
Output:
Purchased 10 units.
Product ID: Air
Product Name: Mac
Stock: 890
Price: 40.5
Result:
2.c
Program:
import [Link];
public class Bank {
private int acc_Num;
private String acc_Name;
private float acc_Bal;
public Bank(int accNum, String accName) { this.acc_Num = accNum;
this.acc_Name = accName;
}
public void deposit(float amt) {
acc_Bal = acc_Bal + amt;
[Link]("Amount credited: " + amt);
[Link]("Current Balance: " + acc_Bal); }
public void withdraw(float amt) {
if (acc_Bal >= amt) {
acc_Bal = acc_Bal - amt;
[Link]("Amount debited: " + amt);
[Link]("Current Balance: " + acc_Bal); } else {
[Link]("No money left to withdraw."); }
}
public void disp() {
[Link]("Name: " + acc_Name);
[Link]("Acc Number: " + acc_Num);
[Link]("Balance: " + acc_Bal); }
public static void main(String[] args) {
Bank b1 = new Bank(511904033, "Nitesh");
[Link](1000.0f);
[Link](500.0f);
[Link]();
}
}
Output:
Amount credited: 1000.0
Current Balance: 1000.0
Amount debited: 500.0
Current Balance: 500.0
Name: Nitesh
Acc Number: 511904033
Balance: 500.0
Result:
3.a
Program:
import [Link];
class Employee {
String emp_name;
String emp_id;
String address;
String mail_id;
String mobile_no;
public Employee(String name, String id, String addr, String mail, String mobile)
{ this.emp_name = name;
this.emp_id = id;
[Link] = addr;
this.mail_id = mail;
this.mobile_no = mobile;
}
}
class Programmer extends Employee {
double bp, da, hra, pf, fund, net, gross;
public Programmer(String name, String id, String addr, String mail, String mobile, double basicPay) {
super(name, id, addr, mail, mobile);
[Link] = basicPay;
}
public void calc() {
da = 0.97 * bp;
hra = 0.10 * bp;
pf = 0.12 * bp;
fund = 0.001 * bp;
gross = bp + da + hra;
net = gross - pf - fund;
display();
}
private void display() {
[Link]("\nProgrammer Payslip");
[Link]("------------------");
[Link]("Name: " + emp_name);
[Link]("ID: " + emp_id);
[Link]("Address: " + address);
[Link]("Mail ID: " + mail_id);
[Link]("Mobile: " + mobile_no);
[Link]("Gross Salary: " + gross);
[Link]("Net Salary: " + net);
}
}
class Assistant_Professor extends Employee {
double bp, da, hra, pf, fund, net, gross;
public Assistant_Professor(String name, String id, String addr, String mail, String mobile, double
basicPay) {
super(name, id, addr, mail, mobile);
[Link] = basicPay;
}
public void calc() {
da = 0.95 * bp;
hra = 0.15 * bp;
pf = 0.12 * bp;
fund = 0.001 * bp;
gross = bp + da + hra;
net = gross - pf - fund;
display();
}
private void display() {
[Link]("\nAssistant Professor Payslip");
[Link]("---------------------------");
[Link]("Name: " + emp_name);
[Link]("ID: " + emp_id);
[Link]("Address: " + address);
[Link]("Mail ID: " + mail_id);
[Link]("Mobile: " + mobile_no);
[Link]("Gross Salary: " + gross);
[Link]("Net Salary: " + net);
}
}
class Associate_Professor extends Employee {
double bp, da, hra, pf, fund, net, gross;
public Associate_Professor(String name, String id, String addr, String mail, String mobile, double
basicPay) {
super(name, id, addr, mail, mobile);
[Link] = basicPay;
}
public void calc() {
da = 0.98 * bp;
hra = 0.20 * bp;
pf = 0.12 * bp;
fund = 0.001 * bp;
gross = bp + da + hra;
net = gross - pf - fund;
display();
}
private void display() {
[Link]("\nAssociate Professor Payslip");
[Link]("---------------------------");
[Link]("Name: " + emp_name);
[Link]("ID: " + emp_id);
[Link]("Address: " + address);
[Link]("Mail ID: " + mail_id);
[Link]("Mobile: " + mobile_no);
[Link]("Gross Salary: " + gross);
[Link]("Net Salary: " + net);
}
}
class Professor extends Employee {
double bp, da, hra, pf, fund, net, gross;
public Professor(String name, String id, String addr, String mail, String mobile, double basicPay)
{ super(name, id, addr, mail, mobile);
[Link] = basicPay;
}
public void calc() {
da = 0.99 * bp;
hra = 0.25 * bp;
pf = 0.12 * bp;
fund = 0.001 * bp;
gross = bp + da + hra;
net = gross - pf - fund;
display();
}
private void display() {
[Link]("\nProfessor Payslip");
[Link]("-----------------");
[Link]("Name: " + emp_name);
[Link]("ID: " + emp_id);
[Link]("Address: " + address);
[Link]("Mail ID: " + mail_id);
[Link]("Mobile: " + mobile_no);
[Link]("Gross Salary: " + gross);
[Link]("Net Salary: " + net);
}
}
public class DemoEmp {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
double[] basic = new double[4];
[Link]("Enter basic pay for each employee type:");
[Link]("Programmer: ");
basic[0] = [Link]();
[Link]("Assistant Professor: ");
basic[1] = [Link]();
[Link]("Associate Professor: ");
basic[2] = [Link]();
[Link]("Professor: ");
basic[3] = [Link]();
Programmer programmer = new Programmer("John Doe", "P001", "123 Tech St",
"john@[Link]", "9876543210", basic[0]);
Assistant_Professor asstProf = new Assistant_Professor("Jane Smith", "AP001", "456 College Ave",
"jane@[Link]", "8765432109", basic[1]);
Associate_Professor assocProf = new Associate_Professor("Robert Johnson", "APR001", "789 University
Blvd", "robert@[Link]", "7654321098", basic[2]);
Professor professor = new Professor("Emily Davis", "PR001", "321 Campus Dr",
"emily@[Link]", "6543210987", basic[3]);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Output:
Enter basic pay for each employee type:
Programmer: 30000
Assistant Professor: 40000
Associate Professor: 50000
Professor: 60000
Programmer Payslip
------------------
Name: John Doe
ID: P001
Address: 123 Tech St
Mail ID: john@[Link]
Mobile: 9876543210
Gross Salary: 62100.0
Net Salary: 58860.0
Assistant Professor Payslip
--------------------------
Name: Jane Smith
ID: AP001
Address: 456 College Ave
Mail ID: jane@[Link]
Mobile: 8765432109
Gross Salary: 60000.0
Net Salary: 55200.0
Result:
3.b
Program:
class Parent {
public void showMessage() {
[Link]("This is the Parent class message."); }
}
class Child extends Parent {
@Override
public void showMessage() {
[Link]("This is the Child class message."); }
}
public class MethodOverridingSimple {
public static void main(String[] args) {
Parent p1 = new Parent();
[Link]();
Child c1 = new Child();
[Link]();
[Link]("\n--- Runtime Polymorphism ---");
Parent p2 = new Child();
[Link]();
}
}
Output:
This is the Parent class message.
This is the Child class message.
--- Runtime Polymorphism ---
This is the Child class message.
Result:
4a)
PROGRAM:
import [Link];
abstract class shape {
abstract void print_area();
}
class rectangle extends shape {
void print_area() {
Scanner sc = new Scanner([Link]); [Link]("enter
the value for l"); int l = [Link]();
[Link]("enter the value for b"); int b = [Link]();
[Link]("area is: " + (l * b)); }
}
class triangle extends shape {
void print_area() {
Scanner sc = new Scanner([Link]); [Link]("enter
the value for b"); int b = [Link]();
[Link]("enter the value for h"); int h = [Link]();
[Link]("area is: " + (0.5 * b * h)); }
}
class circle extends shape {
void print_area() {
Scanner sc = new Scanner([Link]); [Link]("enter
the value for r"); int r = [Link]();
[Link]("area is: " + (3.14 * r * r)); }
}
class demo2 {
public static void main(String args[]) { rectangle r = new
rectangle(); r.print_area();
triangle t = new triangle(); t.print_area();
circle c = new circle();
c.print_area();
}
}
OUTPUT:
enter the value for l
5
enter the value for b
4
area is: 20
enter the value for b
6
enter the value for h
3
area is: 9.0
enter the value for r
7
area is: 153.86
Result:
4B)
import [Link];
abstract class Employee {
abstract void calculate_bonus();
}
class Manager extends Employee {
void calculate_bonus() {
Scanner sc = new Scanner([Link]);
[Link]("Enter name: ");
String name = [Link]();
[Link]("Enter HRA: ");
int hra = [Link]();
[Link]("Enter TA: ");
int ta = [Link]();
[Link]("Enter bonus: ");
int bonus = [Link]();
[Link]("Total salary for Manager " + name + " is: " + (hra + ta + bonus)); }
}
class Developer extends Employee {
void calculate_bonus() {
Scanner sc = new Scanner([Link]);
[Link]("Enter name: ");
String name = [Link]();
[Link]("Enter HRA: ");
int hra = [Link]();
[Link]("Enter TA: ");
int ta = [Link]();
[Link]("Enter bonus: ");
int bonus = [Link]();
[Link]("Total salary for Developer " + name + " is: " + (hra + ta + bonus)); }
}
class Intern extends Employee {
void calculate_bonus() {
Scanner sc = new Scanner([Link]);
[Link]("Enter name: ");
String name = [Link]();
[Link]("Enter HRA: ");
int hra = [Link]();
[Link]("Enter TA: ");
int ta = [Link]();
[Link]("Enter bonus: ");
int bonus = [Link]();
[Link]("Total salary for Intern " + name + " is: " + (hra + ta + bonus)); }
}
class Demo4 {
public static void main(String args[]) {
Manager m = new Manager();
Developer d = new Developer();
Intern i = new Intern();
m.calculate_bonus();
d.calculate_bonus();
i.calculate_bonus();
}
}
OUTPUT:
Enter name: Alice
Enter HRA: 5000
Enter TA: 2000
Enter bonus: 1000
Total salary for Manager Alice is: 8000
Enter name: Bob
Enter HRA: 4000
Enter TA: 1500
Enter bonus: 800
Total salary for Developer Bob is: 6300
Enter name: Carol
Enter HRA: 3000
Enter TA: 1000
Enter bonus: 500
Total salary for Intern Carol is: 4500
RESULT:
5a)
import [Link];
interface shape {
void print_area();
}
class rectangle implements shape {
public void print_area() {
Scanner sc = new Scanner([Link]); [Link]("enter
the value for l"); int l = [Link]();
[Link]("enter the value for b"); int b = [Link]();
[Link]("area is: " + (l * b)); }
}
class triangle implements shape {
public void print_area() {
Scanner sc = new Scanner([Link]); [Link]("enter
the value for b"); int b = [Link]();
[Link]("enter the value for h"); int h = [Link]();
[Link]("area is: " + (0.5 * b * h)); }
}
class circle implements shape {
public void print_area() {
Scanner sc = new Scanner([Link]); [Link]("enter
the value for r"); int r = [Link]();
[Link]("area is: " + (3.14 * r * r)); }
}
class demo {
public static void main(String args[]) { rectangle r = new
rectangle(); r.print_area();
triangle t = new triangle(); t.print_area();
circle c = new circle();
c.print_area();
}
}
OUTPUT:
enter the value for l
5
enter the value for b
4
area is: 20
enter the value for b
6
enter the value for h
3
area is: 9.0
enter the value for r
7
area is: 153.86
RESULT:
5b)
interface playable {
void play();
}
interface recordable {
void record();
}
class mediaplayer implements playable, recordable { public void
play() {
[Link]("video is played"); }
public void record() {
[Link]("audio is recorded"); }
}
class demo3 {
public static void main(String args[]) { mediaplayer m =
new mediaplayer(); [Link]();
[Link]();
}
}
OUTPUT:
video is played
audio is recorded
RESULT:
6a)
PROGRAM:
import [Link];
public class ArrayAccess {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of elements in the array");
int size = [Link]();
int[] array = new int[size];
for (int i = 0; i < size; i++) {
[Link]("Enter element " + (i + 1));
array[i] = [Link]();
}
[Link]("Array:");
for (int i = 0; i < size; i++) {
[Link](array[i] + " ");
}
[Link](); // for new line
try {
[Link]("Enter the index of the array element you want to access:");
int index = [Link]();
[Link]("The array element at index " + index + " is " + array[index]);
[Link]("The array element successfully accessed");
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index is out of range!");
}
[Link]();
}
}
Output:
Enter the number of elements in the array
5
Enter element 1
7
Enter element 2
6
Enter element 3
5
Enter element 4
4
Enter element 5
3
Array:
76543
Enter the index of the array element you want to access:
2
The array element at index 2 is 5
The array element successfully accessed
RESULT:
6b)
PROGRAM:
import [Link];
public class ArrayIndexHandling {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
try {
[Link]("Enter the number of elements:");
int size = [Link]([Link]());
int[] array = new int[size];
[Link]("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
array[i] = [Link]([Link]());
}
[Link]("Enter the index of array element you want to access:");
String input = [Link]();
int index = [Link](input);
[Link]("The element at index " + index + " = " + array[index]);
} catch (ArrayIndexOutOfBoundsException e) {
[Link](e);
} catch (NumberFormatException e) {
[Link](e);
}
[Link]();
}
}
Output:
Enter the number of elements:
3
Enter the elements of the array:
1
2
3
Enter the index of array element you want to access:
0
The element at index 0 = 1
RESULT:
6c)
PROGRAM:
import [Link];
public class HandleNumberFormat {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter an integer:");
try {
String input = [Link]();
int number = [Link](input);
[Link]("You entered: " + number);
}
catch (NumberFormatException e) {
[Link]("Invalid input! Please enter a valid integer.");
[Link](e);
}
[Link]();
}
}
Output:
Enter the number of elements:
3
Enter the elements of the array:
1
2
3
Enter the index of array element you want to access:
0
The element at index 0 = 1
RESULT:
6d)
PROGRAM:
import [Link];
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
public class AgeValidation {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter your age:");
try {
int age = [Link]();
if (age < 0) {
throw new InvalidAgeException("Age cannot be negative!");
}
[Link]("Your age is: " + age);
}
catch (InvalidAgeException e) {
[Link]("Exception caught: " + [Link]());
}
catch (Exception e) {
[Link]("Invalid input! Please enter a valid number.");
[Link]();
}
}
Output:
Enter an integer:
25
You entered: 2
RESULT:
7a)
PROGRAM:
public static String reverse(String str) {
if ([Link]()) {
return str;
}
return reverse([Link](1)) + [Link](0);
}
public static void main(String[] args) {
String original = "Hello, World!";
String reversed = reverse(original);
[Link](reversed);
}
Output:
dlroW ,olleH
RESULT:
7b.
PROGRAM:
public class PalindromeChecker {
public static boolean isPalindrome(String str) {
String cleanedStr = [Link]("\\s+", "").toLowerCase();
String reversedStr = new StringBuilder(cleanedStr).reverse().toString();
return [Link](reversedStr);
}
public static void main(String[] args) {
String testString = "A man a plan a canal Panama";
[Link]("Is the string a palindrome? " + isPalindrome(testString));
}
}
Output:
Is the string a palindrome?
True
RESULT:
7c)
PROGRAM:
public class CharacterCounter {
public static void main(String[] args) {
String input = "Hello World 123!";
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
for (char ch : [Link]()) {
if ([Link](ch)) {
spaces++;
} else if ([Link](ch)) {
digits++;
} else if (isVowel(ch)) {
vowels++;
} else if ([Link](ch)) {
consonants++;
}
}
[Link]("Vowels: " + vowels);
[Link]("Consonants: " + consonants);
[Link]("Digits: " + digits);
[Link]("Spaces: " + spaces);
}
private static boolean isVowel(char ch) {
return "AEIOUaeiou".indexOf(ch) != -1;
}
}
Output:
Vowels: 3
Consonants: 7
Digits: 3
Spaces: 2
RESULT:
7d.
PROGRAM:
import [Link];
public class CharacterFrequency {
public static void main(String[] args) {
String input = "hello world";
HashMap<Character, Integer> frequencyMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
for (char key : [Link]()) {
[Link](key + ": " + [Link](key));
}
}
}
Output:
h:1
e:1
l:3
o:2
w:1
r:1
d:1
RESULT:
8.
PROGRAM:
import [Link].*;
import [Link].*;
public class FileOperations {
public static void readAndWriteFile(String filePath, String content) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
[Link](content);
}
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
}
}
public static int countCharacterOccurrences(String filePath, char character) throws
IOException {
int count = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
int ch;
while ((ch = [Link]()) != -1) {
if (ch == character) {
count++;
}
}
}
return count;
}
public static void copyFile(String sourcePath, String destinationPath) throws IOException
{
[Link]([Link](sourcePath), [Link](destinationPath),
StandardCopyOption.REPLACE_EXISTING);
}
public static void main(String[] args) {
try {
String filePath = "[Link]";
readAndWriteFile(filePath, "Hello, World!");
[Link]("Character 'o' appears: " + countCharacterOccurrences(filePath,
'o') + " times.");
copyFile(filePath, "copy_example.txt");
} catch (IOException e) {
[Link]();
}
}
}
Output:
Enter filename to write: [Link]
Enter text to write into the file (type 'exit' to finish):
Hello World
Java Programming
exit
Data written successfully to [Link]
Reading content from the file:
Hello World
Java Programming
Enter the character to count: a
Character 'a' appears 4 times in the file.
Enter destination filename to copy content: [Link]
Content copied successfully to [Link]
RESULT:
9a.
PROGRAM:
public class GenericSearch<T> {
private T[] array;
public GenericSearch(T[] array) {
[Link] = array;
}
public boolean contains(T element) {
for (T item : array) {
if ([Link](element)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
GenericSearch<Integer> intSearch = new GenericSearch<>(intArray);
[Link]("Contains 3? " + [Link](3)); // Output: true
String[] strArray = {"apple", "banana", "cherry"};
GenericSearch<String> strSearch = new GenericSearch<>(strArray);
[Link]("Contains 'banana'? " + [Link]("banana"));
}
}
Output:
Enter an integer to search: 30
30 is found in the array.
Enter a string to search: banana
banana is found in the array.
RESULT:
9b.
PROGRAM:
public class AverageCalculator<T extends Number> {
private T[] numbers;
public AverageCalculator(T[] numbers) {
[Link] = numbers;
}
public double calculateAverage() {
double sum = 0.0;
for (T number : numbers) {
sum += [Link]();
}
return sum / [Link];
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
AverageCalculator<Integer> intCalculator = new AverageCalculator<>(intArray);
[Link]("Average of Integer array: " + [Link]());
Double[] doubleArray = {1.5, 2.5, 3.5};
AverageCalculator<Double> doubleCalculator = new
AverageCalculator<>(doubleArray);
[Link]("Average of Double array: " + [Link]());
}
}
Output:
Average of Integer array: 30.0
Average of Double array: 10.5
RESULT:
9c.
PROGRAM:
public class ArrayUtils {
public static <T> void swap(T[] arr, int i, int j) {
if (arr == null || i < 0 || j < 0 || i >= [Link] || j >= [Link]) {
throw new IllegalArgumentException("Invalid index or null array");
}
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
String[] stringArray = {"A", "B", "C", "D"};
[Link]("Before swap: " + [Link](stringArray));
swap(stringArray, 1, 3);
[Link]("After swap: " + [Link](stringArray));
}
}
Output:
Before swap:[1,2,3,4,5]
After swap:[1,4,3,2,5]
Before swap:[Apple,Banana,cherry]
After swap:[Cherry,Banana,Apple]
r swap: [1, 4, 3, 2 2, 5]
Before swap: [apple, banana, cherry]
RESULT:
9d.
PROGRAM:
public class MaxValueFinder {
public static <T extends Comparable<T>> T getMax(T a, T b, T c) {
T max = a;
if ([Link](max) > 0) {
max = b; // b is greater than max
}
if ([Link](max) > 0) {
max = c; // c is greater than max
}
return max;
}
public static void main(String[] args) {
[Link](getMax(3, 5, 4));
[Link](getMax("apple", "banana", "cherry"));
}
}
Output:
Maximum Integer: 25
Maximum Double: 9.8
Maximum String: cherry
RESULT:
10.
PROGRAM:
import [Link];
class Thread1 implements Runnable {
public void run() {
Random rand = new Random();
for (int i = 0; i < 20; i++) {
int number = [Link](10);
[Link]("Generated Number: " + number);
if (number % 2 == 0) {
Even evenThread = new Even(number);
[Link]();
} else {
Odd oddThread = new Odd(number);
[Link]();
}
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]();
}
}
}
}
class Even extends Thread {
private int n;
public Even(int n) {
this.n = n;
}
public void run() {
[Link]("Square of " + n + ": " + (n * n));
}
}
class Odd extends Thread {
private int n;
public Odd(int n) {
this.n = n;
}
public void run() {
[Link]("Cube of " + n + ": " + (n * n * n));
}
}
public class Threads {
public static void main(String[] args) {
Thread1 thread1 = new Thread1();
Thread t1 = new Thread(thread1);
[Link]();
}
}
Output:
Generated number: 5
Cube of 5 = 125
Generated number: 2
Square of 2 = 4
Generated number: 7
Cube of 7 = 343
Generated number: 0
Square of 0 = 0
RESULT:
11a.
PROGRAM:
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Calculator calculator = new Calculator();
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
[Link]("Addition: " + [Link](num1, num2));
[Link]("Subtraction: " + [Link](num1, num2));
[Link]("Multiplication: " + [Link](num1, num2));
[Link]("Division: " + [Link](num1, num2));
[Link]();
}
}
Output:
Enter first number:
10
Enter second number:
5
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0
RESULT:
11b.
PROGRAM:
package fileoperations;
import [Link];
import [Link];
import [Link];
import [Link];
public class FileWriterDemo {
public void writeToFile(String message) {
try (FileWriter writer = new FileWriter("[Link]")) {
[Link](message);
[Link]("Message written to file successfully.");
} catch (IOException e) {
[Link]("An error occurred while writing to the file: " + [Link]());
}
}
public void readFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader("[Link]"))) {
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
} catch (IOException e) {
[Link]("An error occurred while reading the file: " + [Link]());
}
}
}
Output:
Enter the filename (with extension, e.g., [Link]):
[Link]
Enter the message to write into the file:
Hello, this is a test message.
Message written to file successfully.
Reading from file:
Hello, this is a test message.
RESULT:
12a.
PROGRAM:
import [Link];
import [Link];
public class SortArrayList {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
[Link](5);
[Link](2);
[Link](8);
[Link](1);
[Link](4);
[Link](numbers);
[Link]("Sorted in Ascending Order: " + numbers);
[Link](numbers, [Link]());
[Link]("Sorted in Descending Order: " + numbers);
}
}
Output:
Enter the number of elements:
5
Enter the elements of the ArrayList:
4
1
7
3
5
Original ArrayList: [4, 1, 7, 3, 5]
ArrayList in ascending order: [1, 3, 4, 5, 7]
ArrayList in descending order: [7, 5, 4, 3, 1]
RESULT:
12b.
PROGRAM:
import [Link];
public class QueueUsingLinkedList {
private LinkedList<Integer> queue;
public QueueUsingLinkedList() {
queue = new LinkedList<>();
}
public void enqueue(int value) {
[Link](value);
[Link]("Enqueued: " + value);
}
public int dequeue() {
if (![Link]()) {
int value = [Link]();
[Link]("Dequeued: " + value);
return value;
} else {
[Link]("Queue is empty!");
return -1; // Indicating the queue is empty
}
}
public static void main(String[] args) {
QueueUsingLinkedList queue = new QueueUsingLinkedList();
[Link](10);
[Link](20);
[Link]();
[Link]();
[Link]();
}
}
Output:
Enqueued: 10
Enqueued: 20
Dequeued: 10
Dequeued: 20
Queue is empty
RESULT:
12c.
PROGRAM:
import [Link];
public class HashSetRemoval {
public static void main(String[] args) {
HashSet<String> set1 = new HashSet<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
HashSet<String> set2 = new HashSet<>();
[Link]("Banana");
[Link]("Date");
[Link](set2);
[Link]("Modified HashSet: " + set1);
}
}
Output:
Enter the number of elements for first HashSet:
5
Enter elements of first HashSet:
12345
Enter the number of elements for second HashSet:
3
Enter elements of second HashSet:
246
First HashSet before removing elements: [1, 2, 3, 4, 5]
Second HashSet: [2, 4, 6]
First HashSet after removing elements present in second HashSet: [1, 3, 5]
RESULT:
12d.
PROGRAM:
import [Link];
public class TreeSetExample {
public static void main(String[] args) {
TreeSet<Integer> numbers = new TreeSet<>();
[Link](10);
[Link](5);
[Link](20);
[Link](15);
Integer smallest = [Link]();
Integer greatest = [Link]();
[Link]("Smallest Element: " + smallest);
[Link]("Greatest Element: " + greatest);
}
}
Output:
Enter the number of elements for TreeSet:
5
Enter elements:
10 5 20 3 15
TreeSet elements: [3, 5, 10, 15, 20]
Smallest element in TreeSet: 3
Greatest element in TreeSet: 20
RESULT:
12e.
PROGRAM:
import [Link];
public class CharacterFrequency {
public static void main(String[] args) {
String input = "hello world";
HashMap<Character, Integer> frequencyMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
for (char c : [Link]()) {
[Link](c + ": " + [Link](c));
}
}
}
Output:
Enter a string:
hello world
Character frequencies:
h:1
e:1
l:3
o:2
:1
w:1
r:1
d:1
RESULT: