Skip navigation

Introductory Programming In Java

Lab 1 Lab 2 Lab 3 Lab 4 Lab 5 Lab 6 Lab 7

Lab 1

Getting started with the computing environment,
extending and writing first Java programs

Objectives

This lab has two objectives: Introduce you to the computing environment — Ubuntu Linux, and start writing your first Java programs.

Logging in, taking a look around

Log on into your computer account — use your ANU UniID and password as the user name and the password. Check the front panel at the bottom of the screen: if it contains the Terminal icon, then left-click on it to open the command line window; if the front panel doesn't contain the icon, then by clicking on Menu icon on the left, choose System→Terminal to open it. Similarly, you can open the Kate text editor for later use. To place the icon of an often used application, right-click on the front panel, choose "Add application" and follow the selection dialog to find the necessary application and add its icon to the front panel.

The DCS provides the "User Guide" to the Student Computing Environment. Download the user guide, and if necessary, ask for a printed copy at the front desk. The guide has a listing of some commands available in the Terminal window (you can find them here), it explains some useful features of Kate, and more...

Next, open Kate text editor (make an icon for it in the front panel, as explained above). Open a text document (a new one, or a text of downloaded program). Check the user guide guide about how to activate and use a terminal window which is built-in in editor. Use it later to compile and run your Java programs.

Play with the command line and Kate for a while until you feel that you can get around with these tools.

If you prefer something less basic, and you know how to use it, here we can offer you Emacs, a powerful editor, or Eclipse, a modern sophisticated IDE (Integrated Development Environment). Launch them using the front panel (as described above), or use the command line:

eclipse &

Another widely used Java-IDE, Netbeans, can also be used by typing:

netbeans &

in the command line (and hitting the "Return" button).

But you will be (mostly) on your own — we do not have time to learn usage of these tools.

Exercise One

Write a program that takes two integers and prints out their sum, difference, product and average.

Change the values of these integers by editing your program. Then make the program read the values of integers from a user using JOptionPane as in the example from lectures (Block-1, Slide 46), and performs the above arithmetic operations.

Exercise Two

Download the free book How to Think Like a Computer Scientist, Java™ Version by Allen B. Downey, and do the Exercise 1.7 at the end of Chapter One, p.11-12: you have to type in (really, type it in, do not paste and copy!) the "Hello World" program (like it's presented in the lectures or in the Downey's book, p. 7). Compile (javac Hello.java) and rut (java Hello) it. Then follow the advice of breaking the initial code (in Ex 1.3, ai), to make small changes, and try to compile the program again. Read and think through the meaning of the compile messages.

Exercise Three

  • Write a program to remove the first character from a string which the user types in.

  • Write a program to remove the last character from a string which the user types in.

  • Write a program that calculates the number of characters in a string which user types in.

Note You will make your life simpler if you reuse a previous program to do the next task which may be similar (like in the Exercise Two). Copy an existing source file (on the Terminal this is done by the copy command:

> cp OldClass.java NewClass.java

and appropriate editing of the NewClass.java.)

Further programming ideas

Not for this week (relax).

Lab 1 Lab 2 Lab 3 Lab 4 Lab 5 Lab 6 Lab 7

 

Updated:  Tue 14 May 2013 01:03:20 EST Responsible Officer:   JavaScript must be enabled to display this email address. Page Contact:   Course Webmaster