Lecture 26, CS 302-6, November 2

  1. Review:
    1. Object Oriented Programming
    2. Objects and Classes

                                                               i.     Example – blueprint/house

    1. Encapsulation

                                                               i.     Public Interface

    1. Instance Methods

                                                               i.     Accessors

                                                             ii.     Mutators

    1. In class examples

                                                               i.     Voting Machine

                                                             ii.     Vending Machine

  1. Instance variables
    1. Variables that allow an object to store its data.

                                                               i.     Declared for the entire class, not for just one method

    1. How to think about instance variables – they store all of the data the object will ever need.

                                                               i.     Need to consider the needs of each method in determining how many/which ones to declare

    1. Think back to the voting machine example – we need to remember how many votes for the democrat and the republican.

                                                               i.     We'll have an instance variable to keep track of each of these.

    1. How to declare?

                                                               i.     Declared in the class, outside any methods.

                                                             ii.     Usually at the top of the class.

                                                            iii.     private <type> <name>; …

    1. Each object stores its own copy of these variables/this data
    2. Definition – 'state' of the object – values stored in instance variables.
    3. Default value – 0, null, etc – until we initialize it
    4. Think back – what does a mutator do?  It changes the state of an object.  So, it changes an instance variable
    5. What does an accessor do?  It gets an instance variable
    6. private vs public

                                                               i.     Since we declare these as private, they cannot be accessed directly by the object.  Must use accessor instead.

    1. Example – VotingMachines.java
  1. Instance Methods
    1. Explicit parameter – declared in method header

                                                               i.     0 or more of them

    1. Implicit parameter – the object itself

                                                               i.     One and only one

    1. this keyword

                                                               i.     Holds reference to the object itself (the implicit parameter)

                                                             ii.     Use to refer to instance variables from within instance methods

1.    this.<variable name>

    1. Example – how implement voteDemocrat?

                                                               i.     For now, we won't consider the undoVotes functionality

1.    this.demVotes++;

    1. Example – how implement getDemVotes?

1.    return this.demVotes;

    1. Where this really matters

                                                               i.     Consider a new method, voteDemocratXTimes

1.    We won't worry about undoVote here

public void voteDemocratXTimes(int x)

{

this.demVotes+=x;

}

                                                             ii.     What if, instead of calling our int x, we called it demVotes?

public void voteDemocratXTimes(int demVotes)

{

this.demVotes+=demVotes;

}

                                                            iii.     Think about variable scope…

  1. HW – for Friday – Circle class

Design a class that represents a circle.  It should store how big the circle is (radius).  It should have methods to set the radius, get the value for the radius, get the value for the area of the circle, and get the value for the circumference of the circle.