MSc introductory lab 2: programming under Linux John Sargeant (johns@cs.man.ac.uk) Purpose The main purpose of this lab is to check your Java programming skills via a simple programming exercise. This is important since many of the course units, and most projects, involve programming and Java is by far the most common language used in teaching here and elsewhere, as well as being one of the most commonly used languages in industry. If you’re a strong Java programmer the exercise should only take an hour or so. If not, then by doing the exercise you, and we, will be able to tell whether this is likely be a problem, and whether, for example, it should influence your choice of themes (and eventually of project). If you have real difficulty with this exercise, please come and talk to me. If you can do the exercise simply by following this script, that’s ideal. If you need help, ask one of the lab staff; in particular if you are familiar with C++ or C# rather than Java, you will probably be able to do it with a bit of help on some Java specifics. For people new to Linux this lab also provides some extra practice in using Linux commands. Prerequisites Before you start this lab, you should have done at least the following from the introductory lab: • Logged into Linux and entered a window manager • Changed your password • If you are new to Linux, have done at least enough of the command line tutorial that you can create directories, list fines etc. • Run a text editor and a command window (IDEs such as NetBeans and Eclipse are available, but using an IDE would be overkill for such a simple program). You’ll probably need the access the Java API documentation which is at http://download.oracle.com/javase/7/docs/api/ Specifically, you’ll probably want to make use of stuff in the java.util package. The exercise is to write a simple traffic queue simulation, so create a directory called trafficqueue to put your files in. The other thing you need to know is how to compile and run Java programs from the command line. The command javac filename.java compiles a Java file and either displays errors or creates a file called filename.class which can then be executed by the command java filename (note that you don’t need the .class extension). There are many other ways to compile and run Java code but the basic commands are all we need here. Step 1: the simplest possible traffic queue Firstly, to have something to put into the queue, we need a class representing road vehicles. To keep this simple, for now a road vehicle will have a single attribute, its colour, and that will just be a string. Now we need the queue itself. Normally we will add vehicles to the back of the queue and remove them from the front. However your implementation should be such that it will be possible in the future to add at the front and/or remove from the back (although you don’t need to implement those operations now). You should also be able to display the state of the queue as textual output (which will appear in the command window when you execute the program). There are many was to implement a queue: think carefully about what’s appropriate given the information you have so far. Check that your code works correctly by adding some vehicles to the back of the queue, removing some from the front, and showing the state at the queue at each step (for the moment the output will just be a series of colours). Step 2: different kinds of road vehicles Now let’s make it a bit more interesting by having two different kinds of road vehicles, cars and fire engines: • Cars have a make as well as a colour, and the textual output should give the colour and the make, e.g. “blue Ford” • Fire engines are always red. Also, the have two states, a normal state where they are added to the back of the queue as usual and a “code blue” state (i.e. the blue light is flashing) where they go straight to the front of the queue. The textual output for a fire engine in the normal state should be just “Fire engine” while for one in the code blue state is should be “Fire engine: CODE BLUE!!” To keep things simple we will assume that a fire engine will not go code blue while in the queue, so we only need to check when we add it. Check that this works correctly, in particular that fire engines do jump the queue iff they are in the code blue state. Step 3: Adding random vehicles By now you’ve probably got bored adding and removing one vehicle at a time, so let’s make it more like a real simulation by adding random vehicles automatically. Implement a method which will return a random vehicle as follows: • Some proportion (e.g. 1 in 5) of the vehicles are fire engines, the rest are cars • Some proportion (e.g. 1 in 3) of the fire engines are in the code blue state, the rest of them are in the normal state • The colour and make of each car is chosen randomly from a number of options. Hint: the java.util.Random class will be very useful here. Test this by adding some (e.g. 5) random vehicles to the queue, and then removing one and adding one some number (e.g. 10) times. Sample output from the standard solution using these parameters is: [red lada , red lada , blue BMW , blue lada , red lada ] [Fire engine CODE BLUE!! , red lada , blue BMW , blue lada , red lada ] [red lada , blue BMW , blue lada , red lada , orange Jaguar ] [blue BMW , blue lada , red lada , orange Jaguar , green Jaguar ] [blue lada , red lada , orange Jaguar , green Jaguar , green BMW ] [red lada , orange Jaguar , green Jaguar , green BMW , orange Jaguar ] [Fire engine CODE BLUE!! , orange Jaguar , green Jaguar , green BMW , orange Jaguar ] [orange Jaguar , green Jaguar , green BMW , orange Jaguar , red Jaguar ] [green Jaguar , green BMW , orange Jaguar , red Jaguar , blue BMW ] [green BMW , orange Jaguar , red Jaguar , blue BMW , green Jaguar ] [orange Jaguar , red Jaguar , blue BMW , green Jaguar , orange lada ] Once you have a completed solution (or partial solution), show it to a member of lab staff, who may be able to offer suggestions on how to improve it. Optional extras The obvious next step is to implement the case where a fire engine goes code blue while in the queue. To do this we need to know that this has occurred. One way to do this is to loop through the queue and check each vehicle in turn at every step. Can you think of two other ways to do it which avoid using a loop? If so, which way do you think is best? Feel free to extend the application, e.g. by having a graphical representation of the queue with fire engine graphics, blue flashing lights etc. On the other hand you probably have more pressing things to do this week. You might want to think about how would you go about implementing a complete road traffic simulation over an arbitrary road network. (This is a much harder problem; modelling junctions is non-trivial.)