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

COMPUTER SOFTWARE APPLICATION - CITS






                      // Displaying account details
                      account.displayAccountDetails();
                  }
              }

           Output:

















           Explanation:

           •  In this program, we define a class BankAccount with attributes accountNumber and balance.
           •  We provide a default constructor for the BankAccount class, which initializes the accountNumber attribute to
              a default value (“0000000000”) and the balance attribute to zero.

           •  In the main method, we create an instance of the BankAccount class using the default constructor.
           •  We call the displayAccountDetails method of the BankAccount class to display the account details.
           Default constructors are invoked  automatically  when an object is created if no other constructor is explicitly
           defined. They initialize the object’s attributes to default values, ensuring that the object is in a consistent state
           upon creation. Default constructors are particularly useful when no specific initialization is required.



           2  Parameterized Constructor

           TASK 1: Salary details of an employee
              //Salary details of an employee
              class Employee {
                  String name;
                  double salary;

                  // Parameterized constructor
                  Employee(String name, double salary) {
                      this.name = name;
                      this.salary = salary;

                  }
                  // Method to display employee details
                  void displayDetails() {
                      System.out.println(“Name: “ + name);
                      System.out.println(“Salary: “ + salary);
                  }



                                                           112

                               CITS : IT & ITES - Computer Software Application - Exercise 97
   122   123   124   125   126   127   128   129   130   131   132