Sunday, 12 March 2023

5. Write an interface called numbers, with a method in Process(int x, int y). Write a class called Sum, in which the method Process finds the sum of two numbers and returns an int value. Write another class called Average, in which the Process method finds the average of the two numbers and returns an int.



import java.util.*;

interface Number
{
    int process(int x,int y);
}

class Sum implements Number
{
    public int process(int x,int y)
    {
        return x+y;
    }
}

class Avg implements Number
{
    public int process(int x,int y)
    {
        return (x+y)/2;
    }
}

class Program5
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
         
        System.out.print("enter value for x: ");
        int x = sc.nextInt();
       
        System.out.print("enter value for y: ");
        int y = sc.nextInt();
       
        Number n = new Sum();
        System.out.println("sum is: " +n.process(x,y));
       
        n = new Avg();
        System.out.println("average is: " + n.process(x,y));
    }
}


No comments:

Post a Comment

python programs

1. sum of two number