<?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>Bryan Simonson &#187; Java</title>
	<atom:link href="http://www.bryansimonson.com/tags/programming/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bryansimonson.com</link>
	<description>Trust Door AI</description>
	<lastBuildDate>Wed, 01 Sep 2010 07:26:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Making a Fractal in Java&#8217;s Console</title>
		<link>http://www.bryansimonson.com/2010/02/21/making-a-fractal-in-javas-console/</link>
		<comments>http://www.bryansimonson.com/2010/02/21/making-a-fractal-in-javas-console/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 00:32:31 +0000</pubDate>
		<dc:creator>bryan</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[fractal]]></category>

		<guid isPermaLink="false">http://www.bryansimonson.com/?p=305</guid>
		<description><![CDATA[From, Data Structures and Other Objects Using Java, Chapter 8, Project #9, page 440: Examine this pattern of asterisks and blanks and write a recursive method that can generate exactly this pattern: * * * * * * * * * * * * * * * * * * * * * * * [...]]]></description>
			<content:encoded><![CDATA[<p>From, <a rel="nofollow" href="http://www.amazon.com/Data-Structures-Other-Objects-Using/dp/0321375254/ref=sr_1_2?ie=UTF8&#038;s=books&#038;qid=1266797057&#038;sr=1-2manticorcom-20" >Data Structures and Other Objects Using Java</a>, Chapter 8, Project #9, page 440:</p>
<p>
<blockquote>
Examine this pattern of asterisks and blanks and write a recursive method that can generate exactly this pattern:</p>
<pre>
*
* *
  *
* * * *
    *
    * *
      *
* * * * * * * *
        *
        * *
          *
        * * * *
            *
            * *
              *
</pre>
<p>With recursive thinking, the method needs only seven or eight lines of ode (including two recursive calls). How is this pattern a fractal? Your method should also be capable of producing larger or smaller patterns of the same variety. Hint: Have two parameters. One parameter indicates the indentation of the leftmost line in the pattern; the other parameter indicates the number of stars in the longest line.
</p></blockquote>
<p>WARNING: Cheating is bad, mmmmkay children? Don’t cheat.</p>
<pre class="brush: java; ">

 /*
|	Bryan Simonson
 \	CS211 - Craig Niiyama
  | 2/21/2010
*/

package bryansimonson;

public class Fractal {
	public static void main(String args[]){
		drawFractal(32,0);
	}

	public static void drawFractal(int length, int space){
		if (length==1){ // the stopping case
			for (int n=0; n&lt;space; n++) System.out.print(&quot;  &quot;);
			System.out.println(&quot;* &quot;);
		} else {		// length is a power of two
			drawFractal(length/2,space);
			for (int n=0; n&lt;space; n++) System.out.print(&quot;  &quot;);
			for (int n=0; n&lt;length; n++) System.out.print(&quot;* &quot;);
			System.out.println();
			drawFractal(length/2,length/2+space);
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bryansimonson.com/2010/02/21/making-a-fractal-in-javas-console/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simulating a Car Wash in Java</title>
		<link>http://www.bryansimonson.com/2010/02/21/simulating-a-car-wash-in-java/</link>
		<comments>http://www.bryansimonson.com/2010/02/21/simulating-a-car-wash-in-java/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 00:23:44 +0000</pubDate>
		<dc:creator>bryan</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[car wash]]></category>
		<category><![CDATA[simulation]]></category>

		<guid isPermaLink="false">http://www.bryansimonson.com/?p=303</guid>
		<description><![CDATA[From, Data Structures and Other Objects Using Java, Chapter 7, Project #8, page 396: Make improvements to the car was simulation program from Section 7.2. Note: Portions of code taken from Michael Main&#8217;s version. WARNING: Cheating is bad, mmmmkay children? Don’t cheat. /* &#124; Bryan Simonson \ CS211 - Craig Niiyama &#124; 2/21/2010 */ // [...]]]></description>
			<content:encoded><![CDATA[<p>From, <a rel="nofollow" href="http://www.amazon.com/Data-Structures-Other-Objects-Using/dp/0321375254/ref=sr_1_2?ie=UTF8&#038;s=books&#038;qid=1266797057&#038;sr=1-2manticorcom-20" >Data Structures and Other Objects Using Java</a>, Chapter 7, Project #8, page 396:</p>
<p>
<blockquote>
Make improvements to the <a href="http://www.cs.colorado.edu/~main/applications/CarWash.java">car was simulation program</p>
<p> from Section 7.2.
</p></blockquote>
<p>Note: Portions of code taken from <a href="http://www.cs.colorado.edu/~main/applications/CarWash.java">Michael Main&#8217;s</a> version.</p>
<p>WARNING: Cheating is bad, mmmmkay children? Don’t cheat.</p>
<pre class="brush: java; ">

 /*
|	Bryan Simonson
 \	CS211 - Craig Niiyama
  | 2/21/2010
*/

// FILE: Carwash.java
// This program illustrates the use of the carWashSimulate method which uses
// a simple queue to simulate cars waiting at a car wash.
package bryansimonson;
import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import edu.colorado.simulations.BooleanSource;
import edu.colorado.simulations.Washer;
import edu.colorado.simulations.Averager;

/******************************************************************************
* The &lt;CODE&gt;CarWash&lt;/CODE&gt; Java application illustrates the use of
* the &lt;CODE&gt;carWashSimulate&lt;/CODE&gt; method.
* The illustration uses the following values:
*   &lt;CODE&gt;
*   &lt;br&gt;washTime = 240
*   &lt;br&gt;arrivalTime = 0.0025
*   &lt;br&gt;totalTime = 6000
*   &lt;/CODE&gt;
*
* &lt;p&gt;&lt;dt&gt;&lt;b&gt;Java Source Code for this class:&lt;/b&gt;&lt;dd&gt;
*   &lt;A HREF=&quot;../applications/CarWash.java&quot;&gt;
*   http://www.cs.colorado.edu/~main/applications/CarWash.java
*   &lt;/A&gt;
*
* @author Michael Main
*   &lt;A HREF=&quot;mailto:main@colorado.edu&quot;&gt; (main@colorado.edu) &lt;/A&gt;
*
* @version
*   Jun 12, 1998
******************************************************************************/
public class CarWash
{
   /**
   * The main method activates &lt;CODE&gt;carWashSimulate&lt;/CODE&gt; with the values:
   *   &lt;CODE&gt;
   *   &lt;br&gt;washTime = 240
   *   &lt;br&gt;arrivalTime = 0.0025
   *   &lt;br&gt;totalTime = 6000
   *   &lt;/CODE&gt;
   * &lt;BR&gt;The &lt;CODE&gt;String&lt;/CODE&gt; argument (&lt;CODE&gt;args&lt;/CODE&gt;) is not used in
   * this implementation.
   **/
   public static void main(String[ ] args) throws IOException
   {
	  BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

	  // get total length of simulation from the user
      System.out.println(&quot;How many seconds would you like to simulate?&quot;);
      System.out.println(&quot;(Default is 6000)&quot;);
      int simtime = Integer.parseInt(stdin.readLine());

      // get the probability of a new customer from the user
      System.out.println(&quot;What is the probably of a new customer arriving in any given second?&quot;);
      System.out.println(&quot;(Default is 0.0025)&quot;);
      double probability = Double.parseDouble(stdin.readLine());

      // get the number of seconds it takes to wash the car from the user
      System.out.println(&quot;How many seconds does it take to wash one car?&quot;);
      System.out.println(&quot;(Default is 240)&quot;);
      int washtime = Integer.parseInt(stdin.readLine());

      carWashSimulate(washtime, probability, simtime);
   }

   /**
   * Simulate the running of a car washer for a specified amount of time.
   * @param &lt;CODE&gt;washTime&lt;/CODE&gt;
   *   the number of seconds required to wash one car
   * @param &lt;CODE&gt;arrivalProb&lt;/CODE&gt;
   *   the probability of a customer arriving in any second, for example
   *   0.1 is 10%
   * @param &lt;CODE&gt;totalTime&lt;/CODE&gt;
   *   the total number of seconds for the simulation
   * &lt;dt&gt;&lt;b&gt;Precondition:&lt;/b&gt;&lt;dd&gt;
   *   &lt;CODE&gt;washTime&lt;/CODE&gt; and &lt;CODE&gt;totalTime&lt;/CODE&gt; are positive;
   *   &lt;CODE&gt;arrivalProb&lt;/CODE&gt; lies in the range 0 to 1.
   * &lt;dt&gt;&lt;b&gt;Postcondition:&lt;/b&gt;&lt;dd&gt;
   *   The method has simulated a car wash where &lt;CODE&gt;washTime&lt;/CODE&gt; is the
   *   number of seconds needed to wash one car, &lt;CODE&gt;arrivalProb&lt;/CODE&gt; is
   *   the probability of a customer arriving in any second, and
   *   &lt;CODE&gt;totalTime&lt;/CODE&gt; is the total number of seconds for the
   *   simulation. Before the simulation, the method has written its three
   *   parameters to &lt;CODE&gt;System.out&lt;/CODE&gt;. After the simulation, the method
   *   has written two pieces of information to &lt;CODE&gt;System.out&lt;/CODE&gt;:
   *   (1) The number of cars washed, and (2) The average waiting time for
   *   customers that had their cars washed. (Customers that are still in the
   *   queue ARE included in this average).
   * @exception java.lang.IllegalArgumentException
   *   Indicates that one of the arguments violates the precondition.
   **/
   public static void carWashSimulate
   (int washTime, double arrivalProb, int totalTime)
   {
      Queue&lt;Integer&gt; arrivalTimes = new LinkedList&lt;Integer&gt;( );
      int next;
      BooleanSource arrival = new BooleanSource(arrivalProb);
      Washer machine = new Washer(washTime);
      Averager waitTimes = new Averager( );
      int currentSecond;

      // Write the parameters to System.out.
      System.out.println(&quot;Seconds to wash one car: &quot; + washTime);
      System.out.print(&quot;Probability of customer arrival during a second: &quot;);
      System.out.println(arrivalProb);
      System.out.println(&quot;Total simulation seconds (open \&quot;hours\&quot;): &quot; + totalTime);

      // Check the precondition:
      if (washTime &lt;= 0 || arrivalProb &lt; 0 || arrivalProb &gt; 1 || totalTime &lt; 0)
         throw new IllegalArgumentException(&quot;Values out of range&quot;); 

      for (currentSecond = 0; currentSecond &lt; totalTime || !arrivalTimes.isEmpty(); currentSecond++)
      {  // Simulate the passage of one second of time.

         // Check whether a new customer has arrived.
         if (arrival.query( ) &amp;&amp; currentSecond &lt;= totalTime)
            arrivalTimes.add(currentSecond);

         // Check whether we can start washing another car.
         if ((!machine.isBusy( ))  &amp;&amp;  (!arrivalTimes.isEmpty( )))
         {
            next = arrivalTimes.remove( );
            waitTimes.addNumber(currentSecond - next);
            machine.startWashing( );
         }

         // Subtract one second from the remaining time in the current wash cycle.
         machine.reduceRemainingTime( );
      }

      // Write the summary information about the simulation.
      System.out.println(&quot;Customers served: &quot; + waitTimes.howManyNumbers( ));
      if (waitTimes.howManyNumbers( ) &gt; 0)
         System.out.println(&quot;Average wait: &quot; + waitTimes.average( ) + &quot; sec&quot;);

      // Print ending second to compare to expected &quot;closing time&quot;
      System.out.println(&quot;Ending second (or last customer served): &quot; + currentSecond);
   }

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bryansimonson.com/2010/02/21/simulating-a-car-wash-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solving the nQueens Problem in Java</title>
		<link>http://www.bryansimonson.com/2010/02/21/solving-the-nqueens-problem-in-java/</link>
		<comments>http://www.bryansimonson.com/2010/02/21/solving-the-nqueens-problem-in-java/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 00:15:34 +0000</pubDate>
		<dc:creator>bryan</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[nqueens]]></category>

		<guid isPermaLink="false">http://www.bryansimonson.com/?p=291</guid>
		<description><![CDATA[From Data Structures and Other Objects Using Java, Chapter 6, Project #10, Page #347: Suppose that you have n queens from a chess game, and that you also have an n-by-n chess board. Is it possible to place all n queens on the board so that no two queens are in the same row, no [...]]]></description>
			<content:encoded><![CDATA[<p>From <a rel="nofollow" href="http://www.amazon.com/Data-Structures-Other-Objects-Using/dp/0321375254/ref=sr_1_2?ie=UTF8&#038;s=books&#038;qid=1266797057&#038;sr=1-2manticorcom-20" >Data Structures and Other Objects Using Java</a>, Chapter 6, Project #10, Page #347:</p>
<blockquote><p>
Suppose that you have <i>n</i> queens from a chess game, and that you also have an <i>n</i>-by-<i>n</i> chess board. Is it possible to place all <i>n</i> queens on the board so that no two queens are in the same row, no two queens are in the same column, and no two queens are on the same diagonal?
</p></blockquote>
<p></p>
<p>WARNING: Cheating is bad, mmmmkay children? Don&#8217;t cheat.</p>
<p>n Queens source code:</p>
<pre class="brush: java; ">

 /*
|	Bryan Simonson
 \	CS211 - Craig Niiyama
  | 2/21/2010
*/

package bryansimonson;
import edu.colorado.collections.*;

public class nQueens {
	public static void main(String[] args) {
		final int QUEENS = 40;
		queens(QUEENS);
	}

	public static void queens(int n){
		boolean success = false;

		// row = the row within the stack, column = the row&#039;s integer value
		ArrayStack&lt;Integer&gt; s = new ArrayStack&lt;Integer&gt;(n);
		s.push(1);

		while (!success &amp;&amp; !s.isEmpty()){
			int x=0;	// placeholder for when we pop values
			boolean conflict = false;

			// check for conflicts
			for (int i=1; i&lt;s.size(); i++){
				int deltarows = s.size()-i;
				if (	// no need to check row, stack preempts row conflict
						s.peek()==s.itemAt(i) ||			// same column
						s.peek()==s.itemAt(i)+deltarows ||	// same diagonal
						s.peek()==s.itemAt(i)-deltarows		// same diagonal
					) conflict = true;
			}

			if (conflict){
				while (s.peek()==n)
					x=s.pop();
				if (!s.isEmpty())
					s.push(s.pop()+1);
				else
					s.push(x+1);
			} else if (!conflict &amp;&amp; s.size()==n){
				success = true;
			} else {
				// new row, so try column 1
				s.push(1);
			}
		}

		// print out the board with the queens
		for (int x=1; x&lt;=s.size(); x++){
			for (int y=1; y&lt;=n; y++){
				if (s.itemAt(x)==y)
					System.out.print(&quot;X &quot;);
				else
					System.out.print(&quot;O &quot;);
			}
			System.out.println();
		}

	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bryansimonson.com/2010/02/21/solving-the-nqueens-problem-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PseudoRandom Number Generation in Java</title>
		<link>http://www.bryansimonson.com/2010/01/20/pseudorandom-number-generation-in-java/</link>
		<comments>http://www.bryansimonson.com/2010/01/20/pseudorandom-number-generation-in-java/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 02:51:42 +0000</pubDate>
		<dc:creator>bryan</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[School]]></category>

		<guid isPermaLink="false">http://www.bryansimonson.com/?p=286</guid>
		<description><![CDATA[Assignment number 1 in CS211 (Java) programming class. (No stealing! I want full point on my grade!) PseudoRandomDemo.java /* &#124; Bryan Simonson \ CS211 - Craig Niiyama &#124; 1/19/2010 */ package bryansimonson; import bryansimonson.PseudoRandom; /** * @author Bryan Simonson (manticor@gmail.com) */ public class PseudoRandomDemo { /** * The main function generates a table that counts [...]]]></description>
			<content:encoded><![CDATA[<p>Assignment number 1 in CS211 (Java) programming class. (No stealing! I want full point on my grade!)</p>
<p>PseudoRandomDemo.java</p>
<pre class="brush: java; ">

 /*
|	Bryan Simonson
 \	CS211 - Craig Niiyama
  | 1/19/2010
*/

package bryansimonson;
import bryansimonson.PseudoRandom;

/**
 * @author Bryan Simonson (manticor@gmail.com)
 */
public class PseudoRandomDemo {

	/**
	 * The main function generates a table that counts how many
	 * instances of a pseudorandom number fall within a certain range.
	 */
	public static void main(String[] args) {
		final int INTRVL = 10;				// interval of distribution table
		final int LOOPS = 1000000;			// number of times to run loop
		final int SEED = 1;					// initial seed value
		final int INCR = 3641;				// increment
		final int MOD = 729;				// modulus
		final int MULT = 40;				// multiplier
		int[] dist = new int[INTRVL];		// distribution table

		PseudoRandom x = new PseudoRandom(SEED, MULT, INCR, MOD);

		System.out.println(&quot;Output of PseudoRandomDemo&quot;);
		System.out.println();

		// Show initial values
		System.out.println(&quot;Initial values&quot;);
		System.out.println(&quot;Interval:\t&quot;+INTRVL);
		System.out.println(&quot;Seed:\t\t&quot;+SEED);
		System.out.println(&quot;Multiplier:\t&quot;+MULT);
		System.out.println(&quot;Increment:\t&quot;+INCR);
		System.out.println(&quot;Modulus:\t&quot;+MOD);
		System.out.println();

		// Demonstrate current() function
		System.out.println(&quot;Demonstrate current() function&quot;);
		System.out.println(x.current());
		System.out.println();

		// Demonstrate next() function
		System.out.println(&quot;Demonstrate next() function&quot;);
		System.out.println(x.next());
		System.out.println(x.next());
		System.out.println(x.next());
		System.out.println();

		// Demonstrate nextMod() function
		System.out.println(&quot;Demonstrate nextMod() function&quot;);
		System.out.print(&quot;nextMod() value: &quot;);
		System.out.printf(&quot;%6.2f&quot;,x.nextMod());
		System.out.println();
		System.out.println(&quot;new seed value:&quot;+x.current());

		// Demonstrate changeSeed() function
		x.changeSeed(SEED);
		System.out.println(&quot;\nSeed has been reset using changeSeed(&quot;+SEED+&quot;)\n&quot;);

		// Populate array with count of instances between each interval
		for (int i = 0; i&lt;LOOPS; i++){
			dist[(int)(x.nextMod()*INTRVL)]++;
		}

		// Print out a table with instance counts per interval
		System.out.println(&quot;Count of instances&quot;);
		System.out.println(&quot;Range\t\tNumber of Occurrences&quot;);
		for (int i=0; i&lt;INTRVL; i++){
			System.out.println(
					&quot;[&quot;+
					((double)(i)/INTRVL)+
					&quot;..&quot;+
					((double)(i+1)/INTRVL)+
					&quot;)\t&quot;+
					dist[i]
			);
		}
	}
}
</pre>
<p>PseudoRandom.java (Class)</p>
<pre class="brush: java; ">

package bryansimonson;

 /*
|	Bryan Simonson
 \	CS211 - Craig Niiyama
  | 1/19/2010
*/

/**
 * @author Bryan Simonson (manticor@gmail.com)
 *
 */
public class PseudoRandom {
	private int seed;
	private int multiplier;
	private int increment;
	private int modulus;

	/**
	 * Constructor for PseudoRandom object.
	 * @param firstseed - The original seed value
	 * @param mult - Multiplier
	 * @param incr - Increment
	 * @param mod - Modulus value
	 */
	public PseudoRandom (int firstseed, int mult, int incr, int mod){
		multiplier = mult;
		increment = incr;
		modulus = mod;
		seed = (mult * firstseed + incr) % mod;
	}

	/**
	 * Access the current seed value
	 * @return
	 * 	The current seed value
	 */
	public int current(){
		return seed;
	}

	/**
	 * Generates the next pseudorandom number iteration
	 * @return
	 * 	The next pseudorandom number iteration
	 * @postcondition
	 * 	The current seed value set to the same value that is returned
	 */
	public int next(){
		seed = (multiplier * seed + increment) % modulus;
		return seed;
	}

	/**
	 * Change the current seed value
	 * @param newseed
	 * @postcondition
	 * 	The current seed value is replaced by &lt;code&gt;newseed&lt;/code&gt;
	 */
	public void changeSeed(int newseed){
		seed = newseed;
	}

	/**
	 * Generates the next pseudorandom number divided by the modulus value
	 * @return
	 * 	a double in the range [0..1)
	 * @postcondition
	 * 	the current seed value is set to whatever the next pseudorandom integer
	 */
	public double nextMod(){
		return (next()/(double)modulus);
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bryansimonson.com/2010/01/20/pseudorandom-number-generation-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
