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

COMPUTER SOFTWARE APPLICATION - CITS




           Output:



















           Explanation
           •  In this program, we define a method findMaximum that takes two integers as parameters and returns the
              maximum of the two.
           •  We declare variables a and b in the main method and assign values to them.
           •  We call the findMaximum method and pass a and b as arguments.
           •  Inside the findMaximum method, we use the ternary operator to determine the maximum of the two integers.
           •  The maximum value is returned and stored in the max variable in the main method.

           •  Finally, we print the maximum value to the console.
           This example illustrates how to pass primitive data types, such as integers, to methods in Java. The findMaximum
           method encapsulates the logic for finding the maximum of two integers, which promotes code reusability and
           improves readability. When passing primitive data types to methods, the values of the variables are copied, and
           changes made to the parameters inside the method do not affect the original variables in the calling method.



           2  Passing Objects to Methods

           TASK 1: Employee Salary
              class Employee {
                  String name;
                  double salary;
                  // Constructor to initialize Employee object

                  Employee(String name, double salary) {
                      this.name = name;
                      this.salary = salary;
                  }

              }
              public class ObjectDemo {
                  public static void main(String[] args) {
                      // Creating an Employee object
                      Employee employee = new Employee(“John”, 50000);
                 // Passing the employee object to a method for display

                      displayEmployeeDetails(employee);




                                                           94

                               CITS : IT & ITES - Computer Software Application - Exercise 95
   104   105   106   107   108   109   110   111   112   113   114