CS201 - Spring 2014 - Class 6

  • Exercises

  • admin
       - Move office hours tomorrow: 1:30-3pm
       - Lab tutors

  • main method
       - public: accessible outside of the class
       - static: stand-alone method that does not access/require any state of an object (i.e. access instance variables)
       - void: doesn't return anything
       - main: name of the method
       - String[]: an array of Strings
       - args: the name of the parameter

  • revisit importing
       - A package is a collection of classes
          - often related
       - Any class inside a particular package my use any other class in the package without doing anything special
       - If you want to use a class that is not inside your package, you need to import it
       - To import a class:
          import <package_name>.<class_name>
          
          - For example:
             import java.util.Random

             - imports the Random class from the java.util package
             
             import java.util.Scanner

             - imports the Scanner class from the java.util package

  • Design a stopwatch class
       - What functionality should we have?
          - start
          - stop
          - reset
          - getTime
       - Be specific about the functionality:
          - What happens if we call start twice in a row?
          - What happens if we call stop twice in a row? or without having called start?
          - What happens if we call start then call reset (without calling stop)?
       - What data do we need to store?
          - start time
          - stop time (or accumulated time)
          - whether it's running or not (what happens if we just hit stop twice without using a running variable?)

  • 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?

  • inheritance