Java Practicals

Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


         

class OnearrTOAnotherArray 
{
	public static void main(String[] args) 
	{
		int arr1[] = new int[] {1,2,3 ,4 ,5};
		int arr2[] = new int[arr1.length];
		System.out.println("Elements of First array: ");
	    for (int i = 0; i < arr1.length; i++) 
	    {
	      System.out.print(arr1[i] + " ");
	    }
	    //copying the first array to the second array
	    for (int i = 0; i < arr1.length; i++)
	    {
	        arr2[i] = arr1[i];
	    }
	    System.out.println();
	    System.out.println("Elements of Second array: ");
	    for (int i = 0; i < arr2.length; i++) 
	    {
	      System.out.print(arr2[i] + " ");
	    }
	}

}

                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            import java.util.HashMap;
            import java.util.Map;
            
            class FrequencyCounter {
                public static void main(String[] args) {
                   
                    int[] array = {1, 2, 3, 4, 1, 2, 2, 3, 5, 4, 6, 7, 6, 8, 9, 9};
            
                    
                    Map frequencyMap = new HashMap<>();
            
                    
                    for (int element : array) {
                       
                        if (frequencyMap.containsKey(element)) {
                            frequencyMap.put(element, frequencyMap.get(element) + 1);
                        } else {
                           
                            frequencyMap.put(element, 1);
                        }
                    }
            
                    System.out.println("Element frequencies:");
                    for (Map.Entry entry : frequencyMap.entrySet()) {
                        System.out.println(entry.getKey() + ": " + entry.getValue());
                    }
                }
            }
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            /*Program to left rotate the elements of an array*/

            class RotateLeft {  
                public static void main(String[] args) {  
                  
                    int [] arr = new int [] {1, 2, 3, 4, 5}; 
                      //n determine the number of times an array should be rotated 
                    int n = 3;  
                    //Displays original array  
                    System.out.println("Original array: ");  
                    for (int i = 0; i < arr.length; i++) {  
                        System.out.print(arr[i] + " ");  
                    }  
                      
                    for(int i = 0; i < n; i++){  
                        int j, first;  
                        //Stores the first element of the array  
                        first = arr[0];  
                        for(j = 0; j < arr.length-1; j++){  
                            //Shift element of array by one  
                            arr[j] = arr[j+1];  
                        }  
                        //First element of array will be added to the end  
                        arr[j] = first;  
                    }  
                    System.out.println();  
                    //Displays resulting array after rotation  
                    System.out.println("Array after left rotation: ");  
                    for(int i = 0; i< arr.length; i++){  
                        System.out.print(arr[i] + " ");  
                    }  
                }  
            }  
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            //Program to print the duplicate elements of an array.
            class FindDuplicateElements{
                public static void main(String[] args) {
                    int[] arr = {1, 2, 3, 4, 2, 5, 6, 7, 3, 8, 8};
                    findDuplicateElements(arr);
                }
            
                public static void findDuplicateElements(int[] arr) {
                    int n = arr.length;
                    for (int i = 0; i < n; i++) {
                        if (arr[i] != -1) {
                            for (int j = i + 1; j < n; j++) {
                                if (arr[i] == arr[j]) {
                                    System.out.println("Duplicate element: " + arr[i]);
                                    arr[j] = -1;
                                }
                            }
                        }
                    }
                }
            }
            
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            class PrintArrayElements {
              public static void main(String[] args) {
                  // Create an array of integers
                  int[] numbers = {1, 2, 3, 4, 5};
          
                  // Print the elements of the array
                  System.out.println("Elements of the array:");
                  for (int i = 0; i < numbers.length; i++) {
                      System.out.println(numbers[i]);
                  }
              }
          }
          
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            class ReverseArray {
              public static void main(String[] args) {
                  int[] array = {1, 2, 3, 4, 5};
                  
                  for (int i = array.length - 1; i >= 0; i--) {
                      System.out.println(array[i]);
                  }
              }
          }
          
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            
//Program to print the elements of an array present on even position
class ArrayPresentOnEvenPosition 
{
	public static void main(String[] args) 
	{
		int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
        for (int i = 1; i < arr.length; i = i + 2) 
        {
            // printing the even elements
            System.out.println(arr[i]);
        }

	}

}

                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            class oddposition{
              public static void main(String args[])
              {
              int arr[]={1,2,3,4,5};
              for(int i=0;i< 5;i++)
              {
                  if(arr[i]%2!=0){
                  System.out.println(arr[i]);
                  }
                  else{
                   System.out.println("");
                  }
              }
              
              
              }
              }
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            import java.util.Scanner;

class largestelement {
    public static void main(String[] a) {
        Scanner s = new Scanner(System.in);

        System.out.print("Enter the number of elements in the array: ");
        int n = s.nextInt();

        int[] arr = new int[n];

        System.out.println("Enter the elements of the array:");

        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }

        int largest = arr[0];

        for (int i = 1; i < n; i++) {
            if (arr[i] > largest) {
                largest = arr[i];
            }
        }

        System.out.println("The largest element in the array is: " + largest);

    }
}

                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            import java.util.Scanner;

            class smallestelement {
                public static void main(String[] a) {
                    Scanner s = new Scanner(System.in);
            
                    System.out.print("Enter the number of elements in the array: ");
                    int n = s.nextInt();
            
                    int[] arr = new int[n];
            
                    System.out.println("Enter the elements of the array:");
            
                    for (int i = 0; i < n; i++) {
                        arr[i] = s.nextInt();
                    }
            
                    int smallest = arr[0]; 
            
                    for (int i = 1; i < n; i++) {
                        if (arr[i] < smallest) {
                            smallest = arr[i];
                        }
                    }
            
                    System.out.println("The smallest element in the array is: " + smallest);
            
                }
            }
            
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            class ArrayLength{
              public static void main(String[] args) {
                  // Declare and initialize an array
                  int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                  // Get the length of the array
                  int arrayLength = numbers.length;
                  // Print the number of elements in the array
                  System.out.println("The number of elements in the array is: " + arrayLength);
              }
          }
          
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            class ArraySum {
              public static void main(String[] args) {
                  
                  int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
          
               
                  System.out.println("Original Array:");
                  displayArray(array);
          
                 
                  int sum = calculateArraySum(array);
                  
                  System.out.println("Sum of all elements in the array: " + sum);
              }
          
              
              private static int calculateArraySum(int[] arr) {
                  int sum = 0;
          
                 
                  for (int num : arr) {
                      sum += num;
                  }
          
                  return sum;
              }
          
          
              private static void displayArray(int[] arr) {
                  for (int num : arr) {
                      System.out.print(num + " ");
                  }
                  System.out.println();
              }
          }
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            class RightRotateArray {
              public static void main(String[] args) {
                  int[] array = {1, 2, 3, 4, 5};
          
                  int positionsToRotate = 2;
                  rightRotate(array, positionsToRotate);
          
                  System.out.print("Rotated Array: ");
                  for (int num : array) {
                      System.out.print(num + " ");
                  }
              }
              private static void rightRotate(int[] arr, int positions) {
                  int length = arr.length;
          
                  positions = positions % length;
          
                  int[] temp = new int[positions];
          
                  for (int i = 0; i < positions; i++) {
                      temp[i] = arr[length - positions + i];
                  }
          
                  for (int i = length - 1; i >= positions; i--) {
                      arr[i] = arr[i - positions];
                  }
          
                  for (int i = 0; i < positions; i++) {
                      arr[i] = temp[i];
                  }
              }
          }
          
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            class SortAsc {    
              public static void main(String[] args) {        
                      
                  //Initialize array     
                  int [] arr = new int [] {5, 2, 8, 7, 1};     
                  int temp = 0;    
                      
                  //Displaying elements of original array    
                  System.out.println("Elements of original array: ");    
                  for (int i = 0; i < arr.length; i++) {     
                      System.out.print(arr[i] + " ");    
                  }    
                      
                  //Sort the array in ascending order    
                  for (int i = 0; i < arr.length; i++) {     
                      for (int j = i+1; j < arr.length; j++) {     
                         if(arr[i] > arr[j]) {    
                             temp = arr[i];    
                             arr[i] = arr[j];    
                             arr[j] = temp;    
                         }     
                      }     
                  }    
                    
                  System.out.println();    
                      
                  //Displaying elements of array after sorting    
                  System.out.println("Elements of array sorted in ascending order: ");    
                  for (int i = 0; i < arr.length; i++) {     
                      System.out.print(arr[i] + " ");    
                  }    
              }    
          }    
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            import java.lang.*;
class K{    
    public static void main(String[] args) {         
        int [] arr = new int [] {5, 2, 8, 7, 1};     
        int temp = 0;      
        for (int i = 0; i < arr.length; i++) {     
            for (int j = i+1; j < arr.length; j++) {     
               if(arr[i] < arr[j]) {    
                   temp = arr[i];    
                   arr[i] = arr[j];    
                   arr[j] = temp;    
               }     
            }     
        }       
        for (int i = 0; i < arr.length; i++) {     
            System.out.print(arr[i] + " ");    
        }    
    }    
}    

                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            import java.util.Arrays;

            // Program to Find 3rd Largest Number in an array
            
             class kamal7019java1d16 {
                public static int getThirdLargest(int[] a, int total) {
                    Arrays.sort(a);
                    return a[total - 3];
                }
            
                public static void main(String args[]) {
                    int a[] = {1, 2, 5, 6, 3, 2};
                    int b[] = {44, 66, 99, 77, 33, 22, 55};
                    System.out.println("Third Largest: " + getThirdLargest(a, 6));
                    System.out.println("Third Largest: " + getThirdLargest(b, 7));
                }
            }
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            import java.util.Scanner;

            class secondlargestnumber {
                public static void main(String[] a) {
                    Scanner s = new Scanner(System.in);
            
                    System.out.print("Enter the number of elements in the array: ");
                    int n = s.nextInt();
            
                    int[] arr = new int[n];
            
                    System.out.println("Enter the elements of the array:");
            
                    for (int i = 0; i < n; i++) {
                        arr[i] = s.nextInt();
                    }
            
                    if (n < 2) {
                        System.out.println("There is no 2nd largest element in the array.");
                    } else {
                        int firstLargest = Integer.MIN_VALUE;
                        int secondLargest = Integer.MIN_VALUE;
            
                        for (int i = 0; i < n; i++) {
                            if (arr[i] > firstLargest) {
                                secondLargest = firstLargest;
                                firstLargest = arr[i];
                            } else if (arr[i] > secondLargest && arr[i] != firstLargest) {
                                secondLargest = arr[i];
                            }
                        }
            
                        if (secondLargest == Integer.MIN_VALUE) {
                            System.out.println("There is no 2nd largest element in the array.");
                        } else {
                            System.out.println("The 2nd largest element in the array is: " + secondLargest);
                        }
                    }
            
                }
            }
            
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            class findlargestarrayelement {
              public static void main(String[] args) {
                  int[] numbers = {10, 5, 7, 25, 30, 75};
          
                  int largestNumber = findLargestNumber(numbers);
                  System.out.println("The largest number in the array is: " + largestNumber);
              }
          
              public static int findLargestNumber(int[] arr) {
                  if (arr.length == 0) {
                      throw new IllegalArgumentException("Array is empty");
                  }
          
                  int max = arr[0];
                  for (int i = 1; i < arr.length; i++) {
                      if (arr[i] > max) {
                          max = arr[i];
                      }
                  }
                  return max;
              }
          }
          
          
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            class SmallestNumberInAnArray {
              public static void main(String args[]){
                 int temp, size;
                 int array[] = {10, 20, 25, 63, 96, 57};
                 size = array.length;
           
                 for(int i = 0; iarray[j]){
                          temp = array[i];
                          array[i] = array[j];
                          array[j] = temp;
                       }
                    }
                 }
                 System.out.println("2nd Smallest element of the array is:: "+array[0]);
              }
           }
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            import java.util.Scanner;

            class smallestnumber {
                public static void main(String[] a) {
                    Scanner s = new Scanner(System.in);
            
                    System.out.print("Enter the number of elements in the array: ");
                    int n = s.nextInt();
            
                    int[] arr = new int[n];
            
                    System.out.println("Enter the elements of the array:");
            
                    for (int i = 0; i < n; i++) {
                        arr[i] = s.nextInt();
                    }
            
                    if (n == 0) {
                        System.out.println("The array is empty.");
                    } else {
                        int smallest = arr[0]; 
            
                        for (int i = 1; i < n; i++) {
                            if (arr[i] < smallest) {
                                smallest = arr[i];
                            }
                        }
            
                        System.out.println("The smallest element in the array is: " + smallest);
                    }
            
                }
            }
            
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            import java.util.Arrays;

            class array1 {
                public static void main(String[] args) {
                    int[] arr = {1, 2, 3, 4, 2, 7, 8, 8, 3};
                    System.out.println("Duplicate elements in the given array: ");
                    for (int i = 0; i < arr.length - 1; i++) {
                        for (int j = i + 1; j < arr.length; j++) {
                            if ((arr[i] == arr[j]) && (i != j)) {
                                System.out.print(arr[j] + " ");
                            }
                        }
                    }
                }
            }
            
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            
            
           
//Program to Print Odd and Even Numbers from an array


class C
{
       
       
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        System.out.println("Even numbers:");
        for (int number : numbers) {
            if (number % 2 == 0) {
                System.out.print(number + " ");
            }
        }
        System.out.println();

        System.out.println("Odd numbers:");
        for (int number : numbers) {
            if (number % 2 != 0) {
                System.out.print(number + " ");
            }
        }
        System.out.println();
    
}


}
                    

Output


Inheritance program 19 - Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role


            
            // How to Sort an Array in Java (Program 23)
            // Created by Aryan , Rollno 7070
            // Note : In order to compile and run this program , rename it from "7070SingleDimensionArray23.java" to "SingleDimensionArray23.java"
            import java.util.Arrays;
            
            class SingleDimensionArray23 {
                public static void main(String[] args) {
            
                    // example arrays =>
            
                    int[] intArray = {10,91,60,51,9,6};
            
                    String[] stringArray = {"laptop","CPU","Monitor","Keyboard","Mic"};
            
                    // sorting via Array.sort() 
                    System.out.println("\nOriginal Array: " + Arrays.toString(intArray));
                    Arrays.sort(intArray);
                    System.out.println("Sorted int array in ascending order: " + Arrays.toString(intArray));
            
                   System.out.println("\nOriginal Array: " + Arrays.toString(stringArray));
                    Arrays.sort(stringArray);
                    System.out.println("Sorted string array in ascending order: " + Arrays.toString(stringArray));
                }
            }
                    

Output