<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CodeMocha &#187; Code</title>
	<atom:link href="http://codemocha.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://codemocha.com</link>
	<description>Freshly ground code.  Thoughts on software and algorithms by Chet Mancini</description>
	<lastBuildDate>Wed, 16 Nov 2011 02:26:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Bit Operations</title>
		<link>http://codemocha.com/2009/07/bit-operations/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=bit-operations</link>
		<comments>http://codemocha.com/2009/07/bit-operations/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 18:31:41 +0000</pubDate>
		<dc:creator>Chet</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[bits]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[function]]></category>

		<guid isPermaLink="false">http://codemocha.com/?p=22</guid>
		<description><![CDATA[For CS351, Intro to Systems, we had to do several operations using bit operators only. These took awhile, but are pretty cool to show off. We were graded by how few operations we used to return the right values for the generated test cases, including some difficult boundary ones. 1234//NOR operation using only ~ and [...]]]></description>
			<content:encoded><![CDATA[<p>For CS351, Intro to Systems, we had to do several operations using bit operators only.  These took awhile, but are pretty cool to show off.  We were graded by how few operations we used to return the right values for the generated test cases, including some difficult boundary ones.<span id="more-22"></span></p>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//NOR operation using only ~ and &amp;</span><br />
<span style="color: #993333;">int</span> bitNor<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>~x<span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span><span style="color: #009900;">&#40;</span>~y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The XOR operation is equivalent to the (x NAND y) AND (x OR y).</p>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//XOR operation using only ~ and &amp;</span><br />
<span style="color: #993333;">int</span> bitXor<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>~<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">&amp;</span>y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span><span style="color: #009900;">&#40;</span>~<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>~x<span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span><span style="color: #009900;">&#40;</span>~y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//Return true if x is not equal to y not using comparison operators.</span><br />
<span style="color: #993333;">int</span> isNotEqual<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">!!</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">^</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>There are two types of shifts, arithmetic and logical.  Left shifts are the same in both types, but right shifts are different.  Logical shifts insert zeroes to fill in the empty spaces to keep the logic of the bits, but arithmetic shifts fill in the value of the sign bit to preserve the sign of the operand in twos-complement integers.</p>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> rightLogicalShift<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> allOnes <span style="color: #339933;">=</span> ~<span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> y <span style="color: #339933;">=</span> ~<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>allOnes<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&lt;</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">32</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>~n <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&gt;&gt;</span> n<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://codemocha.com/2009/07/bit-operations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Euclidean Algorithm</title>
		<link>http://codemocha.com/2009/07/euclidean-algorithm/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=euclidean-algorithm</link>
		<comments>http://codemocha.com/2009/07/euclidean-algorithm/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 03:46:41 +0000</pubDate>
		<dc:creator>Chet</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[ml]]></category>

		<guid isPermaLink="false">http://codemocha.com/?p=9</guid>
		<description><![CDATA[One of my favorite algorithms is the simple, elegant Euclidean algorithm for finding the greatest common denominator of two numbers. In ML or another functional language it really is beautiful: 12fun gcd&#40;a, 0&#41; = a &#160; &#160;&#124; gcd&#40;a, b&#41; = gcd&#40;b, a mod b&#41;; Of course, the same can be done in Java iteratively: 123456789public [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favorite algorithms is the simple, elegant Euclidean algorithm for finding the greatest common denominator of two numbers.  In ML or another functional language it really is beautiful:<span id="more-9"></span></p>
<div class="codecolorer-container ocaml vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="ocaml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #06c; font-weight: bold;">fun</span> gcd<span style="color: #a52a2a;">&#40;</span>a, <span style="color: #c6c;">0</span><span style="color: #a52a2a;">&#41;</span> <span style="color: #a52a2a;">=</span> a<br />
&nbsp; &nbsp;<span style="color: #a52a2a;">|</span> gcd<span style="color: #a52a2a;">&#40;</span>a, b<span style="color: #a52a2a;">&#41;</span> <span style="color: #a52a2a;">=</span> gcd<span style="color: #a52a2a;">&#40;</span>b, a <span style="color: #06c; font-weight: bold;">mod</span> b<span style="color: #a52a2a;">&#41;</span><span style="color: #a52a2a;">;</span></div></td></tr></tbody></table></div>
<p>Of course, the same can be done in Java iteratively:</p>
<div class="codecolorer-container java vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> gcd<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> a, <span style="color: #000066; font-weight: bold;">int</span> b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> c<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>b<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; c <span style="color: #339933;">=</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; a <span style="color: #339933;">=</span> b<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #339933;">=</span> c<span style="color: #339933;">%</span>b<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Or recursively:</p>
<div class="codecolorer-container java vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> gcd<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> a, <span style="color: #000066; font-weight: bold;">int</span> b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>b<span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> gcd<span style="color: #009900;">&#40;</span>b, a<span style="color: #339933;">%</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://codemocha.com/2009/07/euclidean-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recursive Fibonacci</title>
		<link>http://codemocha.com/2009/07/recursive-fibonacci/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=recursive-fibonacci</link>
		<comments>http://codemocha.com/2009/07/recursive-fibonacci/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 03:40:28 +0000</pubDate>
		<dc:creator>Chet</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[college]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[ml]]></category>
		<category><![CDATA[recusion]]></category>

		<guid isPermaLink="false">http://codemocha.com/?p=7</guid>
		<description><![CDATA[Here are two recursive fibonacci functions in ML I did for CS243. Can you tell which one is more efficient? 123456789101112fun fibonacci&#40;n&#41; = &#160; let &#160; &#160; &#160; fun fib&#40;&#40;a, b&#41;, i&#41;= &#160; &#160; &#160; if i=0 then b &#160; &#160; &#160; &#160; else fib&#40;&#40;b, a + b&#41;, i-1&#41;; &#160; &#160; &#160; &#160; &#160; &#160;in [...]]]></description>
			<content:encoded><![CDATA[<p>Here are two recursive fibonacci functions in ML I did for CS243.  Can you tell which one is more efficient?<span id="more-7"></span></p>
<div class="codecolorer-container ocaml vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="ocaml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #06c; font-weight: bold;">fun</span> fibonacci<span style="color: #a52a2a;">&#40;</span>n<span style="color: #a52a2a;">&#41;</span> <span style="color: #a52a2a;">=</span> <br />
&nbsp; <span style="color: #06c; font-weight: bold;">let</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">fun</span> fib<span style="color: #a52a2a;">&#40;</span><span style="color: #a52a2a;">&#40;</span>a, b<span style="color: #a52a2a;">&#41;</span>, i<span style="color: #a52a2a;">&#41;</span><span style="color: #a52a2a;">=</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">if</span> i<span style="color: #a52a2a;">=</span><span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">then</span> b<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">else</span> fib<span style="color: #a52a2a;">&#40;</span><span style="color: #a52a2a;">&#40;</span>b, a <span style="color: #a52a2a;">+</span> b<span style="color: #a52a2a;">&#41;</span>, i<span style="color: #a52a2a;">-</span><span style="color: #c6c;">1</span><span style="color: #a52a2a;">&#41;</span><span style="color: #a52a2a;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">in</span><br />
&nbsp; fib<span style="color: #a52a2a;">&#40;</span><span style="color: #a52a2a;">&#40;</span><span style="color: #c6c;">0</span>, <span style="color: #c6c;">1</span><span style="color: #a52a2a;">&#41;</span>, n<span style="color: #a52a2a;">&#41;</span><br />
&nbsp; <span style="color: #06c; font-weight: bold;">end</span><span style="color: #a52a2a;">;</span><br />
<br />
<span style="color: #06c; font-weight: bold;">fun</span> fibonacci<span style="color: #a52a2a;">&#40;</span><span style="color: #c6c;">0</span><span style="color: #a52a2a;">&#41;</span> <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">1</span><br />
&nbsp; <span style="color: #a52a2a;">|</span> fibonacci<span style="color: #a52a2a;">&#40;</span><span style="color: #c6c;">1</span><span style="color: #a52a2a;">&#41;</span> <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">1</span><br />
&nbsp; <span style="color: #a52a2a;">|</span> fibonacci<span style="color: #a52a2a;">&#40;</span>n<span style="color: #a52a2a;">&#41;</span> <span style="color: #a52a2a;">=</span> fibonacci<span style="color: #a52a2a;">&#40;</span>n<span style="color: #a52a2a;">-</span><span style="color: #c6c;">1</span><span style="color: #a52a2a;">&#41;</span> <span style="color: #a52a2a;">+</span> fibonacci<span style="color: #a52a2a;">&#40;</span>n<span style="color: #a52a2a;">-</span><span style="color: #c6c;">2</span><span style="color: #a52a2a;">&#41;</span><span style="color: #a52a2a;">;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://codemocha.com/2009/07/recursive-fibonacci/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Circle Calculator</title>
		<link>http://codemocha.com/2009/07/circle-calculator/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=circle-calculator</link>
		<comments>http://codemocha.com/2009/07/circle-calculator/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 03:26:57 +0000</pubDate>
		<dc:creator>Chet</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[calculator]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[college]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://codemocha.com/?p=3</guid>
		<description><![CDATA[For my first code post I thought it might be appropriate to put the first code from the first lab of my first computer science class at Wheaton.]]></description>
			<content:encoded><![CDATA[<p>For my first code post I thought it might be appropriate to put the first code from the first lab of my first computer science class at Wheaton.<span id="more-3"></span></p>
<div class="codecolorer-container java vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Scanner</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Circle <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Scanner keyboard <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">in</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<span style="color: #666666; font-style: italic;">// input from keyboard</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Please enter the radius--&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> radius <span style="color: #339933;">=</span> keyboard.<span style="color: #006633;">nextDouble</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #666666; font-style: italic;">// radius to work with</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> diameter <span style="color: #339933;">=</span> radius<span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The diameter is &quot;</span> <span style="color: #339933;">+</span> diameter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> circumference <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">PI</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">*</span>radius<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The circumference is &quot;</span> <span style="color: #339933;">+</span> circumference<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> area <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">PI</span> <span style="color: #339933;">*</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>radius, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The area is &quot;</span> <span style="color: #339933;">+</span> area<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> volume <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4.0</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">3.0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">PI</span> <span style="color: #339933;">*</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>radius, <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The volume of a sphere is &quot;</span> <span style="color: #339933;">+</span> volume<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> surfaceArea <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">PI</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">4</span> <span style="color: #339933;">*</span> &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>radius, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The surface area of a sphere is &quot;</span> <span style="color: #339933;">+</span> surfaceArea<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://codemocha.com/2009/07/circle-calculator/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

