On 1 July 1882, the Swiss watchmaker Johann A. Hanhart opened a watch shop in Diessenhofen, in north-eastern Switzerland. In 1902, he relocated his business to Schwenningen, southern Germany. In 1924, Hanhart launched its first stopwatch, and a short time later, the product range was extended to include pocket watches and wristwatches. From 1932 onwards, following the death of his father, Wilhelm Julius Hanhart concentrated on manufacturing raw movements. In 1938, the first single-button chronograph with the "Calibre 40" entered series production, pilot’s chronographs followed in 1939.
WARNING: This one might go down in flames or be subject to recall!
class Timer {
long timeStarted;
long timeStopped;
Timer() {
this.reset();
}
void reset() {
this.timeStarted = -1;
this.timeStopped = -1;
}
void start() {
this.timeStarted = System.currentTimeMillis();
}
void stop() {
this.timeStopped = System.currentTimeMillis();
}
boolean isStarted() {
return this.timeStarted != -1;
}
boolean isStopped() {
return this.timeStopped != -1;
}
boolean isRunning() {
return this.isStarted() && !this.isStopped();
}
long elapsedTimeInMs() {
if (this.timeStarted != -1) {
if (this.timeStopped != -1) {
return this.timeStopped - this.timeStarted;
}
else {
return System.currentTimeMillis() - this.timeStarted;
}
}
else {
return -1;
}
}
}
/** FROM: https://en.wikipedia.org/wiki/Stopwatch
*
* A stopwatch is a handheld timepiece designed to measure
* the amount of time elapsed from a particular time when it
* is activated to the time when the piece is deactivated.
*
* The timing functions are traditionally controlled by two
* buttons on the case. Pressing the top button starts the
* timer running, and pressing the button a second time stops
* it, leaving the elapsed time displayed. A press of the
* second button then resets the stopwatch to zero. The second
* button is also used to record split times or lap times.
* When the split time button is pressed while the watch is
* running, the display freezes, allowing the elapsed time to
* that point to be read, but the watch mechanism continues
* running to record total elapsed time. Pressing the split
* button a second time allows the watch to resume display of
* total time.
*/
class Stopwatch {
Timer timer;
Stopwatch() {
this.timer = new Timer();
}
/** topButtonPress:
* Starts timer if timer is not started.
* Stops timer if timer is running.
* Returns:
* 0 if button press causes timer to start.
* Elapsed time in ms otherwise.
*/
long topButtonPress() {
// MISSING CODE!
return -666;
}
/** sideButtonPress:
* Resets timer if timer is not running.
* Returns:
* -1 if button press causes timer to reset.
* Elapsed time in ms otherwise.
*/
long sideButtonPress() {
// MISSING CODE!
return -666;
}
}
class TraditionalStopwatch {
public static void main(String[] args) {
Stopwatch s1 = new Stopwatch();
s1.topButtonPress();
// Long.MAX_VALUE is 9,223,372,036,854,775,807
for (long i=0; i < 1000000000; i++);
long elapsedTime = s1.topButtonPress();
System.out.println(elapsedTime);
}
}
function Timer() {
this.reset();
}
Timer.prototype = {
reset: function() {
this.timeStarted = null;
this.timeStopped = null;
},
start: function() {
this.timeStarted = new Date();
},
stop: function() {
this.timeStopped = new Date();
},
isStarted: function() {
return this.timeStarted != null;
},
isStopped: function() {
return this.timeStopped != null;
},
isRunning: function() {
return this.isStarted() && !this.isStopped();
},
elapsedTimeInMs: function() {
if (this.timeStarted != null) {
if (this.timeStopped != null) {
return this.timeStopped - this.timeStarted;
}
else {
return new Date() - this.timeStarted;
}
}
else {
return undefined;
}
}
};
/** FROM: https://en.wikipedia.org/wiki/Stopwatch
*
* A stopwatch is a handheld timepiece designed to measure
* the amount of time elapsed from a particular time when it
* is activated to the time when the piece is deactivated.
*
* The timing functions are traditionally controlled by two
* buttons on the case. Pressing the top button starts the
* timer running, and pressing the button a second time stops
* it, leaving the elapsed time displayed. A press of the
* second button then resets the stopwatch to zero. The second
* button is also used to record split times or lap times.
* When the split time button is pressed while the watch is
* running, the display freezes, allowing the elapsed time to
* that point to be read, but the watch mechanism continues
* running to record total elapsed time. Pressing the split
* button a second time allows the watch to resume display of
* total time.
*/
function Stopwatch() {
this.timer = new Timer();
}
Stopwatch.prototype = {
/** topButtonPress:
* Starts timer if timer is not started.
* Stops timer if timer is running.
* Returns:
* 0 if button press causes timer to start.
* Elapsed time in ms otherwise.
*/
topButtonPress: function() {
// MISSING CODE!
},
/** sideButtonPress:
* Resets timer if timer is not running.
* Returns:
* -1 if button press causes timer to reset.
* Elapsed time in ms otherwise.
*/
sideButtonPress: function() {
// MISSING CODE!
},
}
var s1 = new Stopwatch();
s1.topButtonPress();
// Number.MAX_SAFE_INTEGER in JavaScript is 2^53 - 1
for (var i=0; i <= 1000000000; i++);
var elapsedTime = s1.topButtonPress();
alert(elapsedTime);
// Java CODE, AP Computer Science A (11 Oct 2016)
// Math with Natural Numbers
class NMath {
final long NOT_NATURAL;
static final long ZERO_FACTORIAL = 1;
NMath(long notNatural) {
this.NOT_NATURAL = notNatural;
}
// Triangle Number
long tri(long n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else if (n == 0) {
return 0;
}
else {
return n + this.tri(n - 1);
}
}
// Factorial
long fac(long n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else if (n == 0) {
return NMath.ZERO_FACTORIAL;
}
else {
return n * this.fac(n - 1);
}
}
}
// Math with Whole Numbers
class ZMath extends NMath {
ZMath() {
super(-1);
}
// Absolute Value
long abs(long n) {
return (n < 0) ? -n : n;
}
}
// Math with Rational Numbers
class QMath extends ZMath {
QMath() {
super();
}
// Triangle Number
long tri(long n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else {
return (n*(n+1))/2;
}
}
}
// NAME: ________________________________________________________
// GIVEN NMath, ZMath, and QMath (on a separate page):
// PART I - QUIZ
// 10 points each: Below each println statement, write the value the statement outputs.
class MathClass {
public static void main(String[] args) {
NMath nMath = new NMath(0);
QMath qMath = new QMath();
System.out.println(qMath.abs(qMath.NOT_NATURAL));
// ANSWER:_______________
System.out.println(nMath.tri(3));
// ANSWER:_______________
System.out.println(qMath.tri(100));
// ANSWER:_______________
System.out.println(qMath.fac(5));
// ANSWER:_______________
System.out.println(qMath.fac(-5));
// ANSWER:_______________
System.out.println(nMath.tri(-4));
// ANSWER:_______________
System.out.println(qMath.tri(-5));
// ANSWER:_______________
}
}
/* PART II – BONUS QUESTION
// 5 BONUS POINTS: Which executes faster: nMath.tri or qMath.tri?
// 5 BONUS POINTS: Why? (Use the other side of the paper if necessary.)
*/
// JavaScript CODE, AP Computer Science Principles (11 Oct 2016):
// Math with Natural Numbers
function NMath(notNatural) {
this.NOT_NATURAL = notNatural;
}
NMath.prototype = {
ZERO_FACTORIAL: 1,
// Triangle Number
tri: function(n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else if (n == 0) {
return 0;
}
else {
return n + this.tri(n - 1);
}
},
// Factorial
fac: function(n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else if (n == 0) {
return this.ZERO_FACTORIAL;
}
else {
return n * this.fac(n - 1);
}
},
}
// Math with Whole Numbers
function ZMath() {
NMath.call(this, -1);
};
ZMath.prototype = Object.create(NMath.prototype);
// Absolute Value
ZMath.prototype.abs = function(n) {
return (n < 0) ? -n : n;
}
// Math with Rational Numbers
function QMath() {
ZMath.call(this);
}
QMath.prototype = Object.create(ZMath.prototype);
// Triangle Number
QMath.prototype.tri = function(n) {
if (n < 0) {
return this.NOT_NATURAL;
}
else {
return (n*(n+1))/2;
}
}
// NAME: ________________________________________________________
// GIVEN NMath, ZMath, and QMath (on a separate page) and the following:
var nMath = new NMath(0);
var qMath = new QMath();
// PART I - QUIZ
// 10 points each: Below each statement, write the value the statement outputs.
console.log(qMath.abs(qMath.NOT_NATURAL));
console.log(nMath.tri(3));
console.log(qMath.tri(100));
console.log(qMath.fac(5));
console.log(qMath.fac(-5));
console.log(nMath.tri(-4));
console.log(qMath.tri(-5));
/* PART II – BONUS QUESTION
// 5 BONUS POINTS: Which executes faster: nMath.tri or qMath.tri?
// 5 BONUS POINTS: Why?
*/
This is an example of a Java solution to Exercise[20].
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* The class Timer and its constructor may be
* used to produces instances of objects of
* type Timer. An object of type Timer has
* methods start() and stop() that start
* and stop a timer, a method called reset
* that resets a timer, and a method called
* elaspedTimeInMs that returns an amount of
* elapsed time. If a timer has been started
* but not stopped, then elapsedTimeInMs
* returns the amount of time (in milliseconds)
* that has elapsed since the timer was started.
* If a timer has been started and stopped,
* then elapsedTimeInMs returns the amount of
* time that elapsed between the time the
* timer was started and stopped.
*/
class Timer {
long timeStarted;
long timeStopped;
Timer() {
this.reset();
}
void reset() {
this.timeStarted = 0;
this.timeStopped = 0;
}
void start() {
this.timeStarted = System.currentTimeMillis();
}
void stop() {
this.timeStopped = System.currentTimeMillis();
}
long elapsedTimeInMs() {
if (this.timeStarted != 0) {
if (this.timeStopped != 0) {
return this.timeStopped - this.timeStarted;
}
else {
return System.currentTimeMillis() - this.timeStarted;
}
}
else {
return -1;
}
}
}
/**
* ProgramController is a class that may be
* used to control the execution of a Java
* program. ProgramController objects have
* the following methods:
*
* pause is a method defined in terms
* of a parameter called timeInMs that
* specifies the number of milliseconds
* that the Java program should pause
* and do nothing before resuming. The
* method does not return a value.
*
* quit is a method that, when called, causes
* the Java program to terminate. The
* method is not defined in terms of any
* parameters and does not return a value.
*/
class ProgramController {
/*
* pause does nothing for timeInMs millisends
* before returning control to the code that
* called pause. Internally, pause uses a
* Timer object to "spin" in a trivial while
* loop the specified amount of time before
* completing.
*/
void pause(long timeInMs) {
// Previously missing code starts here:
Timer timer = new Timer();
timer.start();
// Previously missing code ends here.
while (timer.elapsedTimeInMs() < timeInMs);
}
/*
* Calling quit makes the Java program
* terminate.
*/
void quit() {
System.exit(0);
}
}
/**
* This is an extremely scaled-down sketching canvas; with it you
* can only scribble thin black lines. For simplicity the window
* contents are never refreshed when they are uncovered.
*
* This implementation of TrivialSketcher and the corresponding
* code in the static method QuickSketch.main below are based on
* code found at:
*
* http://cs.lmu.edu/~ray/notes/javagraphics/
*/
class TrivialSketcher extends JPanel {
/**
* Keeps track of the last point to draw the next line from.
*/
private Point lastPoint;
/**
* Constructs a panel, registering listeners for the mouse.
*/
public TrivialSketcher() {
// When the mouse button goes down, set the current point
// to the location at which the mouse was pressed.
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
lastPoint = new Point(e.getX(), e.getY());
}
});
// When the mouse is dragged, draw a line from the old point
// to the new point and update the value of lastPoint to hold
// the new current point.
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.drawLine(lastPoint.x, lastPoint.y, e.getX(), e.getY());
lastPoint = new Point(e.getX(), e.getY());
g.dispose();
}
});
}
}
public class QuickSketch {
public static void main(String[] args) {
// Display the sketcher
JFrame frame = new JFrame("Ten Second Sketch");
frame.getContentPane().add(
new TrivialSketcher(), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
// The final countdown...
ProgramController controller = new ProgramController();
controller.pause(10000);
controller.quit(); // The end!
}
}