Page 140 - CTS - CSA TP - Volume 2
P. 140

COMPUTER SOFTWARE APPLICATION - CITS




















           Explanation:
           •  In this program, we have a class SumCalculator with three overloaded methods named sum.

           •  The first method sums two integers.
           •  The second method sums three integers.
           •  The third method sums an array of integers.
           •  In the main method, we call each of the overloaded methods to calculate the sum of two numbers, three
              numbers, and an array of numbers.


           TASK 4: Finding Maximum
           // To find the Maximum using Overloaded Methods
           public class MaxFinder {
           // Method to find the maximum of two integers
           public static int max(int a, int b) {
           return (a > b) ? a : b;
           }
           // Method to find the maximum of three integers
           public static int max(int a, int b, int c) {
           return max(max(a, b), c); // Using recursion to find the maximum
           }
           public static void main(String[] args) {
           int num1 = 15;
           int num2 = 20;
           int num3 = 10;
           // Find maximum using overloaded methods
           System.out.println(“Maximum of two numbers: “ + max(num1, num2));
           System.out.println(“Maximum of three numbers: “ + max(num1, num2, num3));
           }
           }
           Output:

















                                                           125

                               CITS : IT & ITES - Computer Software Application - Exercise 98
   135   136   137   138   139   140   141   142   143   144   145