G6DICP - Introduction to Computer Programming Excercise Sheet 1 by gregorio11

VIEWS: 12 PAGES: 2

									G6DICP


                    G6DICP - Introduction to Computer Programming
                             Excercise Sheet 1
Getting Started
Before you can compile a Java program, you must type the source code into a text editor, and
save it onto a hard disk or the network. The first part of this sheet is devoted to this.

1. Log into the SCSiT Windows XP system, and find a suitable text editor. It is suggested that
   you use JFE (available on the machines in the IT terminal room). If you are already familiar
   with another programmer’s editor such as PFE or EMACS then you may use that. It is very
   strongly suggested that you do not use Windows Notepad or MS Word. Although they are
   both capable of editing text you will find them extremely limiting as programmer’s editors.
2. Using an editor such as JFE create a new file, and type in the source code of a simple Java
   program.

     Your first program should be a "Hello World" program similar to the following:

         class firstprogram
         {
              public static void main (String[] args)
              {
                   System.out.println("Hello World!");
              }
         }

     Once you have typed in your program, then save it to a file with the same name as the main
     class file, but use the “.java” suffix - eg the above code would be saved in a file called
     “firstprogram.java”.
3.   You are advised to save your files into your personal file store on the SCSIT servers – this
     should map as your drive H: from the SCSiT Windows XP systems. In order to be organised,
     you should create directories for your work (eg H:\javalabs – for lab excercises etc). If you
     write any files onto the local hard disk (drive C:), then please make sure that you delete them
     when you finish your session. If you wish to take files home on a floppy disk (drive A:) then
     do not read & write files directly from the floppy (and certainly do not attempt to compile
     files on a floppy) – that can result in data loss. It is much safer to save files onto the network
     or hard disk, and then copy them onto the floppy disk using Windows Explorer.
4.   Open a command line window, and move to the directory in which you saved your source
     code (eg if you saved the source code in H:\javalabs then type cd javalabs ).
5.   Compile the code using the javac compiler. Assuming that you have named your program
     as above, then this will be done by typing: javac firstprogram.java . If there are
     any syntax errors, then a message will be displayed, and they must be fixed before another
     attempt is made to compile the program. If there are no errors, then the program will compile
     without any message and a bytecode file called firstprogram.class will be created.
6.   Execute your program by running the interpreter with the command java
     firstprogram. NB you must not use a file extension here – the interpreter will
     automatically look for a .class file (if you type “java firstprogram.class”
     then the interpreter will look for a file called firstprogram.class.class, and
     give you a “file not found” error. NB compare your output with the screenshot shown
     overleaf.
7.   If there are any error messages, then read the above instructions very carefully to work out
     where you have gone wrong. However, if all goes well you should now see your program
     executing – contratulations! you have just written your first Java program.



Tim Brailsford                                                                          October, 2003
G6DICP


The session described overleaf should look similar to this.




Further Exercises
1. Write, comile and execute another program (with a different name), that prints out several
   lines of text. You might like to print out a short poem or your favourite saying.
2. Modify the above code so that instead of using the System.out.println() method you use
   System.out.print(). What is the difference? Can you think how you might make use of this in
   programs.

If you are new to programming, then do not attempt the following until after lectures 3 and 4. If
you have some experience with a programming language such as C or C++ though, you might
want to try these immediately.

3. Write a program that calculates the factorials of all numbers from 1 to 12, and displays this in
   a table (NB the factorial of 4 is 4x3x2x1 etc).
4. Write a program that prints a labelled Centigrade to Farenheight conversion table. (NB to
   convert from degrees C to degrees F you should multiply by 9, divide by 5 and then add 32).
   Don’t worry if the output is a bit ugly at this stage – we will see how to control the formatting
   of the output in the near future.




Tim Brailsford                                                                       October, 2003

								
To top