Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access the data of an object. System.out has a println method You do not know how it works What is important is that it does the work you request of it
Classes A class describes a set of objects with the same behavior. Some string objects: "Hello World" "Goodbye” You can invoke the same methods on all strings.
Class vs Object A class is a blue print of a particular classification of objects An object belongs to a class of objects An object is an instance of a class Memory space Class: not allocated Object: allocated Declaration Class: once Object: many times
Building 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
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
Public 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
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); } }
Scope of variables Instance attributes/variables One for each object – dynamically allocated Local variables Parameter variables Static variables and constants Only one copy – belong to the class
Instance and static methods Instance method Belong to the object Called from the name of the object harrysChecking.getBalance() Can call instance/static methods/variables Static method Belong to the class Called from the name of the class BankAccount. showLastAccountNumber() Can only call other static methods/variables
Parameter Passing Pass by value Pass by reference Variables of primitive data types a=2000; harrysChecking.deposit(a); Pass by reference Objects/arrays Checking.transfer(500, tomsChecking);
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
Case Study Cash Register 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
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); } }