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

COMPUTER SOFTWARE APPLICATION - CITS



           EXERCISE 96 : Return data and Objects from Methods



            Objectives

           At the end of this exercise you shall be able to
           •  know about how to return data and objects from methods in JAVA
           •  develop Java programs to return data and object from  methods.

           Requirements


           Tools/Materials
           •  PC/Laptop  with Windows OS
           •  JDK Software
           •  Text Editor (Visual Studio/Sublime/Notepad)



           Procedure

           Below are examples of Java programs demonstrating returning data and objects from methods:
           TASK 1: Returning the Maximum of Two Numbers
               public class MaxNumberDemo {
                  public static void main(String[] args) {

                      int a = 10;
                      int b = 20;
                      // Calling the method to find the maximum of two numbers
                      int max = findMax(a, b);
                      System.out.println(“Maximum of “ + a + “ and “ + b + “ is: “ + max);

                  }
                  // Method to find the maximum of two numbers
                  public static int findMax(int x, int y) {
                      return (x > y) ? x : y;

                  }
              }
           Output:























                                                           105
   115   116   117   118   119   120   121   122   123   124   125