Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.

Similar presentations


Presentation on theme: "Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access."— Presentation transcript:

1 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

2 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.

3 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

4 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

5 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

6 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

7 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); } }  

8 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

9 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

10 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);

11 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

12 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

13 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 "Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access."

Similar presentations


Ads by Google