Note:-
👉static methods can only access static variables
👉A static method can only call other static method
👉static method can't refer to non-static variables or methods.
Q.1 Find the smallest and largest element in the given array.
import java.util.Arrays;
import java.util.Scanner;
public class SmallestAndLargestElenet {
static int [] smallestAndLargest(int []arr){
Arrays.sort(arr);
int []ans={arr[0],arr[arr.length-1]};
return ans;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the Array: ");
int n=sc.nextInt();
int [] arr=new int [n];
System.out.println("Enter the "+ n+" " + "elements:");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int[] ans=smallestAndLargest(arr);
System.out.println("Smallest Element ="+ans[0]);
System.out.println("Largest element="+ans[1]);
}
}
OUTPUT:-
Q.2 Check if the given array is sorted or not?
import java.util.Scanner;
public class CheckArray {
static boolean isSorted(int []arr){
boolean check=true;
for(int i=1;i<arr.length;i++){
if(arr[i]<arr[i-1]){
check=false;
break;
}
}
return check;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the Array: ");
int n=sc.nextInt();
int [] arr=new int [n];
System.out.println("Enter the "+ n+" " + "elements:");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println( isSorted(arr));
}
}
OUTPUT:-
Q.3 Find the last occurrence of an element x in a given array.
import java.util.Scanner;public class LastOccurrence { static int LastOccurrenceElement(int arr[],int x){ int lastIndex=-1; for(int i=0;i<arr.length;i++){ if(arr[i]==x){ lastIndex=i; } } return lastIndex; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the size of the Array: "); int n=sc.nextInt(); int [] arr=new int [n]; System.out.println("Enter the "+ n+" " + "elements:"); for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } System.out.println("Enter the value of x:"); int x=sc.nextInt(); System.out.println("Last Occarrence Value=" +LastOccurrenceElement(arr,x)); } }OUTPUT:-
import java.util.Scanner;
public class LastOccurrence {
static int LastOccurrenceElement(int arr[],int x){
int lastIndex=-1;
for(int i=0;i<arr.length;i++){
if(arr[i]==x){
lastIndex=i;
}
}
return lastIndex;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the Array: ");
int n=sc.nextInt();
int [] arr=new int [n];
System.out.println("Enter the "+ n+" " + "elements:");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println("Enter the value of x:");
int x=sc.nextInt();
System.out.println("Last Occarrence Value=" +LastOccurrenceElement(arr,x));
}
}
OUTPUT:-
Q.4 Count the number of occurrences of a particular element x.
import java.util.Scanner;
public class ArrayLecture2 {
//Q-1 Count the number of occurrences of a particular element x
static int countOccarrences(int arr[],int x){
int count=0;
for(int i=0;i<arr.length;i++){
if(arr[i]==x){
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the Array:");
int n=sc.nextInt();
int [] arr=new int [n];
System.out.println("Enter the "+ n+" " + "elements:");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println("Enter the value of x:");
int a=sc.nextInt();
System.out.println("count of x="+countOccarrences(arr,a));
}
}
OUTPUT:-