Lecture 31, CS 302-7 and 8, April 09

1.      Various things

a.       Exam 2

b.      Program 3

2.      Review:

a.       .equals

b.      Command line arguments

c.       Exam 2 prep

3.      Overriding .equals

a.       We talked about overriding the .toString method

b.      .equals behaves similarly

c.       We can define how we want Java to compare .equals – to see if two string objects are the same

d.      Tools we’ll need –

i.                    instanceof

1.      this says whether or not an object has some type

ii.                   convert generic object to specific type

iii.                 public boolean equals(Object other){

if(other instance of <class>){

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

//do comparisons, return true or false

}

return false;

iv.                 Example – Equaller.java

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

1.      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

b.      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…variable scope J

ix.                 Parameter Variables – same as local variables

  1. Importing packages
    1. What is a package?

                                                               i.     A set of related classes

                                                             ii.     Java standard library contains many

                                                            iii.     Examples we’ve seen:

1.    java.util

2.    java.lang

                                                           iv.     Most of the time, we need to import packages before using classes from them.

1.    Why?  Prevent class name collision – different packages can contain classes with the same name

                                                             v.     Don’t need to import java.lang – it is so common that it’s included by default

    1. How to create/add to a package

                                                               i.     Package packageName; at top of file

                                                             ii.     Stored in folder with name/name/name

1.    corresponds to dots

                                                            iii.     Why can we import the java. ones?  Classpath…

                                                           iv.     In Eclipse – create a package, it takes care of the rest.

1.    Drag and drop

5.      Homework – For Wednesday - Study