Overview

Java is a powerful high-level object-oriented programming langauge originally developed by James Gosling at Sun Microsystems and released in 1995.  Syntactically Java is very similar to C/C++.  Source code is written using a text editor and saved in a file with a .java extension.  It is then compiled using the Java compiler into a .class file which contains the bytecode for the application.  The class file is then run by the Java Virtual Machine.  Java is platform independent which allows it to run with few implementation dependencies.

Hello World

To write your first application, the quintessential “Hello World” program,  you will need to first create a directory for your work.  Open a terminal (ctrl + alt + t) and navigate to your home directory by typing cd then return.  After you have navigated to your home directory, create a new folder by typing mkdir java.  Next you will create a source file using a text editor.  Open the text editor by typing gedit & at the terminal.  This will open a text editor and leave the terminal available for more commands.  Type the following code into the text editor and save it in your new directory as HelloWorld.java.

  1.           /*
  2.            *  HelloWorld application prints “Hello World” to the standard output
  3.            */
  4.           class HelloWorld {
  5.                   public static void main (String [] args) {
  6.                           System.out.println (“Hello World”);  //Displays the string
  7.                   }
  8.           }

The first three lines are a multiple-line (or traditional) comment that states the purpose of the application.  Line 4 is the class declaration.  Every Java application contains at least one class declaration.  The keyword class is followed by the class name.  By convention, all class names begin with a capital letter and capitalise the beginning of each subsequent word.  The name of the file must be the same as the name of the class containing the main method followed by a .java, hence the source file above must be saved as HelloWorld.java.  The main method in line 5 is the starting point for every Java application.  Applications will typically contain many methods, but every application must contain exactly one main method.  The public keyword indicates the access granted to the main method.  You will study more about access a little later.  The static keyword likewise will be discussed later.  The void keyword indicates that this method does not return a value.  As you will see, many methods will return a value at their conclusion.  The parameter String [] args specifies command line arguments in the form of an array.  Line 6 specifies the function of this simple application.  System.out refers to the standard output, in this case the console, and println will print the string “Hello World” and include a carriage return at the end.  Line 6 also contains another kind of comment called an in-line comment.  Notice the opening and closing braces and the indentation that helps with the readability.

Once the source code is complete, you are ready to compile the application into a .class file.  At the terminal make sure you are at the directory that contains your source file (type cd java) and then type javac HelloWorld.java.  Assuming everything goes well this will generate a byte code version of your program in a .class file by the same name.  To run your program type java HelloWorld in the terminal.  This should print the phase “Hello World” in the terminal.  If there are any compilation errors, check the error message to identify the source of the error.  Also you may want to refer to common errors and their solutions at http://download.oracle.com/javase/tutorial/getStarted/problems/index.html.

Class Activity

  1.  If you are unfamiliar with or just starting Java, please read over the Language Basics section of the Java tutorial from the Oracle website, http://download.oracle.com/javase/tutorial/java/TOC.html.
  2. After reading the basics, read the section below entitled User Input.
  3. Complete the three exercises below.

** If you have previous experience with Java, please demonstrate your fluency by completing one of the exercises below.

User Input

The three exercises below require you to take input from the user.  Although there are many ways of capturing user input, one of the simpler methods is to use the Scanner class.  The program below demonstrates how the Scanner class can be used to acquire user input through the standard input, i.e. the terminal.

  1.           import java.util.Scanner;
  2.           class SimpleSum {
  3.                   public static void main (String [] args) {
  4.                           Scanner myScanner = new Scanner(System.in);
  5.                           int a = myScanner.nextInt();
  6.                           int b = myScanner.nextInt();
  7.                           int sum = a + b;
  8.                           System.out.println (“The sum is: ” + sum);
  9.                   }
  10.           }

Before you can use the Scanner class, you need to import it into your application as shown in line 1.  The first thing that you need to do to use the Scanner class is to create an instance of Scanner as shown in line 4.  This creates an instance, myScanner, and passes it the standard input, System.in.  You can then use your instance of Scanner to capture the next integer input from the user.

Exercises

  1. Take any three numbers from the user and put them in order – make your code as elegant as possible (not just a bunch of if-then statements)
  2. Build a basic calculator
  3. Program a simple game (craps, blackjack, etc.)

Homework

  1. If you are new to Java spend some more time with the above tutorial, especially the section Classes and Objects.
  2. Familiarize yourself with the Webots user guide and reference manual.
 

Leave a Reply

Set your Twitter account name in your settings to use the TwitterBar Section.