CS 145 Lecture 33 – Stopwatch
Dear students,
As we saw 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. We will use it to time a few operations:
- a words-per-minute calculator
- the amount of time it takes to brute force crack a password
- a competition to type in the alphabet backward
Here’s your TODO to complete before we meet again:
- Two more interviews happen this week, but thankfully they are on Tuesday and Thursday. My office hours will not be disrupted. However, we will not have lab on Tuesday. I will hold a few makeup hours from 10-11:50 on Tuesday.
- Please see the thread Common Homework 6 Issues on Piazza when encountering issues on the homework.
- For lab 11, we will hold the 2017 SplatBot Splat-Down. You must implement a splat bot (which we will demo in class, but expect a more formal specification soon). You will have between now and the end of your lab section to beat RobotRandom for checkpoint 1 and RobotLefty for checkpoint 2. Folks who beat RobotXJ9000 best 3 out of 5 will be awarded 5 bonus Blugolds. You will also be invited to pit your robots against each other.
See you next class!
Sincerely,
P.S. Here’s the code we wrote together…
Stopwatch.java
package lecture1205; public class Stopwatch { private long startedAt; private long stoppedAt; private boolean isRunning; public Stopwatch() { isRunning = false; } public void start() { startedAt = System.currentTimeMillis(); isRunning = true; } public double stop() { if (!isRunning) { throw new BadStopwatchStateException(); } else { stoppedAt = System.currentTimeMillis(); return (stoppedAt - startedAt) / 1000.0; } } }
BadStopwatchStateException.java
package lecture1205; public class BadStopwatchStateException extends RuntimeException { }
Password.java
package lecture1205; 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 = "foobag"; public static final String SECRET8 = "splatbot"; }
BruteForcer.java
package lecture1205; public class BruteForcer { public static void main(String[] args) { Stopwatch timer = new Stopwatch(); boolean foundIt = false; timer.start(); for (char l1 = 'a'; !foundIt && l1 <= 'z'; ++l1) { for (char l2 = 'a'; !foundIt && l2 <= 'z'; ++l2) { for (char l3 = 'a'; !foundIt && l3 <= 'z'; ++l3) { for (char l4 = 'a'; !foundIt && l4 <= 'z'; ++l4) { for (char l5 = 'a'; !foundIt && l5 <= 'z'; ++l5) { for (char l6 = 'a'; !foundIt && l6 <= 'z'; ++l6) { for (char l7 = 'a'; !foundIt && l7 <= 'z'; ++l7) { for (char l8 = 'a'; !foundIt && l8 <= 'z'; ++l8) { String password = "" + l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8; if (Password.SECRET8.equals(password)) { System.out.println(password); foundIt = true; } } } } } } } } } double time = timer.stop(); System.out.println(time); } }