The University of Western Australia CITS2200 Data Structures and Algorithms : School of Computer Science and Software Engineering : The University of Western Australia

Data Structures and Algorithms Lab Exercises 1

Aims

The aims of these exercises are to:

  1. Set up your laptop for Java development using the Eclipse Development Environment.

  2. familiarise yourself with the Eclipse development environment that you will use to write Java code, and

  3. refresh your memory of Java by completing a couple of simple exercises.

Students should read Weiss Chapter 1 in preparation for these lab exercises.

Exercises

  1. Getting Started with Eclipse

    In this unit it is recommended that you use the Eclipse integrated development environment (IDE) for your programming labs. In general, a GUI IDE is much better for large projects with many dependencies. Installing Eclipse on your laptop is required to enable you to complete programming exercises during the intensive lecture/workshop classes in November.

    Use this for instructions on how to install the Mars release of Eclipse on your laptop. Choose the Eclipse IDE for Java Developers package.

    You will also need a Java runtime environment (JRE) to use Eclipse (Java SE 7 or greater is recommended). This is most likely already installed on your laptop, but if you need to install Java, follow the instructions on the Eclipse web site.

    The first time you start Eclipse it is recommended that you take the Overview tour for Workbench basics and Java Development and take the tutorial Create a Hello World application. Being familiar with how to use Eclipse will save you a lot of time in the long run!

  2. Programming Exercise (Weiss, Exercise 1.19)

    Write a program to print out all pairs of positive integers (a,b), such that a < b < 1000 and (a2+b2+1)/(ab) is an integer.

    Recall, your program must contain the method:

    public static void main(String[] args)

    in order for the Java JVM to know what method to first call when your program is executed.

    Open Eclipse in the Applications Folder. You may be asked to nominate a workspace, in which case specify a directory where your work will be saved. Then select new Java class from the top menu. You will be given a number of options, but make sure that the package is left as default. This will generate some source code for you, and you can then complete the class. To run, press the green button with the arrow on it.

  3. Generalise your solution to question 4 by allowing the user to enter a positive integer, n at the command line, and then printing out all pairs of positive integers (a,b), such that a < b < n and (a2+b2+1)/(ab) is an integer.

    The list of words the user that types after java Q4 is passed to the main method as an array of Strings, so

    csse2100% java Q4 10 100 1000

    will call the method Q4.main({"10","100","1000"}). We can turn the first argument from a String to an integer by using the command Integer.parseInt(args[0]).

    You can make the similar modification in Eclipse. To add arguments to the command line, you need to change the action associated with the green arrow. Right click (or control-leftclick) the class name and select Run As and then Run Configurations. This will allow you to specify command line arguments.

  4. What happens if the user enters something that is not a number? We would like top throw an IllegalValue exception that is defined in the CITS2200 package. To do this you will have to

    1. Import the CITS2200 package into your file. You can download the CITS2200.jar to your working directory (or whereever you like). Add the line import CITS2200.IllegalValue; to the top of your class.
    2. Catch the NumberFormatException and throw an IllegalValue exception. Check the first workshop for instructions on how you might do this.
    3. Make sure that the CITS2200.jar file is accessible in your IDE. This is different for each IDE, so you should try to get it to work in each IDE. At the command line you just need to specify the classpath when compiling and running with the flag javac -cp [path-to-jar]CITS2200.jar:. YourClass.java

      In Eclipse, you need to add the jar. Write click the class and select Configure Build Path. Then add a user defined library. You can then add the jar file to the new library.

    End of lab exercises 1