teaching machines

CS1: Lecture 35 – Stopwatch

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:

  • What behaviors should a Stopwatch class support?
  • What state must we track in order for the Stopwatch to do its job?

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:

  • If you are planning on resubmitting a homework, be sure to email me by December 10 to tell me that you have homework 6 finished. I am not going to automatically check everyone’s code to see who has it done. If you do not email me, you are not eligible.

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;
  }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *