Array Compatibility Program in java

Question:

Two arrays are said to be compatible if they are of the same size and if the ith element in the first array is greater than or equal to the ith element in the second array for all i elements.If the array size is zero or lesser then display the message “Invalid array size”.Write a Java program to find whether 2 arrays are compatible or not.If the arrays are compatible display the message as “Arrays are Compatible” ,if not then display the message as “Arrays are Not Compatible”.

Sample Input 1:

Enter the size for First array:

5

Enter the elements for First array:

5

14

17

19

15

Enter the size for Second array:

5

Enter the elements for Second array:

2

5

9

15

7

Sample Output 1:

Arrays are Compatible

Sample Input 2:

Enter the size for First array:

3

Enter the elements for First array:

1

4

7

Enter the size for Second array:

5

Enter the elements for Second array:

2

5

9

5

7

Sample Output 2:

Arrays are Not Compatible

Sample Input 3:

Enter the size for First array:

-2

Sample Output 3:

Invalid array size

CODE:

import java.util.*;
public class CompatibleArrays 
{
    public static void main (String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        
        //1st array
        System.out.println("Enter the size for First array:");
        int n1=sc.nextInt();
        int flag=0;
        if(n1>0)
        {
            int[] arr1=new int[n1];
            
            System.out.println("Enter the elements for First array:");
            for(int i=0;i<n1;i++)
            {
                arr1[i]=sc.nextInt();
            }
            
            //2nd array
            System.out.println("Enter the size for Second array:");
            int n2=sc.nextInt();
            if(n2>0)
            {
                    int[] arr2=new int[n2];
                    
                    System.out.println("Enter the elements for Second array:");
                    for(int i=0;i<n2;i++)
                    {
                        arr2[i]=sc.nextInt();
                    }
                    
                    if(n1==n2)
                    {
                        for(int i=0; i<n1;i++)
                        {
                            if(arr1[i]>=arr2[i])
                            {
                                flag++;
                            }
                        }
                        
                        if(flag==n1)
                        {
                            System.out.println("Arrays are Compatible");
                        }
                        else
                        {
                            System.out.println("Arrays are not Compatible");
                        }
                        
                    }
                    else
                    {
                        System.out.println("Arrays are Not Compatible");
                    }
            }
            else
            {
                System.out.println("Invalid array size");
            }
        }
        else
        {
            System.out.println("Invalid array size");
        }
    }
}
public class Student 
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    
    public void setId(int id)
    {
        this.id=id;
    }
    
    public int getId()
    {
        return this.id;
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    
    public int[] getMarks()
    {
        return this.marks;
    }
    
    public void calculateAvg()
    {   float sum=0;
        
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
        
    }
    
    public void setAverage(float average)
    {
        this.average=average;
    }
    
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    
    public float getAverage()
    {
        return this.average;
    }
    
    public char getGrade()
    {
        return this.grade;
    }
}
Next:
  1. Array Compatiblilty
  2. Sum of the maximum and the minimum element
  3. String Concatenation

Leave a Comment

Scroll to Top