Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.

Similar presentations


Presentation on theme: "Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data."— Presentation transcript:

1 Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data Types (ADTs) “blue-prints” to build objects

2 Defining ADT classes class attributes are normally encapsulated (i.e. private) class methods are normally public avoid using input/output inside the methods of a class static attributes and methods class constants

3 class BankAccount { private double balance; private int accountNumber; private static int lastAssignedNumber; //automatically initialized with zero public static final double OVERDRAFT_FEE=5; // constant /*overloaded constructors */ public BankAccount() { balance=0; lastAssignedNumber++; accountNumber= lastAssignedNumber; } public BankAccount(double initialBalance) { balance= initialBalance; lastAssignedNumber++; accountNumber= lastAssignedNumber; } public static int showLastAccountNumber(){ return lastAssignedNumber; } public void deposit(double amount) { double newBalance=balance+amount;//newBalance and amount are local variables balance=newBalance; } public void withdraw(double amount) {// a mutator method double newBalance=balance-amount; balance=newBalance; } Case Study

4 public double getBalance() { // an accessor method return balance; } public void transfer (double amount, BankAccount other) { // a side effect balance=balance-amount; other.balance=other.balance+amount; } } /* A class to test the BankAccount class */ public class BankAccountTest { public static void main (String[] args) { BankAccount harrysChecking=new BankAccount(); int a=100; BankAccount tomsChecking=new BankAccount(a); a=2000; harrysChecking.deposit(a); harrysChecking.withdraw(500); harrysChecking.transfer(500, tomsChecking); System.out.println(harrysChecking.getBalance()); System.out.println(tomsChecking.getBalance()); System.out.println(BankAccount. showLastAccountNumber()); System.out.println(BankAccount. OVERDRAFT_FEE); } }

5 Scope of variables Instance attributes/variables Local variables Parameter variables Static variables and constants

6 Parameter Passing Pass by value – Variables of primitive data types a=2000; harrysChecking.deposit(a); Pass by reference – Objects/arrays Checking.transfer(500, tomsChecking);

7 Other things to consider… Constructors – Default constructor Used only to initialize the encapsulated variables to the default values (0, false, null) Accessors and mutators – “Getters” and “setters” Static methods – Utility methods or – Used to handle static variables

8 public class CashRegister{ public CashRegister() { purchase = 0; payment = 0; } public void recordPurchase(double amount) { double total = purchase + amount; purchase = total; } public void enterPayment(double amount) { payment = amount; } public double giveChange() { double change = payment - purchase; purchase = 0; payment = 0; return change; } private double purchase; private double payment; } Case Study Cash Register

9 public class CashRegisterTester{ public static void main(String[] args) { CashRegister register = new CashRegister(); register.recordPurchase(29.50); register.recordPurchase(9.25); register.enterPayment(50); double change = register.giveChange(); System.out.println(change); } }


Download ppt "Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data."

Similar presentations


Ads by Google