Exercise Sheet A

General Information

Derek, what are you expecting from me?

Why should I bother?

Apart from the sheer intellectual joy of it?

Well, because some of the exercises will count towards your overall mark for CS2000.

Which exercises will count towards my overall mark?

This information will not be divulged until the end of the year. You are therefore advised to tackle all exercises.

How many exercises will count towards my overall mark?

Again, this is not divulged until the end of the year. Last year, it was 4 out of 7; three years ago it was 3 out of 8. But this year it could be more or fewer.

In first-year, Dr. Manning awarded you a mark based on your best m from n pieces of work. In other words, he wrote off your weakest piece of work. We do not do this in second-year. You are therefore advised to make a good job of all exercises.

Will each exercise count equally?

No - not necessarily. I typically combine your marks using a weighted average so that what I consider to be the more difficult pieces of work count for more.

What are the marking criteria?

I refer you to lecture 1, where we discussed software quality.

At some point in the not too distant future, you should read the CS2000 Style File, which tells you how to lay out your programs. (You can read it now if you wish, but not all of it will make sense yet.)

Will there be any feedback during the year?

Yes. This term we'll be doing the following:

Can I use my laptop/PC at home to do these exercises?

Yes, but...

Should I use Linux or Windows, or should I work on the server?

Linux.

What help will there be?

You can speak to me after any lecture. You can email me or call into my office. (My office hour is 4-5 on Thursdays.) And, most importantly, you can speak to me or my helpers at the practicals.

Can I get help from my friends?

To some extent, yes.

Discuss your plans for tackling an exercise. Discuss concepts that you are having trouble with. Your friends may have found a way of explaining something that works better for you than the ways we explain things.

But, do not cog.

Do not look at your friends' files; do not type their code into your files; do not ask them to send you their files.

In no way, shape or form should you submit work as if it were your own when some or all of it is not.

Be warned that I think that cogging is contemptible and I will apply the maximum penalties both to the person who submits the cogged work and to the person who helped them.

You are a member of a large class and so you might think that we won't detect your cogging.

Well, I have a program which automatically compares every submission with every other submission and reports every suspicious similarity. Note that it does not simply look for exact copies. The program knows all the usual tricks for disguising cogging.

How is work collected in?

All of the work that you do for this course should be put into a particular subdirectory of your home directory. I want this directory to be called cs2000 (use lowercase `c' and `s' and zeros not the letter 'O'). It is essential that you spell this directory name exactly as I have specified it here.

I have a program that I can run to automatically collect work from your cs2000 subdirectory. (If you've not named your directory correctly, my program will not find your work and you will score zero.) If the program has trouble, it may, on occasion, be necessary for me to manually inspect the contents of your cs2000 directory. So, do not put into this subdirectory anything you do not want me to see!

Begin every file with comments so that we can identify whose work is whose. The first four lines of your file should be along the following lines:

/**
 * A description of the contents of this file
 * @author Your name  Your student id  Your College email address
 */

Obviously, anonymous work results in zero marks.

So now what?

Make a subdirectory of your cs2000 directory. Call this subdirectory sheetA (all lowercase except 'A'). (Again, you must spell this exactly as I have specified it.)

Now tackle the exercise. Put all your work for this exercise sheet into your cs2000/sheetA subdirectory. We will collect the work at 5 p.m. on Friday 25th October.

You will see that there is also a 'challenge' exercise. Challenge exercises are always optional. They will not form part of your year's work and they are not worth any marks. They are designed for those students who finish the main exercise but who want to push their programming further.

Exercise

This exercise is about URLs. You all surf the web, so you should know what a URL is. It was also covered in CS1020. (Still no clue? See this dictionary entry and/or this beginner's guide.)

In your program, you will be sending messages to String objects. Use the ideas from lecture 4. Do not convert the Strings to arrays of chars. (We are not in first-year now!) If you use arrays, you will score zero.

The exercise has a trivial solution if you use Java's URL facilities (not yet covered in lectures). Since I don't want the exercise to be trivial, you may not use these facilities in your solution. If you do, you will score zero.

Create a file URLParser.java (in your cs2000/sheetA directory). Again, correct spelling is essential.

Your file should contain the following:

import javax.swing.JOptionPane;

/**
 * A program that parses a user-supplied URL, printing Yes if the
 * user input is a legal URL and printing No otherwise.
 * @author   Your name   Your student id   Your College email address
 */
public class URLParser
{
   public static void main(String[] args)
   {  while (true)
      {  String input =
            JOptionPane.showInputDialog("URL: ");
         if (input == null) // Cancel was clicked
         {  System.exit(0);
         }
         input = input.trim();
         System.out.println(input + " " + parse(input));
      }
   }

   public static String parse(String theInput)
   {  Your code goes here
   }
}

The above main method does the following repeatedly. It displays a little dialog box on the screen, into which the user can type some input:

If the user clicks Cancel, execution of the program ends. (By the way, if the user closes the dialog box by clicking the cross in the top right-hand corner, execution of the program ends and an error message is printed. I decided not to remedy this in the interests of keeping this code concise.)

If the user presses Return or clicks OK, whatever s/he typed into the textfield is stored in the variable called input. Then we send a message to this variable to trim off leading and trailing spaces. This has the nice effect of tidying the string up before we do anything more to it. (By the way, why do we write

input = input.trim();

instead of just

input.trim();

You can work out the answer from things you were told in lecture 4. But if you're not sure, speak to me or one of my helpers.)

The last line of the loop calls another method whose name is parse. It is your job to write the parse method.

The parse method takes in a string, checks whether the string is a legal URL and returns "Yes" or "No" accordingly.

The rules for what constitute a legal URL are surprisingly complicated. So, in this exercise, we will use a hugely simplified set of rules. For the purposes of this exercise, we assume that a URL must consist of three parts:

The rules that we will use in this exercise are:

The protocol is separated from the hostname by "://". The hostname is separated from the path by a fowards slash.

Here are some examples of URLs which, according to our rules, are legal and illegal.

Legal (in this exercise) Illegal (in this exercise)
http://www.cs.ucc.ie/dgb www.cs.ucc.ie/dgb
http://www/dgb http://www.cs.ucc.ie/
http://www.cs.ucc.ie/dgb/swd www.cs.ucc.ie/dgb
http://www.cs/dgb/swd/exA http:/www.cs.ucc.ie/dgb
HtTp://wWw.cS.ucc.Ie/dGb http://www.cs.ucc.ie/dgb/exA.html
http://w/d http://www.cs.ucc.ie/dgb/
http://w.w/d/d http://www..cs.ucc.ie/dgb
http://dgb.exA/www/cs/ucc/ie empty string

If you are disappointed by how much these simple rules deviate from the real rules, then you may wish to tackle the Challenge Exercise.

Challenge Exercise

Remember, challenge exercises are optional. They never count towards your final mark. (If you do tackle them, put them into the proper directory and we will try to find time to look over your solution and give you some feedback.)

Create a subdirectory of your cs2000/sheetA directory called challenge. In your challenge directory, write another version of URLParser. Follow this link to see some diagrams (called `syntax diagrams') that specify the set of rules that you should use. These rules are still simplified but are much closer to the real set of rules. In these syntax diagrams, strings that appear within ovals should appear in URLs. But a name inside a rectangle is the name of another diagram. If you're not sure how to make sense of these syntax diagrams, ask me or one of my helpers.

This is very challenging for novice programmers, so here's a few hints.

I suggest that you write one method per syntax diagram. The parameter to the method should be an int that tells you where to start parsing from, i.e. from what position in the user's input string. The return value from the method should also be an int. Ordinarily, it will tell you where parsing has finished, i.e. the position in the string that you have reached by the end of the method. But if the user's input does not match the diagram, you should return -1.

Each method should try to match the user's input against the parts of the diagram. Where there are ovals, you are seeing whether strings match. Where there are rectangles, you call another method. Where there are alternative branches, you use an if statement.

Speak to me or one of my helpers if you need further hints.