<?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>alexrose.net &#187; Java</title>
	<atom:link href="http://www.alexrose.net/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alexrose.net</link>
	<description></description>
	<lastBuildDate>Mon, 03 Aug 2009 22:50:16 +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>My First Scala Program</title>
		<link>http://www.alexrose.net/2009/08/03/my-first-scala-program/</link>
		<comments>http://www.alexrose.net/2009/08/03/my-first-scala-program/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 22:41:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.alexrose.net/2009/08/03/my-first-scala-program/</guid>
		<description><![CDATA[A while back for my birthday, I got the Odersky Programming in Scala book and today I finally got a chance to crack it and play around. I had been interested in trying to pick up one of the new wave of dynamic JVM langagues like Groovy, JRuby, or Jython and had settled on Scala. [...]]]></description>
			<content:encoded><![CDATA[<p>A while back for my birthday, I got the Odersky <a href="http://www.amazon.com/Programming-Scala-Comprehensive-Step-step/dp/0981531601/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1249338643&amp;sr=8-1">Programming in Scala</a> book and today I finally got a chance to crack it and play around. I had been interested in trying to pick up one of the new wave of dynamic <a href="http://en.wikipedia.org/wiki/List_of_JVM_languages">JVM langagues</a> like <a href="http://groovy.codehaus.org/">Groovy</a>, <a href="http://jruby.codehaus.org/">JRuby</a>, or <a href="http://www.jython.org/">Jython</a> and had settled on <a href="http://www.scala-lang.org/">Scala</a>. I had read a suggestion that <a href="http://projecteuler.net/">Project Euler</a> would make for a fertile playground of test problems to learn on, and having already done the first several in Java that rang true for me. So, here is my overly complicated solution to the simple first problem..</p>
<blockquote>
<p style="font: 11.0px Monaco; color: #ff5e5e">//If we list all the natural numbers below 10 that are multiples of 3</p>
<p style="font: 11.0px Monaco; color: #ff5e5e"><span style="text-decoration: underline;">/</span>/or 5, <span style="text-decoration: underline;">we</span> get 3, <span style="text-decoration: underline;">5</span>, <span style="text-decoration: underline;">6</span> and 9. The sum of these multiples is 23.</p>
<p style="font: 11.0px Monaco; color: #ff5e5e">//Find the sum of all the multiples of 3 or 5 below 1000.</p>
<p style="font: 11.0px Monaco; color: #8e3e00"><span style="color: #4c4c4c">import</span> scala<span style="color: #000000">.</span>actors<span style="color: #000000">._</span></p>
<p style="font: 11.0px Monaco; min-height: 15.0px">
<p style="font: 11.0px Monaco; color: #4c4c4c">object <span style="color: #8e3e00">Adder</span> extends <span style="color: #8e3e00">Actor</span> <span style="color: #000000">{</span></p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 30px;"><span style="color: #4c4c4c">var</span> <span style="color: #2300fb">sum</span> = 0</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 30px;"><span style="color: #4c4c4c">def</span> <span style="color: #003e85">act</span>() = {</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 60px;"><span style="color: #003e85">react</span> {</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 90px;"><span style="color: #4c4c4c">case</span> (<span style="color: #2300fb">num</span>: <span style="color: #8e3e00">Int</span>) =&gt;</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 120px;"><span style="color: #2300fb">sum</span> <span style="color: #003e85">+=</span> <span style="color: #2300fb">num</span></p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 120px;"><span style="color: #003e85">act</span>()</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 90px;"><span style="color: #4c4c4c">case</span> <span style="color: #ff5e5e">&#8220;REPORT&#8221;</span> =&gt;</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 120px;"><span style="color: #8e3e00">Console</span>.<span style="color: #003e85">println</span>(<span style="color: #ff5e5e">&#8220;Total = &#8220;</span> <span style="color: #003e85">+</span> <span style="color: #2300fb">sum</span>);</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 60px;">}</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 30px;">}</p>
<p style="font: 11.0px Monaco">}</p>
<p style="font: 11.0px Monaco; min-height: 15.0px">
<p style="font: 11.0px Monaco; color: #8e3e00"><span style="color: #4c4c4c">object</span> <span style="text-decoration: underline;">ProblemOneRunner</span> <span style="color: #000000">{</span></p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 30px;"><span style="color: #4c4c4c">def</span> <span style="color: #003e85">main</span>(<span style="color: #2300fb">args</span>: <span style="color: #8e3e00">Array</span>[<span style="color: #8e3e00">String</span>]): <span style="color: #8e3e00">Unit</span> = {</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 60px;"><span style="color: #8e3e00">Adder</span>.<span style="color: #003e85; text-decoration: underline;">star</span><span style="color: #003e85">t</span></p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 60px;"><span style="color: #4c4c4c">for</span>(<span style="color: #2300fb">i</span> &lt;- 1 <span style="color: #003e85">to</span> 999) {</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 90px;"><span style="color: #4c4c4c">if</span>(<span style="color: #2300fb">i</span> <span style="color: #003e85">%</span> 3 <span style="color: #003e85">==</span> 0 <span style="color: #003e85">||</span> <span style="color: #2300fb">i</span> <span style="color: #003e85">%</span> 5 <span style="color: #003e85">==</span> 0 ) {</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 120px;"><span style="color: #8e3e00">Adder</span> <span style="color: #003e85">!</span> <span style="color: #2300fb">i</span></p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 90px;">}</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 60px;">}</p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 60px;"><span style="color: #8e3e00">Adder</span> <span style="color: #003e85">!</span> <span style="color: #ff5e5e">&#8220;REPORT&#8221;</span></p>
<p style="font-family: Monaco; font-style: normal; font-variant: normal; font-weight: normal; font-size: 11px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; padding-left: 30px;">}</p>
<p style="font: 11.0px Monaco">}</p>
<p style="font: 11.0px Monaco">
</blockquote>
<p>It starts out looking something like a Java program with an import statement, although Scala uses the _ notation to import everything instead of * since * is an identifier. Scala supports a lot of flexibility in its import statements, they can show up anywhere and you can use them to remap names or set up exclusion filters &#8211; but lets not get to far astray.</p>
<p>Next, you&#8217;ll notice that my program has two objects and no classes. Its not that Scala doesn&#8217;t have classes, its just that Scala classes don&#8217;t have the Java concept of static &#8211; if you want a singleton object, you use the <em>object</em> keyword. The solution I have above uses one singleton as the main program entry point and another Adder singleton that extends Actor. This is just so I could play with Scala&#8217;s built in multithreading and message passing support &#8211; obviously this problem doesn&#8217;t really require it but I thought I&#8217;d try it out anyway for fun.</p>
<p>The Adder has its own sum variable it uses to keep track of the numbers it will add up &#8211; Scala uses the <em>var</em> keyword for variables that can be reset and <em>val</em> for ones that can&#8217;t. You&#8217;ll notice the lack of any type declaration here, its not that Scala variables are not typed, its just that it uses type inference to figure out what they are. Also, Java users will notice the lack of semicolons &#8211; semicolons are optional in Scala in most cases. The Adder overrides the <em>act</em> method, the entry point for threads, and then calls <em>react</em> which will look for a message in the actor&#8217;s inbox and if it doesn&#8217;t find one, put the actor to sleep. Unlike some of the other constructs Scala provides for Actors like <em>receive</em>, the thread will die at the end of <em>react</em> instead of looping so we have to put a call to <em>act</em>() at the end of the int message we send it to keep it alive.</p>
<p>In the main method, we start up our Actor thread, then loop through 1 through 999 looking for numbers divisible by 3 or 5 to send to our Adder, then once we are done we send a message to the thread to report and finish its loop. Again, Java users will notice the easy condensed for loop and the ! message passing syntax.</p>
<p>I&#8217;ve barely scratched the surface of Scala here, I&#8217;ll try to get a couple more problems done and posted that likewise abuse language features as I get time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexrose.net/2009/08/03/my-first-scala-program/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaBat</title>
		<link>http://www.alexrose.net/2009/03/27/javabat/</link>
		<comments>http://www.alexrose.net/2009/03/27/javabat/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 19:22:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://alexrose.net/?p=108</guid>
		<description><![CDATA[Stumbled over another Java practice problem site for those poor bored individuals who just like writing code. This one is unique in that you are basically asked to implement a function which matches some specification you then write in a web form. On submit the site compiles the code and runs some unit tests against [...]]]></description>
			<content:encoded><![CDATA[<p>Stumbled over another Java practice problem site for those poor bored individuals who just like writing code. This one is unique in that you are basically asked to implement a function which matches some specification you then write in a web form. On submit the site compiles the code and runs some unit tests against it to verify you have solved the particular challenge. You can create an account to track your progress if you like but its not mandatory. Pretty cool.</p>
<p><a href="http://www.javabat.com">JavaBat</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexrose.net/2009/03/27/javabat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler</title>
		<link>http://www.alexrose.net/2008/12/01/project-euler/</link>
		<comments>http://www.alexrose.net/2008/12/01/project-euler/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 20:17:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://alexrose.net/?p=72</guid>
		<description><![CDATA[I stumbled across Project Euler the other day and keep on finding myself going back for more. What is Project Euler you might ask? In their own words&#8230; Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at [...]]]></description>
			<content:encoded><![CDATA[<p>I stumbled across <a href="http://projecteuler.net/">Project Euler</a> the other day and keep on finding myself going back for more. What is Project Euler you might ask? In their own words&#8230;</p>
<blockquote>
<p>Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.</p>
<p>The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.</p>
</blockquote>
<p>I&#8217;ve only done the first 4 problems so far so I haven&#8217;t even reached &#8220;level 1&#8243; according to their scoring yet. Nothing has been all that challenging up to this point, the problems have been taking me around 30-60 minutes to solve. It&#8217;s just nice to have a pool of simple programming problems that one can go back to when one is in the mood, sort of like having a book of logic problems laying around.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexrose.net/2008/12/01/project-euler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

