Sunteți pe pagina 1din 6

Computer Studies 2013 Syllabus

First Program (02)


Creating a Java program and understanding the basic concepts.

Creating a Program in JCreator


It is usually a good idea to create a folder where youre going to save your Java programs. So the first step is to create a folder. Create a folder named My Java in My Documents

JCreator is then used to create our program. But the first step is to create a new file. 1. Start JCreator 2. Click on File > New File > File 3. Choose Empty Java File

4. Specify the filename in the space provided and choose the location where you want to save the file. In this case choose the previously created folder My Java. Click on Finish.
Mr. A. Gatt Page 1 of 6

Computer Studies 2013 Syllabus

5. That will create the empty Java file. 6. In the space provided were going to type our first program.

7. Now click on the Build File icon. This will compile the program. If you do not have any mistakes it will show you Process Complete in the Build Output, otherwise it will show you that you have errors.

8. Finally, the program can be executed by clicking on the Run Project.

Mr. A. Gatt

Page 2 of 6

Computer Studies 2013 Syllabus

9. This will execute the program and run it in a window.

10. When a program is compiled a new file is created with the class extension. This is the byte code which the JVM uses to run the program.

This is the source-code file typed in JCreator. Its contents can also be viewed with a basic text editor.

This is the byte code file created when the program is compiled.

Mr. A. Gatt

Page 3 of 6

Computer Studies 2013 Syllabus

Program Structure
This is the main class and class TestProgram { public static void main (String args[]){ System.out.println("Hello World!"); } The class is created here by giving it a name. This shows a message on The curly brackets show where a scope begins and ends. the screen. The semi-colon ends a statement. } where the program starts to execute.

Comments

are

//Program 1 : My First Program

used to remind the programmer what the code does.

Comments Comments are used to remind the programmer what the code does. They are called in-line documentation because they are found within the program itself and not as a separate document. There are two type of comments; single line and multi-line. Single line comments can only be on a single line. In order to create a single-line comment, // must be used before the comment. Multi-line comments can be spread over a number of lines, these type of comments must start with /* and then end with */. class The keyword class shows that a new class is being created. Just after the keyword, there is the name of the class, in this case TestProgram. Then the curly brackets are used to show where the code of the class begins and where it ends.

Mr. A. Gatt

Page 4 of 6

Computer Studies 2013 Syllabus

public static void main (String args[]) Every program must have one main method; this is where the program starts. It is made up of different parts: public this means that this method is available from outside the class static this method can be used without creating an instance of the class void this method does not return a value main the name of the method String args[] an array of type String which can accept a number of arguments in array args

Curly Brackets { } These show where a scope begins and ends. System.out.println(Hello World); This statement shows the text between the quotes on the screen; in this case Hello World. Since the keyword println() is used, the cursor will then move to the next line. The println() is found in the out stream in the System class. If text must be displayed on screen and the cursor has to remain in the same line print() can be used instead of println(). Semi-colon Every statement in Java must end with a semi-colon ;

Escape Characters
Escape characters can be used within quotes when using the println() method to format text. Escape characters include:

Escape Character \ \ \\ \n \t

Use Displays a single quote Displays a double quote Dispays a backslash Moves to a new line Insert a tab

Mr. A. Gatt

Page 5 of 6

Computer Studies 2013 Syllabus

Activities
1. Copy the following program and try to build it. Check for the errors, fix them and build it again.
class question1 { public static void main (String args[]) { System.out.println(This is the first line) System.out.println(This is the second line); System.out.println(This is the third line); } }

2.

Explain the difference between println() and print(). Write a program to show the difference.

3.

Write a program that shows your name, surname on one line and the address on another line but use only one println() method.

4.

Write a program that displays the names and marks of a number of students. It should be displayed as follows:
Name Alan Borg Peter Abela Anna Zarb Marks 90 23 65

5.

Write a program that displays a recipe ingredients and the quantity required. The items must be neatly aligned.

***

Mr. A. Gatt

Page 6 of 6

S-ar putea să vă placă și