Conor Mac Aoidh
http://macaoidh.name
conor@macaoidh.name
 

Search

Archives

  • July 2010
  • June 2010
  • April 2010
  • March 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008

Spam

4,729 spam comments
blocked by
Akismet

Tag Cloud

    bt broadband CMS conor's management system conormacaoidh conor mac aoidh content management system drums fast Fedora fedora 10 furasta furasta cms furasta org gnome guide hello world HTML icsp iPhone Java javascript joomla kde Linux Mandriva mc kennas Monaghan Music mysql php repository Scratch stealing the ceiling The Dominican Affair the pot smoking pirates the strats tutorial Twitter updates web design forum webme webworks weekly tweets Windows wordpress

Ads

Java – Calculate Taxi Fares

Posted Dec 2nd, 2009 by Conor in in College,Java,Languages

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 fare table is also presented in the slides. The compile command for java should be “javac TaxiRide.java”)

Fare table
Distance (km) Cost (€)
>50 30
50-100 25, plus 9 for each km > 50
100 – 2000 190, plus 8 for each km > 100
> 2000 1800

It wasn’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:

<?php
switch($num){
        case ($num<10):
                // blah
        break;
        case ($num<100):
                // blah
        break;
        default:
                //blah
?>

I couldn’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…

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<50){
                        return 30;
                }
                if(distance%lt;100){
                        fare=25;
                        distance-=50;
                        for(i=0;i<distance;i++){
                                fare+=9;
                        }
                        return fare;
                }
                if(distance<2000){
                        fare=190;
                        distance-=100;
                        for(i=0;i<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");
        }
}

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’ve also been messing around with bash, trying to automate some of my most common commands… 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.

No responses yet

ICSP Week One Assignment

Posted Oct 25th, 2009 by Conor in in College,Games,Java,Languages

I started a course in UCD last week called “Introduction to Computer Science and Programing”, or ICSP. It’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’m not sure yet if we are going to be using anything else. We haven’t actually even started with Java yet, but we are using this program called Scratch to program our Java for us! I wouldn’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 – quite similar to the source of Java actually except simplified. It also eliminates the need for compiling.

Last week I went to my first “lecture” 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’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’s own unique programing language. The game is called “Capture the Flag”. I was actually flat out all week and didn’t get a chance to start work on it until today, which is coincidentally the deadline for it’s completion! I spent about three hours making the game, here’s the manual and the game:

Capture The Flag

Overview
Capture the flag is a simple two player game which involves stealing the flag from the opposite colours corner.

Controls
Blue Player
Up Arrow Key = Move Up
Down Arrow Key = Move Down
Left Arrow Key = Move Left
Right Arrow Key = Move Right

Red Player
W Key = Move Up
S Key = Move Down
A Key = Move Left
D Key = Move Right

Rules
To gain a point a player must capture the opposite colour flag and return it to their base (the starting point).

If a player is in possession of a flag and his opponent touches him the flag is returned to it’s base and the player to his base.

To win a player must gain three points.

Faults
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!

Learn more about this project

No responses yet

Siopa Geansaí

Posted Nov 16th, 2008 by Conor in in Games,Web Projects

I started and finished work on a computer game today. Gavin Mc Bride handed me a copy of scratch the other day and I decided to have a go! Scratch is a program based on Java, at least it has the same structure, but a nice graphical interface.

The game called “Siopa Geansaí” is located here. To be honest it is pretty crap but it was good enough crac making it. I had some display problems which I intent to fix (I am waiting for a response from the Scratch Forum), but besides that its a pretty simple game. It is set in a clothes shop – i know this may seem weird but it is explained below. The objective of the game is to pick the correct colour of Geansaí, if you do so you will win! As i said before simple!

You can use the arrow keys to move the Cat who changes colour when you put diferent Geansaí’s on him. There is a Geansaí Gorm, a Geansaí Glás and a Geansaí Dearg. Press the Enter/Return button to restart the game. Bring the geansaí to the check out to finish, that is where the display problems are happening… try to ignore them! :-)

The game is called “Siopa Geansaí” after the Geansaí Gorm SEO Competition which basically requires its participants to reach number one on the Google search results for “geansaí gorm” without having those words in the domain name. I couldn’t think of anything else to make a game about so I made it about this!!

Anyway enjoy the game, you can get back to me with feed back if you want but bear in mind that I know its terrible! :-) Oh yeah and

GEANSAÍ GORM GEANSAÍ GORM GEANSAÍ GORM

3 responses so far



Conor's Blog is powered by Wordpress | Template design by Conor Mac Aoidh