0% found this document useful (0 votes)
5 views14 pages

Java Lab Programs

The document contains multiple Java programs that demonstrate various programming concepts including factorial calculation, prime number display, math functions, constructors, method overloading, method overriding, sorting with exception handling, and inheritance. Each program is structured with a main method and includes user input handling where applicable. The programs illustrate practical applications of Java features and object-oriented programming principles.

Uploaded by

plusabcplus786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

Java Lab Programs

The document contains multiple Java programs that demonstrate various programming concepts including factorial calculation, prime number display, math functions, constructors, method overloading, method overriding, sorting with exception handling, and inheritance. Each program is structured with a main method and includes user input handling where applicable. The programs illustrate practical applications of Java features and object-oriented programming principles.

Uploaded by

plusabcplus786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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(args.length==0)
12 {
13 System.out.println("No Command line arguments");
14 return;
15 }
16
17 for(int i=0;i<args.length;i++)
18 {
19 arr[i]=Integer.parseInt(args[i]);
20 fact=1;
21 while(arr[i]>0)
22 {
23 fact=fact*arr[i];
24 arr[i]--;
25 }
26 System.out.println("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 java.util.Scanner;
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(System.in);
27
28 System.out.print("Enter the lower limit: ");
29 int lowerLimit = scanner.nextInt();
30
31 System.out.print("Enter the upper limit: ");
32 int upperLimit = scanner.nextInt();
33
34 if (lowerLimit > upperLimit)
35 {
36 System.out.println("Invalid range.");
37 return;
38 }
39
40 System.out.println("Prime numbers between " + lowerLimit + " and " + upperLimit +
":");
41 for (int i = lowerLimit; i <= upperLimit; i++)
42 {
43 if (isPrime(i))
44 {
45 System.out.print(i + " ");
46 }
47 }
48 System.out.println();
49
50 scanner.close();
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 System.out.println("Absolute value of -10: " + Math.abs(-10));
10 System.out.println("Maximum of 10 and 20: " + Math.max(10, 20));
11 System.out.println("Minimum of 10 and 20: " + Math.min(10, 20));
12 System.out.println("Square root of 25: " + Math.sqrt(25));
13 System.out.println("Cube root of 27: " + Math.cbrt(27));
14
15 // Power and Exponential Functions
16 System.out.println("2 to the power 3: " + Math.pow(2, 3));
17 System.out.println("Exponential of 1 (e^1): " + Math.exp(1));
18 System.out.println("Natural logarithm of 10: " + Math.log(10));
19 System.out.println("Base-10 logarithm of 100: " + Math.log10(100));
20
21 // Rounding Functions
22 System.out.println("Ceiling of 4.2: " + Math.ceil(4.2));
23 System.out.println("Floor of 4.8: " + Math.floor(4.8));
24 System.out.println("Round 4.5: " + Math.round(4.5));
25 System.out.println("Round 4.4: " + Math.round(4.4));
26
27 // Trigonometric Functions (angles in radians)
28 double radians = Math.toRadians(30); // Convert 30 degrees to radians
29 System.out.println("Sine of 30 degrees: " + Math.sin(radians));
30 System.out.println("Cosine of 30 degrees: " + Math.cos(radians));
31 System.out.println("Tangent of 30 degrees: " + Math.tan(radians));
32
33 // Random Number Generation
34 System.out.println("Random number (0.0 to 1.0): " + Math.random());
35 System.out.println("Random number (1 to 100): " + (int)(Math.random() * 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 java.util.Scanner;
5
6 class Employee
7 {
8 String name;
9 int id;
10
11 // Parent class constructor
12 Employee(String name, int id)
13 {
14 this.name = name;
15 this.id = id;
16 System.out.println("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 this.department = department;
29 System.out.println("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(System.in);
38
39 // Taking user input
40 System.out.print("Enter Employee Name: ");
41 String name = scanner.nextLine();
42
43 System.out.print("Enter Employee ID: ");
44 int id = scanner.nextInt();
45 scanner.nextLine(); // Consume the newline character
46
47 System.out.print("Enter Department: ");
48 String department = scanner.nextLine();
49
50 // Creating Manager object with user input
51 Manager manager = new Manager(name, id, department);
52
53 scanner.close();
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 java.util.Scanner;
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(System.in);
31
32 // Taking integer input
33 System.out.print("Enter first integer: ");
34 int int1 = scanner.nextInt();
35 System.out.print("Enter second integer: ");
36 int int2 = scanner.nextInt();
37 System.out.println("Sum of integers: " + add(int1, int2));
38
39 // Taking float input
40 System.out.print("Enter first float: ");
41 float float1 = scanner.nextFloat();
42 System.out.print("Enter second float: ");
43 float float2 = scanner.nextFloat();
44 System.out.println("Sum of floats: " + add(float1, float2));
45
46 // Using the default method
47 System.out.println("Default sum: " + add());
48
49 // Closing the scanner
50 scanner.close();
51 }
52 }
1 //PART-A: PROGRAM-7
2 //Write a program to calculate bonus for different departments using method overriding.
3
4 import java.util.Scanner;
5
6 // Base class
7 class Employee
8 {
9 double salary;
10
11 // Constructor to initialize salary
12 Employee(double salary)
13 {
14 this.salary = 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(System.in);
77
78 // Taking user input for salary
79 System.out.print("Enter employee salary: ");
80 double salary = scanner.nextDouble();
81
82 Employee emp; // Reference to Employee
83
84 emp = new ITDepartment(salary);
85 System.out.println("Bonus for IT Department: " + emp.calculateBonus());
86
87 emp = new HRDepartment(salary);
88 System.out.println("Bonus for HR Department: " + emp.calculateBonus());
89
90 emp = new SalesDepartment(salary);
91 System.out.println("Bonus for Sales Department: " + emp.calculateBonus());
92
93 scanner.close();
94 }
95 }
1 //PART-A: PROGRAM-8
2 //Write a java program to implement default and parameterized constructors.
3
4 import java.util.Scanner;
5
6 class Student
7 {
8 String name;
9 int age;
10
11 // Default Constructor
12 Student()
13 {
14 System.out.println("Default Constructor Called");
15 this.name = "Unknown";
16 this.age = 0;
17 }
18
19 // Parameterized Constructor (Takes user input)
20 Student(String name, int age)
21 {
22 System.out.println("Parameterized Constructor Called");
23 this.name = name;
24 this.age = age;
25 }
26
27 // Method to display student details
28 void display() {
29 System.out.println("Name: " + name + ", Age: " + age);
30 }
31
32 public static void main(String[] args)
33 {
34 Scanner scanner = new Scanner(System.in);
35
36 // Using Default Constructor
37 Student student1 = new Student();
38 student1.display();
39
40 // Taking User Input for Parameterized Constructor
41 System.out.print("Enter Student Name: ");
42 String name = scanner.nextLine();
43 System.out.print("Enter Student Age: ");
44 int age = scanner.nextInt();
45
46 // Using Parameterized Constructor
47 Student student2 = new Student(name, age);
48 student2.display();
49
50 scanner.close();
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 java.util.Arrays;
5 import java.util.Collections;
6 import java.util.InputMismatchException;
7 import java.util.Scanner;
8
9 public class Sort
10 {
11 public static void main(String[] args)
12 {
13 Scanner scanner = new Scanner(System.in);
14
15 try
16 {
17 // Taking array size input
18 System.out.print("Enter the number of elements: ");
19 int n = scanner.nextInt();
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 System.out.println("Enter " + n + " elements:");
31 for (int i = 0; i < n; i++)
32 {
33 arr[i] = scanner.nextInt();
34 }
35
36 // Sorting in ascending order
37 Arrays.sort(arr);
38 System.out.println("Ascending Order: " + Arrays.toString(arr));
39
40 // Sorting in descending order
41 Arrays.sort(arr, Collections.reverseOrder());
42 System.out.println("Descending Order: " + Arrays.toString(arr));
43
44 }
45 catch (InputMismatchException e)
46 {
47 System.out.println("Error: Please enter valid integer values.");
48 }
49 catch (IllegalArgumentException e)
50 {
51 System.out.println("Error: " + e.getMessage());
52 }
53 catch (Exception e)
54 {
55 System.out.println("An unexpected error occurred: " + e.getMessage());
56 }
57 finally
58 {
59 scanner.close();
60 }
61 }
62 }
1 //PART-B: PROGRAM-13
2 //Write a Java program to implement Single Inheritance.
3
4
5 import java.util.Scanner;
6
7 class Person
8 {
9 String name;
10 int age;
11
12 // Constructor
13 public Person(String name, int age)
14 {
15 this.name = name;
16 this.age = age;
17 }
18
19 // Method to display person details
20 public void displayInfo()
21 {
22 System.out.println("Name: " + name);
23 System.out.println("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 this.salary = salary;
37 }
38
39 // Overriding method to add additional functionality
40 @Override
41 public void displayInfo()
42 {
43 super.displayInfo(); // Call parent class method
44 System.out.println("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(System.in);
54
55 // Taking user input
56 System.out.print("Enter name: ");
57 String name = scanner.nextLine();
58
59 System.out.print("Enter age: ");
60 int age = scanner.nextInt();
61
62 System.out.print("Enter salary: ");
63 double salary = scanner.nextDouble();
64
65 // Creating an Employee object
66 Employee emp = new Employee(name, age, salary);
67
68 // Displaying employee details
69 System.out.println("\nEmployee Details:");
70 emp.displayInfo();
71
72 // Closing the scanner
73 scanner.close();
74 }
75 }
1 //PART-B: PROGRAM-14
2 //Write a Java program to implement Multiple Inheritance using Interface.
3
4 import java.util.Scanner; // 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 this.name = name;
29 this.department = department;
30 this.salary = salary;
31 }
32
33 // Implementing doWork() method
34 @Override
35 public void doWork()
36 {
37 System.out.println(name + " is working in the " + department + " department.");
38 }
39
40 // Implementing calculateSalary() method
41 @Override
42 public void calculateSalary()
43 {
44 System.out.println(name + "'s monthly salary is $" + salary);
45 }
46
47 // Display Employee Details
48 public void displayEmployeeInfo()
49 {
50 System.out.println("\nEmployee Details:");
51 System.out.println("Name: " + name);
52 System.out.println("Department: " + department);
53 System.out.println("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(System.in);
63
64 // Asking for number of employees
65 System.out.print("Enter the number of employees: ");
66 int numEmployees = scanner.nextInt();
67 scanner.nextLine(); // 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 System.out.println("\nEnter details for Employee " + (i + 1) + ":");
76
77 System.out.print("Enter Name: ");
78 String name = scanner.nextLine();
79
80 System.out.print("Enter Department: ");
81 String department = scanner.nextLine();
82
83 System.out.print("Enter Salary: $");
84 double salary = scanner.nextDouble();
85 scanner.nextLine(); // 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 System.out.println("\n----- Employee Information -----");
93 for (Employee emp : employees)
94 {
95 emp.displayEmployeeInfo();
96 emp.doWork();
97 emp.calculateSalary();
98 System.out.println("-------------------------------");
99 }
100
101 scanner.close(); // 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 java.util.Scanner; // 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 this.name = name;
17 this.id = id;
18 }
19
20 // Static method to display details
21 static void display(College col)
22 {
23 System.out.println("Name: " + col.name);
24 System.out.println("ID: " + col.id);
25 System.out.println("Course: " + course);
26 System.out.println();
27 }
28 }
29
30 public class staticvar
31 {
32 public static void main(String args[])
33 {
34 Scanner scanner = new Scanner(System.in);
35
36 // Taking user input for first student
37 System.out.print("Enter first student name: ");
38 String name1 = scanner.nextLine();
39 System.out.print("Enter first student ID: ");
40 int id1 = scanner.nextInt();
41 scanner.nextLine(); // Consume newline
42
43 // Creating objects
44 College col1 = new College(name1, id1);
45
46 // Displaying details
47 College.display(col1);
48
49 // Asking the user to update the static course variable
50 System.out.print("Enter new course name: ");
51 College.course = scanner.nextLine(); // Changing static variable
52
53 // Displaying updated details
54 College.display(col1);
55
56 scanner.close(); // Close scanner to prevent memory leaks
57 }
58 }
59
60

You might also like