Solution: Constructors
Completion requirements
1 public class Account { 2 private static int cntAcoountNo = 400000; // static variable, class-wide usable 3 4 private int accountNumber; 5 private double balance; 6 7 Account(double initialCredit) { // Definition of parameterized constructor 8 accountNumber = ++cntAcoountNo; 9 balance = initialCredit; 10 } 11 public int getAccountNumber() { 12 return accountNumber; 13 } 14 public double getBalance() { 15 return balance; 16 } 17 }
1 public class Main { 2 public static void main(String[] args) { 3 Account account1 = new Account(50); // Creation of objects of the class Account 4 Account account2 = new Account(100); // using paramized constructor 5 Account account3 = new Account(0); 6 7 System.out.println("Account No.: " + account1.getAccountNumber() + "\tBalance: " + account1.getBalance()); 8 System.out.println("Account No.: " + account2.getAccountNumber() + "\tBalance: " + account2.getBalance()); 9 System.out.println("Account No.: " + account3.getAccountNumber() + "\tBalance: " + account3.getBalance()); 10 } 11 }
Last modified: Saturday, 18 May 2019, 12:45 PM