Sunday, 12 March 2023

6. Create a class called NumberData that accept any array of the five numbers. Create a sub class called Numplay which provides methods for followings: 1. Display numbers entered. 2. Sum of the number. 3. Average of the numbers. 4. Maximum of the numbers. 5. Minimum of the numbers. Create a class that provides menu for above methods. Give choice from the command-line argument.



import java.util.*;

class NumberData
{
    int arr[] = new int[5];
   
    void createarray()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("\nenter a elements: ");
        for(int i=0; i<5; i++)
        {
            arr[i] = sc.nextInt();
        }
    }
}

class Numpay extends NumberData
{
    void display()
    {
        System.out.print("your array is: ");
        for(int i=0; i<5; i++)
        {
            System.out.print(arr[i] + " ");
        }
    }  
   
    void sum()
    {
        int temp=0;
        for(int i=0; i<5; i++)
        {
            temp = temp + arr[i];
        }
        System.out.print("\nSum is: " + temp);
    }
   
    void avg()
    {
        int temp=0;
        for(int i=0; i<5; i++)
        {
            temp = temp + arr[i];
        }
       
        float x = (float)temp / 2;
        System.out.print("\nAverage is: " + x);
    }
   
    void max()
    {
        int m = arr[0];
       
        for(int i=0; i<5; i++)
        {
            if(m < arr[i])
            {
                m = arr[i];
            }
        }
        System.out.print("\nMax is: " + m);
    }
   
    void min()
    {
        int mn = arr[0];
       
        for(int i=0; i<5; i++)
        {
            if(mn > arr[i])
            {
                mn = arr[i];
            }
        }
    }
}

class Program6
{
    public static void main(String args[])
    {
        int ch = Integer.parseInt(args[0]);
       
        Numpay n1 = new Numpay();
       
        switch(ch)
        {
            case 1: n1.createarray();
                    n1.display();
                    break;
                   
            case 2: n1.createarray();
                    n1.sum();
                    break;
                   
            case 3: n1.createarray();
                    n1.avg();
                    break;
                   
            case 4: n1.createarray();
                    n1.max();
                    break;
                   
            case 5: n1.createarray();
                    n1.min();
                    break;
                   
            default : System.out.println("\nenter a less than 6");
        }
    }
}


No comments:

Post a Comment

python programs

1. sum of two number