teaching machines

CS1: Lecture 35 – Stopwatch

December 4, 2019 by . Filed under cs1, fall 2019, lectures.

Dear students,

As with methods, a primary benefit of object-oriented programming is code reuse. We can create a utility and use it over and over again, in many contexts. We will do just that by designing a Stopwatch class.

Stopwatch

We will use our stopwatch to time a few operations:

  1. the amount of time it takes to brute force crack a password
  2. a competition to type in the alphabet backward (zyxwvutsrqponmlkjihgfedcba)

But first, let’s discuss what this Stopwatch should look like by answering these questions:

When we model an object, we are tempted to give it every feature imaginable. We humans are enumerators. Completionists. However, this sort of behavior is the undoing of many a project. It’s better to ship a product that does one task well than ten tasks poorly. (Even better is to do ten tasks well. But that’s usually not one of our options.) Modern software development follows the principle of YAGNI—”you ain’t gonna need it.” The spirit of this aphorism is to reign in your dreams to just the required functionality, at least initially. Don’t write code unless it’s meeting a felt need.

These two ideas of making code reusable and not writing what we don’t need for the task at hand can be at odds with each other. Our inventions often start simple, but then we add new features as it gets used in other contexts. At some point our beautiful creation becomes an unwieldy monster.

TODO

Here’s your TODO list for next time:

See you next class!

Sincerely,

P.S. It’s time for a haiku!

Ev’rything’s a nail
When you have just a hammer
We have just a clock

P.P.S. Here’s the code we wrote together…

Main.java

package lecture1204.cs145;

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Stopwatch stopwatch = new Stopwatch();

    stopwatch.start();

    String alphabet = "zyxwvutsrqponmlkjihgfedcba";
    Scanner in = new Scanner(System.in);
    System.out.print("> ");
    String attempt = in.nextLine();

    if (attempt.equals(alphabet)) {
      System.out.println("Victory!");
    } else {
      System.out.println("Uh oh!");
    }
//    String password = crack();
//    System.out.println(password);

    stopwatch.stop();
    System.out.println(stopwatch.getElapsedSeconds());
  }

  public static String crack() {
    for (char a = 'a'; a <= 'z'; ++a) {
      for (char b = 'a'; b <= 'z'; ++b) {
        for (char c = 'a'; c <= 'z'; ++c) {
          for (char d = 'a'; d <= 'z'; ++d) {
            for (char e = 'a'; e <= 'z'; ++e) {
              for (char f = 'a'; f <= 'z'; ++f) {
                for (char g = 'a'; g <= 'z'; ++g) {
                  for (char h = 'a'; h <= 'z'; ++h) {
                    String password = "" + a + b + c + d + e + f + g + h;
                    if (Password.SECRET8.equals(password)) {
                      return password;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    return null;
  }
}

Password.java

package lecture1204.cs145;

public class Password {
  public static final String SECRET2 = "of";
  public static final String SECRET3 = "tri";
  public static final String SECRET4 = "slar";
  public static final String SECRET5 = "three";
  public static final String SECRET6 = "purple";
  public static final String SECRET7 = "bizness";
  public static final String SECRET8 = "zimbabwe";
}

Stopwatch.java

package lecture1204.cs145;

public class Stopwatch {
  private long startTime;
  private long stopTime;

  public Stopwatch() {

  }

  public void start() {
    startTime = System.currentTimeMillis();
  }

  public void stop() {
    stopTime = System.currentTimeMillis();
  }

  public void reset() {
  }

  public double getElapsedSeconds() {
    double seconds = (stopTime - startTime) / 1000.0;
    return seconds;
  }
}

Main.java

package lecture1204.cs148;

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Stopwatch stopwatch = new Stopwatch();

    stopwatch.start();

//    String password = crack();
//    System.out.println(password);

    String alphabet = "zyxwvutsrqponmlkjihgfedcba";
    Scanner in = new Scanner(System.in);
    System.out.print("> ");
    String line = in.nextLine();
    if (line.equals(alphabet)) {
      System.out.println("Victory!");
    } else {
      System.out.println("Failure...");
    }

    stopwatch.stop();

    System.out.println(stopwatch.getElapsedSeconds());
  }

  public static String crack() {
    for (char a = 'a'; a <= 'z'; ++a) {
      for (char b = 'a'; b <= 'z'; ++b) {
        for (char c = 'a'; c <= 'z'; ++c) {
          for (char d = 'a'; d <= 'z'; ++d) {
            for (char e = 'a'; e <= 'z'; ++e) {
              for (char f = 'a'; f <= 'z'; ++f) {
                for (char g = 'a'; g <= 'z'; ++g) {
                  for (char h = 'a'; h <= 'z'; ++h) {
                    String password = "" + a + b + c + d + e + f + g + h;
                    if (password.equals(Password.SECRET8)) {
                      return password;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    return null;
  }
}

Password.java

package lecture1204.cs148;

public class Password {
  public static final String SECRET2 = "of";
  public static final String SECRET3 = "tri";
  public static final String SECRET4 = "slar";
  public static final String SECRET5 = "three";
  public static final String SECRET6 = "purple";
  public static final String SECRET7 = "bizness";
  public static final String SECRET8 = "zimbabwe";
}

Stopwatch.java

package lecture1204.cs148;

public class Stopwatch {
  private long startTime;
  private long stopTime;

  public Stopwatch() {
  }

  public void start() {
    startTime = System.currentTimeMillis();
  }

  public void stop() {
    stopTime = System.currentTimeMillis();
  }

  public double getElapsedSeconds() {
    return (stopTime - startTime) / 1000.0;
  }
}