The aims of these exercises are to:
Set up your laptop for Java development using the Eclipse Development Environment.
familiarise yourself with the Eclipse development environment that you will use to write Java code, and
refresh your memory of Java by completing a couple of simple exercises.
Students should read Weiss Chapter 1 in preparation for these lab exercises.
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
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!
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.
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.
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
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.