Lecture 31, CS 302-6, November 14

  1. Various things
    1. Program 3 – Iterators

                                                               i.     hasNext, getNext, lookAtNext, rewind

    1. Review session – Tuesday, 4:30-6:00, CS 1289

                                                               i.     Bring last years exam/sample questions, and your questions

1.    We can go through things together, etc – but, I won’t be lecturing

                                                             ii.     Note – I might be a little late, I have a commitment at 4

  1. Review:
    1. Command line arguments
    2. Refactoring
    3. Importing packages
  2. Country.java
  3. A little more on methods/constructors and overloading
    1. Often times, if we define any constructor, we will need to define a default constructor as well

                                                               i.     For example, in our country example

    1. As mentioned – we can overload methods as well as constructors

                                                               i.     Constructor example – Random(), Random(int seed)

                                                             ii.     Method example – random.nextInt() and random.nextInt(int a)

    1. What defines a method – methodName, #params, paramType, paramOrder

                                                               i.     Public vs private, static vs instance, parameter names don’t matter here

  1. A little more on public vs private (scope in Java)

Access Levels Modifier       Class         Package       Subclass       World

public Y Y Y Y

protected Y Y Y N

no modifier Y Y N N

private Y N N N

    1. We really only need to worry about public/private and Class/Package/World for now
    2. Takeaway – in general,

                                                               i.     Instance variables – private

                                                             ii.     Instance methods – public

                                                            iii.     Instance helper methods - private

                                                           iv.     Constructors – public

                                                             v.     Static/class methods – public

                                                           vi.     Static/class variables – private

                                                          vii.     Static/class constants – public

                                                        viii.     Local variables – usually don’t specify

1.    Why?  Because it doesn’t really matter, due to variable scope

                                                           ix.     Parameter Variables – same as local variables

  1. Overriding .equals(Object o)
    1. We talked about overriding the .toString() method
    2. .equals(Object o) behaves similarly
    3. We can define how we want Java to perform .equals – to see if two objects are the same
    4. Tools we’ll need –

                                                               i.     instanceof

1.    This is a keyword

2.    Says whether or not an object has some type

                                                             ii.     Convert generic object to specific type

1.    Every Java object ‘inherits’ some things from the object class

2.    We can convert a generic object to an object of a specific type by using (<Class>) in front of the variable (if we know that object is the right type – see instanceof)

    1. How to implement this?

public boolean equals(Object other){

      if(other instance of <class>){

<class> that=(<class>) other;

//do comparisons, return true or false

}

return false;

}

    1. Example – Equaller.java
  1. Homework – For Wednesday - Study