Lecture 27, CS 302-7 and 8, March 23

  1. Review:
    1. Instance Variables

                                                               i.     Declare as private

                                                             ii.     Initialized to ‘zero’ value

1.    objects null, ints zero, etc

                                                            iii.     Remember 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

    1. Constructors
  1. Circle Class
    1. As example of constructors
    2. Circle.java
  2. 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: numConstructors>1

                                                               i.     Each one must have unique args (by type and number and order)

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

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

  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 able to run them from some other class.
    3. Think of tester classes 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>(< params >);

                                                           iv.     Use

1.    use it

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

2.    by calling methods from the object

3.    Joke - “An OO surgeon would hand the scalpel to the patient and say: "now perform this operation on yourself!".”

    1. Guidelines for testing - 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.

1.    aka making sure each class works as desired.

    1. CircleTester.java
  1. Shortcut for this
    1. As mentioned, we don’t have to use this to refer to instance variables.

                                                               i.     Only need to when there is potential scope conflict.

    1. Better, in general, just to use this when it applies
  1. HW – for Monday – Finish reading Ch 7