Lecture 27, CS 302-6, November 4

  1. Review:
    1. Instance Variables

                                                               i.     Declare as private

                                                             ii.     Initialized to ‘zero’ value

1.    objects null, ints zero, etc

                                                            iii.     Emphasize variable scope

    1. Instance Methods

                                                               i.     Accessors

1.    ‘getters’

2.    Boolean methods

                                                             ii.     Mutators

1.    ‘setters’

                                                            iii.     Methods can do both as well – this is just a semantic distinction, not a rule

                                                           iv.     Implicit variable

1.    this

                                                             v.     Explicit variables

                                                           vi.     Emphasize variable scope (again)

  1. Tester Classes
    1. In Java, we don’t ‘execute’ classes.  We just run main methods
    2. Many of our classes won’t have main methods – they’ll only be runnable from some other class.
    3. Think of as ‘driver programs’
    4. General steps to using a class:

                                                               i.     Import

1.    find it

2.    Note – since we are in the same directory, don’t need to do this with our classes.  Example, though, is java.util.Scanner;

                                                             ii.     Declare

1.    <Class> <varName>

                                                            iii.     Construct

1.    make it

2.    <varName>=new <Class>();

                                                           iv.     Use

1.    use it

<varName>.<method>(<params>)

2.    by calling methods from the object

    1. Not so much a ‘do-this then do that’ thing as it is a ‘think about what should be tested’
    2. Unit testing

                                                               i.     Testing parts of a program in isolation, outside the full program.

                                                             ii.     aka making sure each class works as desired.

  1. Constructors
    1. basic format:

public <className>(<type>  <varName>,...){}

                                                               i.     No void/return type

                                                             ii.     Not static

    1. Purpose – initialize instance variables to non-default values
    2. Called when you new the object

                                                               i.     Cannot be called like a normal method [object.constructor() does not work].

    1. Basic form – just set everything to default values
    2. Think of Random – what happens when you pass in a seed?  That's stored in an instance variable

                                                               i.     That's just one example of a constructor that takes params

  1. Default Constructor
    1. If no constructor is explicitly specified (such as in our previous examples), it is as though there is a constructor that takes no parameters, and sets each instance variable to a default (zero) value
    2. As soon as we specify one constructor, the default constructor is no longer valid.
    3. Overloading constructors

                                                               i.     Overloading refers to having more than one method with the same name.

                                                             ii.     This is very common with constructors

1.    It is also possible with other methods

                                                            iii.     If we do this, each one must have unique arguments passed in.

                                                           iv.     Unique is based on type, order, and number

1.    different variable name but everything else the same does not count

                                                             v.     When we new an object, the variables must match the args for one of the constructors

  1. Shortcut for this
    1. As mentioned, we don’t have to use this to refer to instance variables.
    2. Only need to use this when there is potential scope conflict
  2. HW – for Friday – Finish Ch 7