Examples of smaller Java programs that do various interesting things.

Stopwatch.html 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <!--NewPage-->
  3. <HTML>
  4. <HEAD>
  5. <!-- Generated by javadoc (build 1.5.0_04) on Mon Jun 19 13:07:01 CDT 2006 -->
  6. <TITLE>
  7. Stopwatch
  8. </TITLE>
  9. <META NAME="keywords" CONTENT="Stopwatch class">
  10. <LINK REL ="stylesheet" TYPE="text/css" HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/stylesheet.css" TITLE="Style">
  11. <SCRIPT type="text/javascript">
  12. function windowTitle()
  13. {
  14. parent.document.title="Stopwatch";
  15. }
  16. </SCRIPT>
  17. <NOSCRIPT>
  18. </NOSCRIPT>
  19. </HEAD>
  20. <BODY BGCOLOR="white" onload="windowTitle();">
  21. <!-- ========= START OF TOP NAVBAR ======= -->
  22. <A NAME="navbar_top"><!-- --></A>
  23. <A HREF="Stopwatch.html#skip-navbar_top" title="Skip navigation links"></A>
  24. <TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
  25. <TR>
  26. <TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
  27. <A NAME="navbar_top_firstrow"><!-- --></A>
  28. <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
  29. <TR ALIGN="center" VALIGN="top">
  30. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
  31. <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
  32. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/class-use/Stopwatch.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>
  33. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
  34. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
  35. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
  36. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
  37. </TR>
  38. </TABLE>
  39. </TD>
  40. <TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
  41. </EM>
  42. </TD>
  43. </TR>
  44. <TR>
  45. <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  46. &nbsp;PREV CLASS&nbsp;
  47. &nbsp;NEXT CLASS</FONT></TD>
  48. <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  49. <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/index.html?Stopwatch.html" target="_top"><B>FRAMES</B></A> &nbsp;
  50. &nbsp;<A HREF="Stopwatch.html" target="_top"><B>NO FRAMES</B></A> &nbsp;
  51. &nbsp;<SCRIPT type="text/javascript">
  52. <!--
  53. if(window==top) {
  54. document.writeln('<A HREF="allclasses-noframe.html"><B>All Classes</B></A>');
  55. }
  56. //-->
  57. </SCRIPT>
  58. <NOSCRIPT>
  59. <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/allclasses-noframe.html"><B>All Classes</B></A></NOSCRIPT>
  60. </FONT></TD>
  61. </TR>
  62. <TR>
  63. <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
  64. SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;<A HREF="Stopwatch.html#field_summary">FIELD</A>&nbsp;|&nbsp;<A HREF="Stopwatch.html#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="Stopwatch.html#method_summary">METHOD</A></FONT></TD>
  65. <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
  66. DETAIL:&nbsp;<A HREF="Stopwatch.html#field_detail">FIELD</A>&nbsp;|&nbsp;<A HREF="Stopwatch.html#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="Stopwatch.html#method_detail">METHOD</A></FONT></TD>
  67. </TR>
  68. </TABLE>
  69. <A NAME="skip-navbar_top"></A>
  70. <!-- ========= END OF TOP NAVBAR ========= -->
  71. <HR>
  72. <!-- ======== START OF CLASS DATA ======== -->
  73. <H2>
  74. Class Stopwatch</H2>
  75. <PRE>java.lang.Object
  76. <IMG SRC="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/resources/inherit.gif" ALT="extended by "><B>Stopwatch</B>
  77. </PRE>
  78. <HR>
  79. <DL>
  80. <DT><PRE>public class <B>Stopwatch</B><DT>extends java.lang.Object</DL>
  81. </PRE>
  82. <P>
  83. A class to measure time elapsed.
  84. <P>
  85. <P>
  86. <HR>
  87. <P>
  88. <!-- =========== FIELD SUMMARY =========== -->
  89. <A NAME="field_summary"><!-- --></A>
  90. <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
  91. <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
  92. <TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
  93. <B>Field Summary</B></FONT></TH>
  94. </TR>
  95. <TR BGCOLOR="white" CLASS="TableRowColor">
  96. <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
  97. <CODE>static&nbsp;double</CODE></FONT></TD>
  98. <TD><CODE><B><A HREF="Stopwatch.html#NANOS_PER_SEC">NANOS_PER_SEC</A></B></CODE>
  99. <BR>
  100. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
  101. </TR>
  102. </TABLE>
  103. &nbsp;
  104. <!-- ======== CONSTRUCTOR SUMMARY ======== -->
  105. <A NAME="constructor_summary"><!-- --></A>
  106. <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
  107. <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
  108. <TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
  109. <B>Constructor Summary</B></FONT></TH>
  110. </TR>
  111. <TR BGCOLOR="white" CLASS="TableRowColor">
  112. <TD><CODE><B><A HREF="Stopwatch.html#Stopwatch()">Stopwatch</A></B>()</CODE>
  113. <BR>
  114. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
  115. </TR>
  116. </TABLE>
  117. &nbsp;
  118. <!-- ========== METHOD SUMMARY =========== -->
  119. <A NAME="method_summary"><!-- --></A>
  120. <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
  121. <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
  122. <TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
  123. <B>Method Summary</B></FONT></TH>
  124. </TR>
  125. <TR BGCOLOR="white" CLASS="TableRowColor">
  126. <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
  127. <CODE>&nbsp;void</CODE></FONT></TD>
  128. <TD><CODE><B><A HREF="Stopwatch.html#start()">start</A></B>()</CODE>
  129. <BR>
  130. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start the stop watch.</TD>
  131. </TR>
  132. <TR BGCOLOR="white" CLASS="TableRowColor">
  133. <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
  134. <CODE>&nbsp;void</CODE></FONT></TD>
  135. <TD><CODE><B><A HREF="Stopwatch.html#stop()">stop</A></B>()</CODE>
  136. <BR>
  137. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stop the stop watch.</TD>
  138. </TR>
  139. <TR BGCOLOR="white" CLASS="TableRowColor">
  140. <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
  141. <CODE>&nbsp;double</CODE></FONT></TD>
  142. <TD><CODE><B><A HREF="Stopwatch.html#time()">time</A></B>()</CODE>
  143. <BR>
  144. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elapsed time in secods.</TD>
  145. </TR>
  146. <TR BGCOLOR="white" CLASS="TableRowColor">
  147. <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
  148. <CODE>&nbsp;long</CODE></FONT></TD>
  149. <TD><CODE><B><A HREF="Stopwatch.html#timeInNanoseconds()">timeInNanoseconds</A></B>()</CODE>
  150. <BR>
  151. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elapsed time in nanoseconds.</TD>
  152. </TR>
  153. <TR BGCOLOR="white" CLASS="TableRowColor">
  154. <TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
  155. <CODE>&nbsp;java.lang.String</CODE></FONT></TD>
  156. <TD><CODE><B><A HREF="Stopwatch.html#toString()">toString</A></B>()</CODE>
  157. <BR>
  158. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
  159. </TR>
  160. </TABLE>
  161. &nbsp;<A NAME="methods_inherited_from_class_java.lang.Object"><!-- --></A>
  162. <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
  163. <TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
  164. <TH ALIGN="left"><B>Methods inherited from class java.lang.Object</B></TH>
  165. </TR>
  166. <TR BGCOLOR="white" CLASS="TableRowColor">
  167. <TD><CODE>equals, getClass, hashCode, notify, notifyAll, wait, wait, wait</CODE></TD>
  168. </TR>
  169. </TABLE>
  170. &nbsp;
  171. <P>
  172. <!-- ============ FIELD DETAIL =========== -->
  173. <A NAME="field_detail"><!-- --></A>
  174. <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
  175. <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
  176. <TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
  177. <B>Field Detail</B></FONT></TH>
  178. </TR>
  179. </TABLE>
  180. <A NAME="NANOS_PER_SEC"><!-- --></A><H3>
  181. NANOS_PER_SEC</H3>
  182. <PRE>public static final double <B>NANOS_PER_SEC</B></PRE>
  183. <DL>
  184. <DL>
  185. <DT><B>See Also:</B><DD><A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/constant-values.html#Stopwatch.NANOS_PER_SEC">Constant Field Values</A></DL>
  186. </DL>
  187. <!-- ========= CONSTRUCTOR DETAIL ======== -->
  188. <A NAME="constructor_detail"><!-- --></A>
  189. <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
  190. <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
  191. <TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
  192. <B>Constructor Detail</B></FONT></TH>
  193. </TR>
  194. </TABLE>
  195. <A NAME="Stopwatch()"><!-- --></A><H3>
  196. Stopwatch</H3>
  197. <PRE>public <B>Stopwatch</B>()</PRE>
  198. <DL>
  199. </DL>
  200. <!-- ============ METHOD DETAIL ========== -->
  201. <A NAME="method_detail"><!-- --></A>
  202. <TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
  203. <TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
  204. <TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
  205. <B>Method Detail</B></FONT></TH>
  206. </TR>
  207. </TABLE>
  208. <A NAME="start()"><!-- --></A><H3>
  209. start</H3>
  210. <PRE>public void <B>start</B>()</PRE>
  211. <DL>
  212. <DD>start the stop watch.
  213. <P>
  214. <DD><DL>
  215. </DL>
  216. </DD>
  217. </DL>
  218. <HR>
  219. <A NAME="stop()"><!-- --></A><H3>
  220. stop</H3>
  221. <PRE>public void <B>stop</B>()</PRE>
  222. <DL>
  223. <DD>stop the stop watch.
  224. <P>
  225. <DD><DL>
  226. </DL>
  227. </DD>
  228. </DL>
  229. <HR>
  230. <A NAME="time()"><!-- --></A><H3>
  231. time</H3>
  232. <PRE>public double <B>time</B>()</PRE>
  233. <DL>
  234. <DD>elapsed time in secods.
  235. <P>
  236. <DD><DL>
  237. <DT><B>Returns:</B><DD>the time recorded on the stopwatch in seconds</DL>
  238. </DD>
  239. </DL>
  240. <HR>
  241. <A NAME="toString()"><!-- --></A><H3>
  242. toString</H3>
  243. <PRE>public java.lang.String <B>toString</B>()</PRE>
  244. <DL>
  245. <DD><DL>
  246. <DT><B>Overrides:</B><DD><CODE>toString</CODE> in class <CODE>java.lang.Object</CODE></DL>
  247. </DD>
  248. <DD><DL>
  249. </DL>
  250. </DD>
  251. </DL>
  252. <HR>
  253. <A NAME="timeInNanoseconds()"><!-- --></A><H3>
  254. timeInNanoseconds</H3>
  255. <PRE>public long <B>timeInNanoseconds</B>()</PRE>
  256. <DL>
  257. <DD>elapsed time in nanoseconds.
  258. <P>
  259. <DD><DL>
  260. <DT><B>Returns:</B><DD>the time recorded on the stopwatch in nanoseconds</DL>
  261. </DD>
  262. </DL>
  263. <!-- ========= END OF CLASS DATA ========= -->
  264. <HR>
  265. <!-- ======= START OF BOTTOM NAVBAR ====== -->
  266. <A NAME="navbar_bottom"><!-- --></A>
  267. <A HREF="Stopwatch.html#skip-navbar_bottom" title="Skip navigation links"></A>
  268. <TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
  269. <TR>
  270. <TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
  271. <A NAME="navbar_bottom_firstrow"><!-- --></A>
  272. <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
  273. <TR ALIGN="center" VALIGN="top">
  274. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
  275. <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
  276. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/class-use/Stopwatch.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>
  277. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
  278. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
  279. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
  280. <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
  281. </TR>
  282. </TABLE>
  283. </TD>
  284. <TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
  285. </EM>
  286. </TD>
  287. </TR>
  288. <TR>
  289. <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  290. &nbsp;PREV CLASS&nbsp;
  291. &nbsp;NEXT CLASS</FONT></TD>
  292. <TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  293. <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/index.html?Stopwatch.html" target="_top"><B>FRAMES</B></A> &nbsp;
  294. &nbsp;<A HREF="Stopwatch.html" target="_top"><B>NO FRAMES</B></A> &nbsp;
  295. &nbsp;<SCRIPT type="text/javascript">
  296. <!--
  297. if(window==top) {
  298. document.writeln('<A HREF="allclasses-noframe.html"><B>All Classes</B></A>');
  299. }
  300. //-->
  301. </SCRIPT>
  302. <NOSCRIPT>
  303. <A HREF="https://www.cs.utexas.edu/~scottm/cs307/javacode/utilities/allclasses-noframe.html"><B>All Classes</B></A></NOSCRIPT>
  304. </FONT></TD>
  305. </TR>
  306. <TR>
  307. <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
  308. SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;<A HREF="Stopwatch.html#field_summary">FIELD</A>&nbsp;|&nbsp;<A HREF="Stopwatch.html#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="Stopwatch.html#method_summary">METHOD</A></FONT></TD>
  309. <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
  310. DETAIL:&nbsp;<A HREF="Stopwatch.html#field_detail">FIELD</A>&nbsp;|&nbsp;<A HREF="Stopwatch.html#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="Stopwatch.html#method_detail">METHOD</A></FONT></TD>
  311. </TR>
  312. </TABLE>
  313. <A NAME="skip-navbar_bottom"></A>
  314. <!-- ======== END OF BOTTOM NAVBAR ======= -->
  315. <HR>
  316. </BODY>
  317. </HTML>