diff options
Diffstat (limited to 'oop/04-constructors')
| -rw-r--r-- | oop/04-constructors/.gitignore | 1 | ||||
| -rw-r--r-- | oop/04-constructors/Account.java | 47 | ||||
| -rw-r--r-- | oop/04-constructors/AccountTest.java | 23 |
3 files changed, 71 insertions, 0 deletions
diff --git a/oop/04-constructors/.gitignore b/oop/04-constructors/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/oop/04-constructors/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/oop/04-constructors/Account.java b/oop/04-constructors/Account.java new file mode 100644 index 0000000..82875be --- /dev/null +++ b/oop/04-constructors/Account.java @@ -0,0 +1,47 @@ + +public class Account { + + /* Declaring fields - represent state */ + + int accountNumber; + double balance; + + // No argument constructor + public Account() { + System.out.println("No argument constructor"); + } + + // A constructor with an argument + public Account(double initialBalance) { + balance = initialBalance; + System.out.println("Argument constructor"); + } + + /* Declaring methods - represent operations */ + + public void withdraw(double amount) { + if (balance >= amount) { + balance = balance - amount; + } + } + + // Overloaded method + public void deposit(double amount) { + balance += amount; + } + + // Overloaded method + public void deposit(int amount) { + balance += amount; + } + + + public double getBalance() { + return balance; + } + + +// public int getBalance() { +// return 1; +// } +} diff --git a/oop/04-constructors/AccountTest.java b/oop/04-constructors/AccountTest.java new file mode 100644 index 0000000..9774e84 --- /dev/null +++ b/oop/04-constructors/AccountTest.java @@ -0,0 +1,23 @@ + +public class AccountTest { + + /** + * @param args + */ + public static void main(String[] args) { + Account a1 = new Account(); // Instantiation of 'Account' class + Account a2 = new Account(10.0); + + System.out.println(a1.getBalance()); + + a1.deposit(100.0); + System.out.println(a1.getBalance()); + + a1.withdraw(50.0); + System.out.println(a1.getBalance()); + + a2.deposit(100.0); + System.out.println(a2.getBalance()); + } + +} |
