1 //PART-A: PROGRAM-1
2 //Write a program to find factorial of list of number reading input as command line
argument.
3
4 public class Factorial
5 {
6 public static void main(String args[])
7 {
8
9 int[] arr = new int[10];
10 int fact;
11 if([Link]==0)
12 {
13 [Link]("No Command line arguments");
14 return;
15 }
16
17 for(int i=0;i<[Link];i++)
18 {
19 arr[i]=[Link](args[i]);
20 fact=1;
21 while(arr[i]>0)
22 {
23 fact=fact*arr[i];
24 arr[i]--;
25 }
26 [Link]("Factorial of "+ args[i]+" is: "+fact);
27 }
28 }
29 }
1 //PART-A: PROGRAM-2
2 //Write a program to display all prime numbers between two limits.
3
4 import [Link];
5
6 public class PrimeNumber
7 {
8 // Method to check if a number is prime
9 public static boolean isPrime(int num)
10 {
11 if (num <= 1) {
12 return false;
13 }
14 for (int i = 2; i < num; i++)
15 {
16 if (num % i == 0)
17 {
18 return false;
19 }
20 }
21 return true;
22 }
23
24 public static void main(String args[])
25 {
26 Scanner scanner = new Scanner([Link]);
27
28 [Link]("Enter the lower limit: ");
29 int lowerLimit = [Link]();
30
31 [Link]("Enter the upper limit: ");
32 int upperLimit = [Link]();
33
34 if (lowerLimit > upperLimit)
35 {
36 [Link]("Invalid range.");
37 return;
38 }
39
40 [Link]("Prime numbers between " + lowerLimit + " and " + upperLimit +
":");
41 for (int i = lowerLimit; i <= upperLimit; i++)
42 {
43 if (isPrime(i))
44 {
45 [Link](i + " ");
46 }
47 }
48 [Link]();
49
50 [Link]();
51 }
52 }
53
1 //PART-A: PROGRAM-3
2 //Write a java program to check all Math class functions.
3
4 public class MathFunc
5 {
6 public static void main(String args[])
7 {
8 // Basic Math Operations
9 [Link]("Absolute value of -10: " + [Link](-10));
10 [Link]("Maximum of 10 and 20: " + [Link](10, 20));
11 [Link]("Minimum of 10 and 20: " + [Link](10, 20));
12 [Link]("Square root of 25: " + [Link](25));
13 [Link]("Cube root of 27: " + [Link](27));
14
15 // Power and Exponential Functions
16 [Link]("2 to the power 3: " + [Link](2, 3));
17 [Link]("Exponential of 1 (e^1): " + [Link](1));
18 [Link]("Natural logarithm of 10: " + [Link](10));
19 [Link]("Base-10 logarithm of 100: " + Math.log10(100));
20
21 // Rounding Functions
22 [Link]("Ceiling of 4.2: " + [Link](4.2));
23 [Link]("Floor of 4.8: " + [Link](4.8));
24 [Link]("Round 4.5: " + [Link](4.5));
25 [Link]("Round 4.4: " + [Link](4.4));
26
27 // Trigonometric Functions (angles in radians)
28 double radians = [Link](30); // Convert 30 degrees to radians
29 [Link]("Sine of 30 degrees: " + [Link](radians));
30 [Link]("Cosine of 30 degrees: " + [Link](radians));
31 [Link]("Tangent of 30 degrees: " + [Link](radians));
32
33 // Random Number Generation
34 [Link]("Random number (0.0 to 1.0): " + [Link]());
35 [Link]("Random number (1 to 100): " + (int)([Link]() * 100 + 1
));
36 }
37 }
38
1 //PART-A: PROGRAM-4
2 //Write a program to implement derived class constructors with ‘super’ keyword.
3
4 import [Link];
5
6 class Employee
7 {
8 String name;
9 int id;
10
11 // Parent class constructor
12 Employee(String name, int id)
13 {
14 [Link] = name;
15 [Link] = id;
16 [Link]("Employee Constructor: Name = " + name + ", ID = " + id);
17 }
18 }
19
20 class Manager extends Employee
21 {
22 String department;
23
24 // Child class constructor
25 Manager(String name, int id, String department)
26 {
27 super(name, id); // Calling parent class constructor
28 [Link] = department;
29 [Link]("Manager Constructor: Department = " + department);
30 }
31 }
32
33 public class supercons
34 {
35 public static void main(String[] args)
36 {
37 Scanner scanner = new Scanner([Link]);
38
39 // Taking user input
40 [Link]("Enter Employee Name: ");
41 String name = [Link]();
42
43 [Link]("Enter Employee ID: ");
44 int id = [Link]();
45 [Link](); // Consume the newline character
46
47 [Link]("Enter Department: ");
48 String department = [Link]();
49
50 // Creating Manager object with user input
51 Manager manager = new Manager(name, id, department);
52
53 [Link]();
54 }
55 }
56
1 //PART-A: PROGRAM-6
2 //Write a Java program to add two integers and two float numbers. When no arguments are
supplied give a default value to calculate the sum. Use method overloading.
3
4 import [Link];
5
6 class Addition
7 {
8
9 // Method to add two integers
10 static int add(int a, int b)
11 {
12 return a + b;
13 }
14
15 // Method to add two float numbers
16 static float add(float a, float b)
17 {
18 return a + b;
19 }
20
21 // Method with no arguments (uses default values)
22 static int add()
23 {
24 int defval1 = 5;
25 int defval2 = 10;
26 return defval1 + defval2;
27 }
28
29 public static void main(String[] args) {
30 Scanner scanner = new Scanner([Link]);
31
32 // Taking integer input
33 [Link]("Enter first integer: ");
34 int int1 = [Link]();
35 [Link]("Enter second integer: ");
36 int int2 = [Link]();
37 [Link]("Sum of integers: " + add(int1, int2));
38
39 // Taking float input
40 [Link]("Enter first float: ");
41 float float1 = [Link]();
42 [Link]("Enter second float: ");
43 float float2 = [Link]();
44 [Link]("Sum of floats: " + add(float1, float2));
45
46 // Using the default method
47 [Link]("Default sum: " + add());
48
49 // Closing the scanner
50 [Link]();
51 }
52 }
1 //PART-A: PROGRAM-7
2 //Write a program to calculate bonus for different departments using method overriding.
3
4 import [Link];
5
6 // Base class
7 class Employee
8 {
9 double salary;
10
11 // Constructor to initialize salary
12 Employee(double salary)
13 {
14 [Link] = salary;
15 }
16
17 // Method to calculate bonus (to be overridden)
18 double calculateBonus()
19 {
20 return 0; // Default, should be overridden by subclasses
21 }
22 }
23
24 // IT Department subclass
25 class ITDepartment extends Employee
26 {
27 ITDepartment(double salary)
28 {
29 super(salary);
30 }
31
32 // Overriding method
33 @Override
34 double calculateBonus()
35 {
36 return salary * 0.10; // 10% bonus
37 }
38 }
39
40 // HR Department subclass
41 class HRDepartment extends Employee
42 {
43 HRDepartment(double salary)
44 {
45 super(salary);
46 }
47
48 // Overriding method
49 @Override
50 double calculateBonus()
51 {
52 return salary * 0.08; // 8% bonus
53 }
54 }
55
56 // Sales Department subclass
57 class SalesDepartment extends Employee
58 {
59 SalesDepartment(double salary)
60 {
61 super(salary);
62 }
63
64 // Overriding method
65 @Override
66 double calculateBonus()
67 {
68 return salary * 0.12; // 12% bonus
69 }
70 }
71
72 public class BonusCalculator
73 {
74 public static void main(String[] args)
75 {
76 Scanner scanner = new Scanner([Link]);
77
78 // Taking user input for salary
79 [Link]("Enter employee salary: ");
80 double salary = [Link]();
81
82 Employee emp; // Reference to Employee
83
84 emp = new ITDepartment(salary);
85 [Link]("Bonus for IT Department: " + [Link]());
86
87 emp = new HRDepartment(salary);
88 [Link]("Bonus for HR Department: " + [Link]());
89
90 emp = new SalesDepartment(salary);
91 [Link]("Bonus for Sales Department: " + [Link]());
92
93 [Link]();
94 }
95 }
1 //PART-A: PROGRAM-8
2 //Write a java program to implement default and parameterized constructors.
3
4 import [Link];
5
6 class Student
7 {
8 String name;
9 int age;
10
11 // Default Constructor
12 Student()
13 {
14 [Link]("Default Constructor Called");
15 [Link] = "Unknown";
16 [Link] = 0;
17 }
18
19 // Parameterized Constructor (Takes user input)
20 Student(String name, int age)
21 {
22 [Link]("Parameterized Constructor Called");
23 [Link] = name;
24 [Link] = age;
25 }
26
27 // Method to display student details
28 void display() {
29 [Link]("Name: " + name + ", Age: " + age);
30 }
31
32 public static void main(String[] args)
33 {
34 Scanner scanner = new Scanner([Link]);
35
36 // Using Default Constructor
37 Student student1 = new Student();
38 [Link]();
39
40 // Taking User Input for Parameterized Constructor
41 [Link]("Enter Student Name: ");
42 String name = [Link]();
43 [Link]("Enter Student Age: ");
44 int age = [Link]();
45
46 // Using Parameterized Constructor
47 Student student2 = new Student(name, age);
48 [Link]();
49
50 [Link]();
51 }
52 }
53
1 //PART-B: PROGRAM-12
2 //Write a program to sort list of elements in ascending and descending order and show
the exception handling.
3
4 import [Link];
5 import [Link];
6 import [Link];
7 import [Link];
8
9 public class Sort
10 {
11 public static void main(String[] args)
12 {
13 Scanner scanner = new Scanner([Link]);
14
15 try
16 {
17 // Taking array size input
18 [Link]("Enter the number of elements: ");
19 int n = [Link]();
20
21 // Handling negative size input
22 if (n <= 0)
23 {
24 throw new IllegalArgumentException("Array size must be greater than 0.");
25 }
26
27 Integer[] arr = new Integer[n];
28
29 // Taking array elements input
30 [Link]("Enter " + n + " elements:");
31 for (int i = 0; i < n; i++)
32 {
33 arr[i] = [Link]();
34 }
35
36 // Sorting in ascending order
37 [Link](arr);
38 [Link]("Ascending Order: " + [Link](arr));
39
40 // Sorting in descending order
41 [Link](arr, [Link]());
42 [Link]("Descending Order: " + [Link](arr));
43
44 }
45 catch (InputMismatchException e)
46 {
47 [Link]("Error: Please enter valid integer values.");
48 }
49 catch (IllegalArgumentException e)
50 {
51 [Link]("Error: " + [Link]());
52 }
53 catch (Exception e)
54 {
55 [Link]("An unexpected error occurred: " + [Link]());
56 }
57 finally
58 {
59 [Link]();
60 }
61 }
62 }
1 //PART-B: PROGRAM-13
2 //Write a Java program to implement Single Inheritance.
3
4
5 import [Link];
6
7 class Person
8 {
9 String name;
10 int age;
11
12 // Constructor
13 public Person(String name, int age)
14 {
15 [Link] = name;
16 [Link] = age;
17 }
18
19 // Method to display person details
20 public void displayInfo()
21 {
22 [Link]("Name: " + name);
23 [Link]("Age: " + age);
24 }
25 }
26
27 // Child class: Employee (Single-Level Inheritance)
28 class Employee extends Person
29 {
30 double salary;
31
32 // Constructor (Chaining with Superclass)
33 public Employee(String name, int age, double salary)
34 {
35 super(name, age); // Calls Person constructor
36 [Link] = salary;
37 }
38
39 // Overriding method to add additional functionality
40 @Override
41 public void displayInfo()
42 {
43 [Link](); // Call parent class method
44 [Link]("Salary: $" + salary);
45 }
46 }
47
48 // Main class to test the program
49 public class Inheritance
50 {
51 public static void main(String[] args)
52 {
53 Scanner scanner = new Scanner([Link]);
54
55 // Taking user input
56 [Link]("Enter name: ");
57 String name = [Link]();
58
59 [Link]("Enter age: ");
60 int age = [Link]();
61
62 [Link]("Enter salary: ");
63 double salary = [Link]();
64
65 // Creating an Employee object
66 Employee emp = new Employee(name, age, salary);
67
68 // Displaying employee details
69 [Link]("\nEmployee Details:");
70 [Link]();
71
72 // Closing the scanner
73 [Link]();
74 }
75 }
1 //PART-B: PROGRAM-14
2 //Write a Java program to implement Multiple Inheritance using Interface.
3
4 import [Link]; // Import Scanner for user input
5
6 // First Interface: Work
7 interface Work
8 {
9 void doWork();
10 }
11
12 // Second Interface: Salary
13 interface Salary
14 {
15 void calculateSalary();
16 }
17
18 // Class implementing multiple interfaces
19 class Employee implements Work, Salary
20 {
21 private String name;
22 private String department;
23 private double salary;
24
25 // Constructor
26 public Employee(String name, String department, double salary)
27 {
28 [Link] = name;
29 [Link] = department;
30 [Link] = salary;
31 }
32
33 // Implementing doWork() method
34 @Override
35 public void doWork()
36 {
37 [Link](name + " is working in the " + department + " department.");
38 }
39
40 // Implementing calculateSalary() method
41 @Override
42 public void calculateSalary()
43 {
44 [Link](name + "'s monthly salary is $" + salary);
45 }
46
47 // Display Employee Details
48 public void displayEmployeeInfo()
49 {
50 [Link]("\nEmployee Details:");
51 [Link]("Name: " + name);
52 [Link]("Department: " + department);
53 [Link]("Salary: $" + salary);
54 }
55 }
56
57 // Main class
58 public class DemoInterface
59 {
60 public static void main(String[] args)
61 {
62 Scanner scanner = new Scanner([Link]);
63
64 // Asking for number of employees
65 [Link]("Enter the number of employees: ");
66 int numEmployees = [Link]();
67 [Link](); // Consume newline
68
69 // Creating an array of Employee objects
70 Employee[] employees = new Employee[numEmployees];
71
72 // Taking user input for each employee
73 for (int i = 0; i < numEmployees; i++)
74 {
75 [Link]("\nEnter details for Employee " + (i + 1) + ":");
76
77 [Link]("Enter Name: ");
78 String name = [Link]();
79
80 [Link]("Enter Department: ");
81 String department = [Link]();
82
83 [Link]("Enter Salary: $");
84 double salary = [Link]();
85 [Link](); // Consume newline
86
87 // Creating Employee object and storing it in the array
88 employees[i] = new Employee(name, department, salary);
89 }
90
91 // Displaying employee details and performing actions
92 [Link]("\n----- Employee Information -----");
93 for (Employee emp : employees)
94 {
95 [Link]();
96 [Link]();
97 [Link]();
98 [Link]("-------------------------------");
99 }
100
101 [Link](); // Closing the scanner
102 }
103 }
1 //PART-B:PROGRAM-15
2 //Write a program to implement static data members and static member methods using
classes.
3
4 import [Link]; // Import Scanner class
5
6 class College
7 {
8 String name;
9 int id;
10
11 // Static variable (shared among all instances)
12 static String course = "BCOM";
13
14 College(String name, int id)
15 {
16 [Link] = name;
17 [Link] = id;
18 }
19
20 // Static method to display details
21 static void display(College col)
22 {
23 [Link]("Name: " + [Link]);
24 [Link]("ID: " + [Link]);
25 [Link]("Course: " + course);
26 [Link]();
27 }
28 }
29
30 public class staticvar
31 {
32 public static void main(String args[])
33 {
34 Scanner scanner = new Scanner([Link]);
35
36 // Taking user input for first student
37 [Link]("Enter first student name: ");
38 String name1 = [Link]();
39 [Link]("Enter first student ID: ");
40 int id1 = [Link]();
41 [Link](); // Consume newline
42
43 // Creating objects
44 College col1 = new College(name1, id1);
45
46 // Displaying details
47 [Link](col1);
48
49 // Asking the user to update the static course variable
50 [Link]("Enter new course name: ");
51 [Link] = [Link](); // Changing static variable
52
53 // Displaying updated details
54 [Link](col1);
55
56 [Link](); // Close scanner to prevent memory leaks
57 }
58 }
59
60