Lecture 29, CS 302-7 and 8, March 28

  1. Various things
    1. Program 3

                                                               i.     How to get started – first threshold (pop up window)

                                                             ii.     You know everything you need to get started now

                                                            iii.     Randomness – not as strict as previous programs

1.    Have fun with it

    1. Exam 2

                                                               i.     Structure:

1.    Part 1 – Write a whole program

2.    Part 2 - Using instantiable classes

3.    Part 3 – Coding an instantiable class

4.    Part 4 – Arrays and Arraylists

                                                             ii.     How to prepare?

1.    Test your code in eclipse

  1. Review:
    1. Constructors
    2. Steps to testing

                                                               i.     Plan

                                                             ii.     Do

                                                            iii.     Test

    1. Object Oriented Thinking
    2. Parallel Arrays
    3. Overriding toString
    4. Helper Instance Methods

                                                               i.     Note about this – you can use this.method

  1. References recap
    1. Reference vs object
    2. Stack vs heap
    3. Two variables can have the same reference
    4. Object vs primitive
    5. Null

                                                               i.     Null string vs “” string (empty string)

    1. This

                                                               i.     Java doesn’t always require us to use it, but we can

  1. .equals vs == reminders
    1. .equals compares the objects the references are pointing to
    2. == compares what’s stored by the variable, be it a reference or a primitive
    3. use == to test whether an object is null

                                                               i.     if you try to call .equals from null object, runtime error (null pointer)

  1. Class members
    1. Definition –something defined at the Class (not object) level
    2. Think of these as some property of the class, shared across all object instances
    3. We declare class members using the static keyword

                                                               i.     Why static?  Leftover from C languages…doesn’t really relate to Java

    1. You access class members with <ClassName>.<memberName>;
    2. You do not use this to refer to class members
  1. Class Variables
    1. These are often referred to as static variables
    2. Usually private
    3. How to declare:

private static <type> <name> = <value>;

    1. Shared by all instances
    2. Examples – next ID, front of the line, etc
    3. Think of these as existing somewhere in heap, outside of objects
  1. Class Constants
    1. Often referred to as static constants
    2. Usually public
    3. How to declare:

public static final <type> <NAME> = <value>;

    1. Example – Math.PI
    2. As opposed to instance constants

                                                              i.     Which would be defined in a method, and not static

    1. <Class>.<NAME> from calling class to use these – eg System.out.println(Math.PI);
  1. Class Methods
    1. Often referred to as static methods
    2. Usually public
    3. How to declare:

                                                               i.     public static <returnType> <name>(<args>){}

                                                             ii.     These are the methods we’ve mostly been working with up until this point!

    1. Note – these cannot directly access instance members – since they are outside of instances.
    2. Use – Class.method from calling class.

                                                               i.     Example – Math.pow(a,b);

    1. Example – GroceryStore.java, Aisle.java

                                                               i.     Note – we don’t use this to access these.

                                                             ii.     Why?  Because they are not on the object

  1. Merging driver class with instantiable class
    1. You don’t actually need a separate tester class
    2. Can put main method in class with other stuff – it is just another static method