DOC

Getting Started with Java

By Jane Dunn,2014-04-24 12:53
15 views 0
Getting Started with Java

Getting Started with Java Practical 1: Java programming overview

    Practical 1: Java Programming Overview

1. Introduction

In this practical session you will do the following:

    ? Set up your homespace

    ? Open and configure a text editor (Notepad++) for use with Java

    ? Load a java program file

    ? Compile a Java program File

    ? Run a Java program file

    ? Edit a Java program file

    ? Write your own Java program file

Asher Rashid 1

    Getting Started with Java Practical 1: Java programming overview

    2. Homespace

2.1 What is it?

    Your home space is the location where you will save your work. This can be either the network or a portable drive.

    The network drive is identified as U:\ at SCM. If your computer account has been set up correctly then your u-drive will be named with your username.

    The u-drive is accessible via the internet from outside the university, but is not as convenient as portable USB storage. Accordingly I recommend students work from a portable USB drive, either:

    ? USB flash memory pen

    ? USB enabledMP3 player

    ? USB enabled mobile phone

    At SCM once an USB devices is connected the computers allocate drive letters F: and G; to the device.

2.2 Configuring Homespace

    Regardless if you use the network or a USB drive, you should structure your homespace directory, i.e. create folders for each session.

You can do this manually or use a zip file:

    ? From the session 1 web site download the file java.zip

    ? Once downloaded, extract the zip file

    ? A directory called Java will be created, with sub folders for sessions 1 to 7 and the ICA

    Each session, save your work in the relevant practical folder, e.g. today save your work in the folder Java/01/Pra01.

    Asher Rashid 2

Getting Started with Java Practical 1: Java programming overview

    3. Notepad++

    3.1 Starting Notepad++

    You can start notepad++ either through running the program shortcut

    ? Start > All programs > Notepad++ > Notepad ++

    Or by

    ? Typing notepad++ in the run dialogue (Start > Run)

    Notepad++ will open with a blank code window

Asher Rashid 3

    Getting Started with Java Practical 1: Java programming overview

    3.2 Configuring Notepad++

We need to set the text editor to show relevant colour formatting for java:

    ? Click: language menu

    ? Select: Java

3.3 Opening a Java Program File

If you downloaded the zip file then the hello world program file will be in pra01 sub folder.

    ? Use either File > Open or the open icon to search and open the HelloWorld.Java file

If you did not download the zip file, then you will have to type the file as shown below:

If you have to type the file, save it as HelloWorld.java in the pra01 sub folder

3.4 Console Window

After saving or opening, you will need to open a Console window, in order to compile and run

    the program file

    ? Click the ‘Show Console Dialog’ icon, i.e. the penultimate icon on the toolbar

    Asher Rashid 4

Getting Started with Java Practical 1: Java programming overview

    3.5 Compiling the java program

    Into the console window:

    ? Type: javac Helloworld.java

    ? Press: The Enter key

    The java compiler will check the program file and in this case will report some errors:

Asher Rashid 5

    Getting Started with Java Practical 1: Java programming overview

    3.6 Correcting the errors

The key statement is line 4

    ? System.out.println(‘HelloWorld’);

This line is supposed to display the words Hello World in the console window

The statement is composed of the following:

    ? Component: System.out

    ? Command: println()

    ? Value: ‘HelloWorld’

The above value is described as a literal string value:

    ? Literal means that the computer uses the value as written

    ? String means an alphanumeric value

The error is that all literal string values should use Double quotes instead of single quotes.

    ? Change the line so it looks like below:

    System.out.println("HelloWorld");

Now we are ready to compile it again

    ? Save the file

    ? Compile it again (type in the console window: javac HelloWorld.java)

If your file has no errors then the compiler will not report any errors

More importantly the compiler will create a Java Class file in the same location where your java

    program file is.

This class file can by run on any java platform, i.e. Windows 98/2000/XP/Vista/Server, Apple

    Mac, Linux, etc.

    Asher Rashid 6

    Getting Started with Java Practical 1: Java programming overview

    3.7 Running the class file

    After a successful compilation, the class file can be run (formal term is ‘Executed’)

To do so

    ? Type in the console window: java HelloWorld

    ? Press: The Enter Key

Note:

    ? We use java and not javac

    ? We do not include the file extension, i.e. no .java and no .class

    ? The class name must be case correct

You should now see the value HelloWorld displayed in the console window. Note the double

    quotes are not displayed.

    Not very exciting, but remember: The longest of journeys start with one small step.

Close Notepad++

    Asher Rashid 7

    Getting Started with Java Practical 1: Java programming overview

    4.0 Second Program

    Your final task for this session is to create, save, compile and successfully run a new java program, which has the following code:

    import java.util.*;

    public class SecondProgram

    {

     public static void main(String[] args)

     {

     Scanner in = new Scanner(System.in);

     System.out.print("What is your name? ");

     String name = in.nextLine();

     System.out.print("How old are you? ");

     int age = in.nextInt();

     System.out.println("Hello, " + name + ". Next year,

    you'll be " + (age + 1));

     }

    }

Note the statement:

    System.out.println("Hello, " + name + ". Next year, you'll be "

    + (age + 1));

    The above statement is too long to show on one line on an A4 page. When you type this line it should be on one line.

    Asher Rashid 8

Report this document

For any questions or suggestions please email
cust-service@docsford.com