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

COMPUTER SOFTWARE APPLICATION - CITS





                  }
              }
           Output:




















           Explanation:
           •  In this program, we define a class Circle with an attribute radius.

           •  We provide a default constructor for the Circle class, which initializes the radius attribute to a default value (1.0
              in this case).
           •  In the main method, we create an instance of the Circle class using the default constructor.
           •  We call the calculateArea method of the Circle class to calculate and display the area of the circle.



           TASK 3: Default Constructor in a Bank Account Class
              // Bank Account using default constructors

              class BankAccount {
                  String accountNumber;
                  double balance;
                  // Default constructor

                  BankAccount() {
                      accountNumber = “0000000000”; // Default account number
                      balance = 0.0; // Initialize balance to zero
                  }
                  // Method to display account details
                  void displayAccountDetails() {

                      System.out.println(“Account Number: “ + accountNumber);
                      System.out.println(“Balance: “ + balance);
                  }
              }

              public class BankAccount_Demo {
                  public static void main(String[] args) {
                      // Creating an instance of BankAccount using the default constructor
                      BankAccount account = new BankAccount();



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