ALGORITHMS

Document Sample
ALGORITHMS Powered By Docstoc
					Object Oriented Programming



  CSC 171 FALL 2001
    LECTURE 11
                History: ENIAC
  Electronic Numerical Integrator and
                       Computer
1943 – 1947 Work on
    ENIAC at the Univ. of
    Pennsylvania
John Mauchly &
J. Presper Eckert.
The world's first electronic
    digital computer was
    developed to compute
    World War II ballistic
    firing tables.
          Computer Science
   What is computer science?
        Computer Science
“fundamentally, computer science is the
  science of abstraction – creating the right
  model for a problem and devising the
  appropriate mechanizable technique to
  solve it.”
                       - A. Aho and J. Ullman
         Acts of mind - Locke
   “The acts of the mind, wherein it exerts its
    power over simple ideas, are chiefly these
    three:
             Aggregation
1. Combining several simple ideas into one
  compound one, and thus all complex ideas
  are made
             Comparision
2. The second is brining two ideas, where
    simple or complex, together, and setting
    them by one another so as to take aview of
    them at once,without uniting them into
    one, by which it gets all its ideas of
    relations.
               Abstraction
3. The third is separating them from all other
  ideas that accompany them in their real
  existence: this is called abstraction, and thus
  all general ideas are made.”

                               John Locke,
                       An Essay Concerning
                Human Understanding, (1690)
           Object Orientation
   Objects are the means of
    aggregation and
    abstraction,
    in an OO programming language


 Aggregation – bundling data elements
 Abstraction - inheritance
             Data Encapulation
   OOP encapulates
    – data
    – behavior
                  Interfaces
   We define systems in terms of many objects
    – Each type of object has certain behaviors
        methods

    – Each object has certain data
        Instance variables


   Objects communicate with other via
    well defined interfaces
           Information Hiding
When we design objects which communicate
 exclusively via interfaces -
   we insulate (abstract) the outward functionality
   from the inward implementation.


This insulation is the principle of information hiding
  – the inward details of the implementation are
  hidden from the outside objects
Benefits of Information Hiding
 Information hiding promotes program
  modifiability.
 Clients are not required to know the
  internals of a class in order to use it.
 So, if the class changes inside, then the
  client need not be changed
 The client and the object are said to be
  “loosely coupled”
Example – A bank account
A specific instance
Multiple instances
     Bank Account - behaviors
public class BankAccount
{
  private double balance;

    public BankAccount() {   balance = 0; }
    public BankAccount(double initialBalance){
      balance = initialBalance;
    }
    public void deposit(double amount){
       balance = balance + amount;
    }
    public void withdraw(double amount){
       balance = balance - amount;
    }

    public double getBalance(){return balance;}
}
                            this
 Java conserves storage by maintaining only one
  copy of each method per class
 The same method is invoked by every object
 Every object has its own copy of its instance
  variables
 Every object, by default has a reference to itself
    – “this” is the name of every object’s reference to itself
           Bank Account - this
public class BankAccount
{
   private double balance;

    public BankAccount() {   this.balance = 0; }
    public BankAccount(double balance){
       this.balance = balance;
    }
    public void deposit(double amount){
       this.balance = this.balance + amount;
    }
    public void withdraw(double amount){
       this.balance = this.balance - amount;
    }

    public double getBalance(){
        return this.balance;}
}
    Sometimes, classes share
 Consider a variation of the BankAccount
public class BankAccount {
  ....
  private double balance;
  private int accountNumber;
    // we want to assign sequential numbers
}
Sharing data between objects
      of the same class
  We want to set the account number automatically
public class BankAccount {
  private double balance;
  private int accountNumber;
  private int lastAssignedNumber = 0; //Will this work?
public BankAccount() {
   lastAssignedNumber++;
   accountNumber = lastAssignedNumber; //???????
}
       Static/class variables
 We don’t want each instance of the class to
  have its own value for lastAssignedNumber
 We need to have a single value that is the
  same for the entire class.
 These are called class or static variables
Sharing data between objects
      of the same class
  We want to set the account number automatically
public class BankAccount {
  private double balance;
  private int accountNumber;
  private static int lastAssignedNumber = 0;
public BankAccount() {
   lastAssignedNumber++;
   accountNumber = lastAssignedNumber;
}
                       Finally
   Some types of variables are fixed constants, that
    we do not want to change
   Like conversion factors
   We can use the keyword “final” to prevent
    changes
   So we have a “constant variable”
   Sort of like
    – “jumbo shrimp”
    – “freezer burn”
       Classes share constants
 Consider a variation of the BankAccount
public class Converter {

    private final static double miles2km = 0.6;

}

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:7/21/2012
language:
pages:26