<?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>Conor's Blog &#187; College</title>
	<atom:link href="http://blog.macaoidh.name/category/college/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.macaoidh.name</link>
	<description>PHP, Music, Linux</description>
	<lastBuildDate>Mon, 05 Jul 2010 13:25:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Java &#8211; Drawing Squares</title>
		<link>http://blog.macaoidh.name/2009/12/05/java-drawing-squares/</link>
		<comments>http://blog.macaoidh.name/2009/12/05/java-drawing-squares/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 03:28:07 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[drawing]]></category>
		<category><![CDATA[icsp]]></category>
		<category><![CDATA[reges]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[shape]]></category>
		<category><![CDATA[squares]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=284</guid>
		<description><![CDATA[This weeks ICSP assignment was to write a program which draws squares. The task was to draw squares from a set list in an array, but I opted rather to accept a command line argument in my script. To write this I had to delve back into regular expression, which I hate because I don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>This weeks ICSP assignment was to write a program which draws squares. The task was to draw squares from a set list in an array, but I opted rather to accept a command line argument in my script. To write this I had to delve back into regular expression, which I hate because I don&#8217;t understand!</p>
<p>The program source is below. You should execute the program from the command line giving the with and height of the square you wish to draw, for example:</p>
<pre class="brush: bash">
java Squares 4x4
</pre>
<p>And the code:</p>
<pre class="brush: java">
import java.lang.String;
import java.util.regex.*;

public class Squares{
        public int dimensions[];

        public Squares(String[] args){
                if(args.length==1){
                        Pattern form=Pattern.compile("^\\S+x\\S+$");
                        Matcher fit=form.matcher(args[0]);
                        if(fit.matches()){
                                try{
                                        String[] dims=args[0].split("x");
                                        int width=Integer.parseInt(dims[0]);
                                        int height=Integer.parseInt(dims[1]);
                                        this.dimensions=new int[] {width,height};
                                }
                                catch(NumberFormatException e){
                                        System.err.println("Argument must be a string, width by height, in the form 6x6");
                                        System.exit(1);
                                }
                        }
                        else{
                                System.out.println("Argument must be a string, width by height, in the form 6x6");
                                System.exit(1);
                        }
                }
                else{
                        System.err.println("Please supply an argument, which must be a string, width by height, in the form 6x6");
                        System.exit(0);
                }
        }

        public String draw(){
                int i;
                int z;
                int width=this.dimensions[0];
                int height=this.dimensions[1];
                String square="";

                for(i=0;i&lt;height ;i++){
                        for(z=0;z&lt;width;z++){
                                if((i==0||i==height-1)&#038;&#038;(z==0||z==width-1))
                                        square+="+";
                                else if(z==0||z==width-1)
                                        square+="|";
                                else if((i==0||i==height-1))
                                        square+="-";
                                else
                                        square+=" ";
                        }
                        square+="\n";
                }

                return square;
        }

        public static void main(String[] args){
                Squares square=new Squares(args);
                String draw=square.draw();
                System.out.println(draw);
        }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/12/05/java-drawing-squares/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java &#8211; Calculate Taxi Fares</title>
		<link>http://blog.macaoidh.name/2009/12/02/java-calculate-taxi-fares/</link>
		<comments>http://blog.macaoidh.name/2009/12/02/java-calculate-taxi-fares/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 00:21:25 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[fare]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[Scratch]]></category>
		<category><![CDATA[taxi]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=281</guid>
		<description><![CDATA[This week my ICSP assignment was to calculate taxi fares. The task was: Using the fare table write a program that calculates the fares and test if it is correct. Upload your .java or your Scratch file. A template file for java will be provided. (Hint, only the boundary values needs to be tested. The [...]]]></description>
			<content:encoded><![CDATA[<p>This week my ICSP assignment was to calculate taxi fares. The task was:</p>
<blockquote><p>Using the fare table write a program that calculates the fares and test if it is correct. Upload your .java or your Scratch file. A template file for java will be provided. (Hint, only the boundary values needs to be tested. The fare table is also presented in the slides. The compile command for java should be &#8220;javac TaxiRide.java&#8221;)</p>
<table border="0">
<tbody>
<tr>
<th colspan="2">Fare table</th>
</tr>
<tr>
<th>Distance (km)</th>
<th>Cost (€)</th>
</tr>
<tr>
<td>&gt;50</td>
<td>30</td>
</tr>
<tr>
<td>50-100</td>
<td>25, plus 9 for each km &gt; 50</td>
</tr>
<tr>
<td>100 &#8211; 2000</td>
<td>190, plus 8 for each km &gt; 100</td>
</tr>
<tr>
<td>&gt; 2000</td>
<td>1800</td>
</tr>
</tbody>
</table>
</blockquote>
<p>It wasn&#8217;t the hardest task. The only thing that confused me was the Java switch syntax. In PHP you can have conditions in a switch statement such as:</p>
<pre class="brush:php">
&lt;?php
switch($num){
        case ($num&lt;10):
                // blah
        break;
        case ($num&lt;100):
                // blah
        break;
        default:
                //blah
?&gt;
</pre>
<p>I couldn&#8217;t get Java to accept anything other than a value for the variable bring switched. So I did it the old-fashioned way! My code is below&#8230;</p>
<pre class="brush:java">
public class TaxiRide{
        public int distance=0;

        public TaxiRide(String arg){
                try{
                        int distance=Integer.parseInt(arg);
                        this.distance=distance;
                }
                catch(NumberFormatException e){
                        System.err.println("Arguement must be an integer representing the distance traveled");
                        System.exit(1);
                }
        }

        public int calculateFare(){
                int distance=this.distance;
                int fare=0;
                int i=0;
                if(distance&lt;50){
                        return 30;
                }
                if(distance%lt;100){
                        fare=25;
                        distance-=50;
                        for(i=0;i&lt;distance;i++){
                                fare+=9;
                        }
                        return fare;
                }
                if(distance&lt;2000){
                        fare=190;
                        distance-=100;
                        for(i=0;i&lt;distance;i++){
                                fare+=8;
                        }
                        return fare;
                }
                return 1800;
        }

        public static void main(String[] args){
                TaxiRide taxiRide = new TaxiRide(args[0]);
                int fare=taxiRide.calculateFare();
                System.out.println("The fare is: "+fare+" euro for traveling "+taxiRide.distance+"kM\n Thank you\n");
        }
}
</pre>
<p>I feel i am learning a lot from this course. Lately I have been experimenting in a few different languages. I added a bit to the Scratch Linux installer, written in Perl, so that it added an icon in the applications menu during installation. I&#8217;ve also been messing around with bash, trying to automate some of my most common commands&#8230; was thinking of writing a small bash script that backed up my data when my external hard drive is plugged in, similar to Time Machine on OSX.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/12/02/java-calculate-taxi-fares/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Visual Hello World Popup</title>
		<link>http://blog.macaoidh.name/2009/11/15/java-visual-hello-world-popup/</link>
		<comments>http://blog.macaoidh.name/2009/11/15/java-visual-hello-world-popup/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 13:07:19 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[add text to jframe]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[jbutton]]></category>
		<category><![CDATA[jframe]]></category>
		<category><![CDATA[jpanel]]></category>
		<category><![CDATA[popup]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[visual]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=261</guid>
		<description><![CDATA[I spent yesterday going a bit deeper into Java with one of my lecturers. I decided to try and write a more complicated hello world app. It&#8217;s quite simple really once you get around the strict OOP and different syntax&#8230; This is how I did it: First you need to import some of the java [...]]]></description>
			<content:encoded><![CDATA[<p>I spent yesterday going a bit deeper into Java with one of my lecturers. I decided to try and write a more complicated hello world app. It&#8217;s quite simple really once you get around the strict OOP and different syntax&#8230; This is how I did it:</p>
<p>First you need to import some of the java libraries that we will be using:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
</pre>
<p>Then add the class, Popup. Make sure to name the file Popup.java</p>
<pre class="brush: java">
public class Popup{
private JFrame frame=new JFrame("Hello!");
private JPanel panel=new JPanel();
private JButton button=new JButton("Say Hello");
private JLabel label=new JLabel("Click the button to say hello...",SwingConstants.CENTER);
private ButtonListener buttonListener=new ButtonListener();
</pre>
<p>What that actually means is create a new instance of JFrame, JPanel, JButton and JLabel and store them in the frame,panel, button and label variables. As for the ButtonListener, we will deal with that later. Now we will deal with the launchFrame function which does what it says on the tin &#8211; launches the frame! Basically it just connects all of the pieces of information stored into the variables above and displays them together.</p>
<pre class="brush: java">
public void launchFrame(){
int height=400, width=600;
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
</pre>
<p>Above we create two new integers which store the width and height of our frame. The next line sets the variables to the frame size. The final line tells the Java application to close when the window, or JFrame, is closed. Now that we have the configuration out of the way we can move on to adding the variables to the frame.</p>
<pre class="brush: java">
frame.getContentPane().add(label,BorderLayout.CENTER);
panel.add(button);
frame.getContentPane().add(panel,BorderLayout.WEST);
button.addActionListener(buttonListener);
frame.setVisible(true);
}
</pre>
<p>The code above adds the label to the frame, then adds the button to the panel and then the panel to the frame. Once again disregard the button.addAction.. line for now. The final line simply sets the frame to visible so we can see it.</p>
<p>Now that we have the frame settled, we need to add our essential main function to start up the execution:</p>
<pre class="brush: java">
public static void main(String args[]){
Popup popup=new Popup();
popup.launchFrame();
}
}
</pre>
<p>This code basically creates an instance of our class, Popup and then executes the function launchFrame which we have created in that class.</p>
<p>That&#8217;s the guts of it, but now we need to talk about these lines:</p>
<pre class="brush: java">
private ButtonListener buttonListener=new ButtonListener();</pre>
<pre class="brush: java">
 public void launchFrame(){
....
button.addActionListener(buttonListener);
</pre>
<p>Basically we have previously created a button, but these lines define what happens when the button is pressed. The second line of code tells the machine to listen out for the clicking of the button, and when that happens to execute the variable buttonListener. We have created the variable buttonListener, which creates a new instance of the ButtonListener class which we have yet to create.</p>
<p>That is the next step, to create that class. Create a new file called ButtonListener.java and add this code:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;</pre>
<pre class="brush: java">
public class ButtonListener implements ActionListener{
</pre>
<p>The first four lines import the libraries that we are using. The final line is important. It states that our class, ButtonListener is implementing the ActionListener class. Below is the content of our new class:</p>
<pre class="brush: java">
private JFrame pressed=new JFrame("Hello World!");</pre>
<pre class="brush: java">
public void actionPerformed(ActionEvent actionEvent){
</pre>
<p>First, we create a new JFrame for our popup just like we did in the other file. The function actionPerformed is  a function from the ActionListener class that we are implementing. The parameters for the function are predefined so we need them. The rest of the code is very similar to the previous code so I won&#8217;t explain it in depth. Basically it just configures the new frame, adds the label and then displays it:</p>
<pre class="brush: java">
int height=160, width=200;
pressed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel pressedtext=new JLabel("Hello World!",SwingConstants.CENTER);
pressed.getContentPane().add(pressedtext,BorderLayout.CENTER);
pressed.setSize(width,height);
pressed.setVisible(true);
}</pre>
<p>Save that file and then compile both files by executing:</p>
<pre class="brush: bash">
javac ButtonListener.java Popup.java
</pre>
<p>Now if you run the program you should see a nice visual dialogue:</p>
<pre class="brush: bash">
java Popup
</pre>
<p>You can download a jar archive of the program <a href="http://files.macaoidh.name/Download/Java-Popup-Hello-World">here</a>.</p>
<p>The full code is below. Popup.java:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Popup{
        private JFrame frame=new JFrame("Hello!");
        private JPanel panel=new JPanel();
        private JButton button=new JButton("Say Hello");
        private JLabel label=new JLabel("Click the button to say hello...",SwingConstants.CENTER);
        private ButtonListener buttonListener=new ButtonListener();

        public void launchFrame(){
                int height=400, width=600;
                frame.setSize(width,height);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(label,BorderLayout.CENTER);
                panel.add(button);
                frame.getContentPane().add(panel,BorderLayout.WEST);
                button.addActionListener(buttonListener);
                frame.setVisible(true);
        }

        public static void main(String args[]){
                Popup popup=new Popup();
                popup.launchFrame();
        }
}
</pre>
<p>ButtonListener.java:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonListener implements ActionListener{
        private JFrame pressed=new JFrame("Hello World!");

        public void actionPerformed(ActionEvent actionEvent){
                int height=160, width=200;
                pressed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JLabel pressedtext=new JLabel("Hello World!",SwingConstants.CENTER);
                pressed.getContentPane().add(pressedtext,BorderLayout.CENTER);
                pressed.setSize(width,height);
                pressed.setVisible(true);
        }

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/11/15/java-visual-hello-world-popup/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Capture the Flag Part 3</title>
		<link>http://blog.macaoidh.name/2009/11/07/capture-the-flag-part-3/</link>
		<comments>http://blog.macaoidh.name/2009/11/07/capture-the-flag-part-3/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 20:48:38 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[blue]]></category>
		<category><![CDATA[capture the flag]]></category>
		<category><![CDATA[part 3]]></category>
		<category><![CDATA[players]]></category>
		<category><![CDATA[red]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=258</guid>
		<description><![CDATA[I have been flat out recently with school and work and this ICSP thing. Well anyway I have had no time to do any furasta or any independent spur of the moment programing! It&#8217;s driving me crazy, but hey, it&#8217;s all over in just over 6 months&#8230;. Capture the Flag Part 3 This time I [...]]]></description>
			<content:encoded><![CDATA[<p>I have been flat out recently with school and work and this ICSP thing. Well anyway I have had no time to do any furasta or any independent spur of the moment programing! It&#8217;s driving me crazy, but hey, it&#8217;s all over in just over 6 months&#8230;.</p>
<p>Capture the Flag Part 3</p>
<p>This time I have deviated from the rules a wee bit. The red player is controlled by the CPU, while the user controls the blue player. Once again the objective is to capture the red flag and return it to your blue base. If you bump into a player who has a flag then flag gets returned to it&#8217;s base.</p>
<p>The players, and flags have three random starting positions each. I have re-written this example from scratch, not using any of the code from part one or two. I am happy with the code this time, and have done my best to make it effective.</p>
<p>The red player has two modes, aggressive and defensive. When the game starts the red player attempts to capture the flag and return it to it&#8217;s red base automatically. If at any point the blue player acquires his flag the red player will go into defensive mode and attempt to recover his flag.</p>
<p>To throw a spanner in the works I have added a BOOST and an obstacle. The boost can gradually makes the player who absorbs it smaller to increase evasive skills. It also makes the player a lot faster. The obstacle has two modes. If you bump into it the wrong way, you will simply bump off it. If you follow the arrows through the obstacle with your player then you will receive a big speed bonus. Both these items will prove vital in winning against the red player, who moves faster than the blue one.</p>
<p>Controls</p>
<p>Up Key &#8211; Move Up<br />
Down Key &#8211; Move Down<br />
Left Key &#8211; Move Left<br />
Right Key &#8211; Move Right</p>
<p><applet id='ProjectApplet' style='display:block' code='ScratchApplet' codebase='http://scratch.mit.edu/static/misc' archive='ScratchApplet.jar' height='387' width='482'>
<param name='project' value='../../static/projects/ConorMacAoidh/749759.sb'></param></applet> <a href='http://scratch.mit.edu/projects/ConorMacAoidh/749759'>Learn more about this project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/11/07/capture-the-flag-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ICSP Week One Assignment</title>
		<link>http://blog.macaoidh.name/2009/10/25/icsp-week-one-assignment/</link>
		<comments>http://blog.macaoidh.name/2009/10/25/icsp-week-one-assignment/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 20:38:05 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[caputre the flag]]></category>
		<category><![CDATA[icsp]]></category>
		<category><![CDATA[Scratch]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=250</guid>
		<description><![CDATA[I started a course in UCD last week called &#8220;Introduction to Computer Science and Programing&#8221;, or ICSP. It&#8217;s gonna be going on for three months and then I will have a big exam which I will probably fail! The whole course centers around the use of Java, I&#8217;m not sure yet if we are going [...]]]></description>
			<content:encoded><![CDATA[<p>I started a course in UCD last week called &#8220;Introduction to Computer Science and Programing&#8221;, or <a href="http://www.csi.ucd.ie/content/introduction-computer-science-and-programming-icsp2009">ICSP</a>. It&#8217;s gonna be going on for three months and then I will have a big exam which I will probably fail!</p>
<p>The whole course centers around the use of Java, I&#8217;m not sure yet if we are going to be using anything else. We haven&#8217;t actually even started with Java yet, but we are using this program called <a href="http://scratch.mit.edu/">Scratch</a> to program our Java for us! I wouldn&#8217;t say that it is the Dream-weaver of Java, far from it. In fact I think that it is a great method of getting into Java. It has a set of preconfigured code which you can arrange into blocks, which then run &#8211; quite similar to the source of Java actually except simplified. It also eliminates the need for compiling.</p>
<p>Last week I went to my first &#8220;lecture&#8221; which was good enough, but I learned something valuable that day; computer hardware is not the road that I want to go down! I didn&#8217;t really enjoy the hardware aspect of the day but when it came to the Scratch lessons I was flying! For our first assignment we had to write a game in Scratch, which is essentially it&#8217;s own unique programing language. The game is called &#8220;Capture the Flag&#8221;. I was actually flat out all week and didn&#8217;t get a chance to start work on it until today, which is coincidentally the deadline for it&#8217;s completion! I spent about three hours making the game, here&#8217;s the manual and the game:</p>
<h3>Capture The Flag</h3>
<p><strong>Overview</strong><br />
Capture the flag is a simple two player game which involves stealing the flag from the opposite colours corner.</p>
<p><strong>Controls</strong><br />
Blue Player<br />
Up Arrow Key = Move Up<br />
Down Arrow Key = Move Down<br />
Left Arrow Key = Move Left<br />
Right Arrow Key = Move Right</p>
<p>Red Player<br />
W Key = Move Up<br />
S Key = Move Down<br />
A Key = Move Left<br />
D Key = Move Right</p>
<p><strong>Rules</strong><br />
To gain a point a player must capture the opposite colour flag and return it to their base (the starting point).</p>
<p>If a player is in possession of a flag and his opponent touches him the flag is returned to it&#8217;s base and the player to his base.</p>
<p>To win a player must gain three points.</p>
<p><strong>Faults</strong><br />
The one fault with the game is that, at least on my computer, when one player is using the keyboard to move the other player cannot move simultaneously. Although this is a fault it does also make the game a bit more interesting because the players have to fight even to move!</p>
<p><applet id='ProjectApplet' style='display:block' code='ScratchApplet' codebase='http://scratch.mit.edu/static/misc' archive='ScratchApplet.jar' height='387' width='482'>
<param name='project' value='../../static/projects/ConorMacAoidh/732336.sb'></param></applet> <a href='http://scratch.mit.edu/projects/ConorMacAoidh/732336'>Learn more about this project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/10/25/icsp-week-one-assignment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
