The aims of this labsheet are to:
familiarise yourself with the MacOSX environment, including both the GUI environment and the underlying Unix operating system,
familiarise yourself with an environment that will check your code automatically and give you feedback and an approximate mark
how to submit your lab solutions when you are ready to submit
refresh your memory of Java by completing a couple of simple exercises.
Getting Started with Ubuntu
The only supported environment for this unit in the labs is the Windows/Linux (Ubuntu) operating system. I would recommend you use the Ubuntu operating system for doing the labs. The machines in Lab 2.03 are booted in Windows by default. Here are the instructions for working in Linux:
Programming Conventions
When you leave University and work for a company as a programmer, you will be required to follow the programming conventions specified by that company. This provides the company with uniformity across different coders and makes their systems easier to maintain. Similarly, all code submitted for assessment in this unit must comply with the unit's Java Programming Conventions. Code that does not follow the conventions will lose marks. If you have not already read these conventions, you should do so now.
Writing your Java code for the labs
How you choose to develop your Java programs is up to you. Though we strongly advise you to use the command line interface for writing your code, the choice of development environment (or IDE) can be a personal one, with some people adamant one approach is superior to others. In this unit you can complete your work in whichever development environment you choose. Please note that while you are welcome to use whatever tools you like to complete your work for this unit, many IDEs make assumptions in their default configurations that will cause your code not to compile when tested. We are not able to support all IDEs, so if you choose to use an IDE, it is still your own responsibility to ensure your submissions are in the right format. We have provided instructions on how to compile and run your code on the command line, and your submissions are expected to work under this configuration.
The setup in CSSE Lab 2.03 provides a number of options, including Eclipse (a free, cross-platform IDE), Xcode and the Unix command-line using standard Unix editors, among others. Each has its advantages and disadvantages, so take the time to explore each option and choose which is best for you. Some people prefer the "prettiness" of a GUI IDE; others prefer the simplicity of the command-line. In general, a GUI IDE is much better for large projects with many dependencies, whilst the command line environment is better for quick prototyping and scripting.
How to check your code (and get an approximate mark)
You can test the correctness of your code and get an approximate mark at this site: https://quiz.jinhong.org/Simple editors for writing code
Some of the simple text editors for writing Java code are emacs, vim and nano. Here is a tutorial for vim. Here is a tutorial for nano. Here is a tutorial for emacs. Please note that you need to know only one editor, and it only takes a few minutes to learn the basics. Please do not use Microsoft Word, as it introduces hidden control characters that will create problem with Moodle.How to compile and run Java programs from command line
Once you have written your Java code using a text editor, you can run your code from the command line. Java must be already installed in your computer. Otherwise, just search in google "How to install Java" with the name of your operating system. If you are working on a lab, it is enough to submit your code within the provided skeleton (each labsheet has such a skeleton). You can also test your code in your local computer. Here are some instructions how to do that. The main difference is that you have to write a main method as part of your class. You can then use the javac command to compile the Java code, and java command to run the class file. I will explain the reasons in the tutorial.Some practice exercises (Not parts of labs, not assessed, just for practice)
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.
Write your method using the command line first. You should write the file using an editor such as Emacs, Vim or Nano. Emacs should be available in the Applications folder, and you can run vim or nano just by typing vim or nano at the command line.
You can compile your code from the Unix command-line (terminal) using the javac
compiler:
csse2100% javac Q4.java
and execute it with the java
interpreter:
csse2100% java Q4
Remember, the name of the file must match the name of the (principal) class defined within the file.
So, in the example above, the file Q4.java
must contain a class called Q4
.
When executed in this manner, output produced by your program will appear in the terminal window.
Generalise your solution to the question 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]).
What happens if the user enters something that is not a number? We would like to 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