The University of Western Australia
School of Computer Science and Software Engineering
 
 

School of Computer Science and Software Engineering

CITS2200 Data Structures and Algorithms

The aims of this labsheet are to:

  1. familiarise yourself with the MacOSX environment, including both the GUI environment and the underlying Unix operating system,

  2. familiarise yourself with an environment that will check your code automatically and give you feedback and an approximate mark

  3. how to submit your lab solutions when you are ready to submit

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

Exercises

  1. 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:

    • Click your mouse on the screen. You will see a reboot button at the bottom-right corner. Click on that and choose the 'Restart' option.
    • Once the machine has booted, it will give you two options at the top-left corner. Choose 'Ubuntu' by using the arrow key on your keyboard (the lower arrow key). Now login with your Pheme credentials, your login id is your UWA student email and password is Pheme password.
    • Once logged in, click the right mouse button on a dark place of the screen and choose the 'Open in Terminal' option. A terminal will open. Now you can use any of the editors mentioned below. You can also use the 'gedit' editor on Ubuntu. Suppose you want to work on a file called Sorter.java, type : gedit Sorter.java and the gedit window will open. You can type your code and save it by clicking the 'Save' button at the top-right corner.
    • This will save your Sorter.java file on your desktop. You can now open the browser (you will find both firefox and chrome on the left menu), open the Moodle site, login and submit your code to Lab1 by cutting and pasting from gedit or another editor using the mouse.
    • You can also write and test your Java code in your account. Java is installed in Ubuntu (See below for details).
    You can also use Windows or Linux operating systems if you are doing the labs at home. It is possible to use a Linux environment from a virtual box in Windows. We will try to help with this, but setting up the virtual box is your responsibility. There are many tutorials on the internet for this. Also, we recommend writing Java programs using a text editor, and compiling and running Java programs from command line. You can use an IDE (Integrated Development Environment) if you wish, but you should remember that: 1) We cannot help you with IDEs, as there are too many; 2) Some of the IDEs inject extra code in your program that the Moodle server (more below) may not recognise. It is your responsibility to remove all such code before submitting to Moodle.

    • 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 Unix command line interface
    • 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.

  2. 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/
    • You will see a list of available units at that site and one of those is "CITS2200 Data Structures and Algorithms (2023S1)" with a picture of a Rubik's cube.
    • Click on that link and the login page will load. You have to first create an account by clicking the "Create new account" link.
    • You should use your student email address at UWA as your Username (outside email addresses will not work and you can only submit code to Moodle from your university account) and set a password of your choice. You will get an email in your UWA account confirming the creation of the account. Now if you click on the link for CITS2200 from the list of units, you will be asked for an enrolment key just once. The enrolment key is: welcometocits2200!. Please be careful to type in the enrolment key correctly, it is case sensitive and the '!' at the end is part of the key.
    • You can now submit your lab solution. You will see a list of labs, for example "Lab 01:sorting". Once you click on the link, you will get a textbox where you can paste your code and check the correctness and an approximate mark by clicking on the "Check" button at the bottom of the textbox. You will get feedback in another textbox below.
    • You can check your solution as many times as you like, before submitting. Your final attempt will be considered as your submission
  3. 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.
  4. 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.
  5. Some practice exercises (Not parts of labs, not assessed, just for practice)

    • 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.

      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

      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


This Page