CS62 - Spring 2010 - Lecture 2

  • http://www.xkcd.com/676/

  • Admin
       - An apology for running over time last time
       - Will send an e-mail this afternoon
       - TA office hours

  • 1.3 For each pair of classes, indicate which class extends the other:
       - java.lang.Number, java.lang.Double
       - java.lang.Number, java.lang.Integer
       - java.lang.Number, java.lang.Object
       - java.util.Stack, java.util.Vector
       - java.util.Hashtable, java.util.Dictionary

  • Look at http://java.sun.com/j2se/1.4.2/docs/api/java/util/Dictionary.html

  • show StopWatch code
       - what happens if we press start twice before pressing stop?
          - noop (nothing happens, just keeps running)

  • we'd like to change a few things about the current stopwatch class
       - add methods to get the elapsed time in seconds, minutes and hours (rounded down)
       - change the functionality of the start method so as to reset the start time if you press start when the watch is already timing

  • what's the best way to do this?
       - inheritence i.e. "extend"
          - allows us to keep underlying functionality but add and change things we want
          - we inherit (i.e have access to) everything that is declared "public" or "protected" (this goes for instance variables as well as methods)
       - we can simply add new methods for getSeconds, getMinutes and getHours
       - what about changing the start method behavior?
          - we can override the method. This is accomplished by creating a method with the same name and parameters

  • show ModifiedStopWatch code
       - what does the "super" method do?
       - why is the compiler complaining?
          - running and startTime are "private"
          - we can change them to "protected" in StopWatch class, which means they can only be modified by a subclass (and to be clear, can also be modified by any class in the same package, but more on this later)

  • Some inheritence questions
       - Will these compile?
          - ModifiedStopWatch modifiedSW = new ModifiedStopWatch();
          StopWatch stopWatch = (StopWatch)modifiedSW;
          
          - StopWatch stopWatch = new StopWatch();
          ModifiedStopWatch modifiedSW = (ModifiedStopWatch)stopWatch;
       - Will they run properly?
       - Will this compile?
          - ModifiedStopWatch modifiedSW = new ModifiedStopWatch();
          StopWatch stopWatch = (StopWatch)modifiedSW;
          modifiedSW.getMinutes();
          stopWatch.getMinutes();
       - Which classes start method will be called?
          - ModifiedStopWatch modifiedSW = new ModifiedStopWatch();
          StopWatch stopWatch = (StopWatch)modifiedSW;
          stopWatch.start();
       - Which of these casts is valid?
          - Object obj = (Object)modifiedSW;
          - ModifiedStopWatch msw = (ModifiedStopWatch)obj;

  • More comments on OO programming
       - You can think of an object as a container for:
          - values of the instance variables (internal state)
          - methods
       - You can think of the class as a specification for an object:
          - types of instance variables
          - signature of the methods
          - static methods and variables
          - constructors

  • Commenting
       - To quote our governor (and Nike)... just do it!
       - Why do we comment?
       - Using Javadoc (http://java.sun.com/j2se/javadoc/writingdoccomments/)
          - Javadoc is HTML documentation generated from the comments in your code
          - a way for you to communicate the "interface" to other programmers/users
          - /** is the beginning comment delimiter for classes/methods
          - Two basic parts
             - The document comment, which is written in HTML and is the first non-whitespace-line chunk of text
          - block tags. The main ones we'll use are below, but many more online
             - @author
             - @date
             - @param
             - @return
             - @throws
          - We can generate the HTML from the comments using eclipse (or on the command line)
             - In Eclipse: Project->Generate Javadoc...
             - in the left window, select the classes that you want to generate the javadoc for (generally, you should do one project at a time)
             - select the destination (the "doc" folder within the project is a good place)
             - click "finish" (or "next" if you'd like other options)

  • pre and post conditions
       - Anyone seen pre/post conditions before?
       - A precondition (pre) is a fact about the state of affairs before a method is run
       - A postcondition (post) is a fact about the state after the method is run
       - why might this be useful?
          - one way is the "assert" command which allows us to check certain things hold

  • assert command (http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html)
       - assert <boolean_expression>
          - if the statement is true, acts like a noop (nothing happens)
          - otherwise, throws an
       - let's say we wanted to copy contents from one String array to a second, what pre/post conditions would we have and how could we check those with assert?
       - show AssertExamples code
       - What will happen if I run goodTest? badTest?
       - badTest throws a java.lang.ArrayIndexOutOfBoundsException. Is this what we expected?
          - assertions need to be enabled! and by default are not enabled
          - briefly discuss the java virtual machine (JVM)
             - people will save that Java is "portable". What does this mean?
          - to enable, you need to add an additional argument to the java virtual machine
             - in Eclipse: Run->Run Configurations
             - Arguments
             - under "VM arguments" add "-ea" (enable assertions)
             - then select run (this will be saved permanently, but only for this one class)
       - now we get a java.lang.AssertionError (at line 43, where the assertion is)
       - Why is this better?
          - sometimes it can be difficult to track down errors
             - e.g. without the assertions, the error is at line 48 and isn't obvious why this is happening

  • Extendable arrays
       - What's one of the biggest downsides/annoyances with arrays?
          - fixed size: you need to know a priori the size of the arrays
       - Java offers a number of classes that implement extensible arrays (i.e. non-fixed length array-like functionality)
       - How do you think they do it (as always, there is more than one way to do it)?
       - java.util.Vector
          - need to import the class: import java.util.Vector;
             - if it's built in to Java, why do we need to import it?
                - make life easier for us when coding: we can write Vector instead of java.util.Vector everywhere.
          - To create a new vector:
             - Vector v = new Vector();
          - Similarly to arrays:
             - get(int index) is equivalent to "array[index]"
             - set(int index, Object element) is equivalent to array[index] = element;
          - The major addition is:
             - add(Object o), which adds the "o" onto the end of the array, regardless of how many elements are in the array
          - size() returns the number of elements in the Vector