it151: introduction to programming

Post on 05-Jan-2016

30 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

IT151: Introduction to Programming. Introduction to Java Applications. // Text-printing program // Created by: Siua Fonua // Date: 14 Feb 2006 public class Welcome { // main method begins execution of Java Applications public static void main (String args[ ]) { - PowerPoint PPT Presentation

TRANSCRIPT

IT151: Introduction to Programming

Introduction to Java Applications

1. // Text-printing program

2. // Created by: Siua Fonua

3. // Date: 14 Feb 2006

4.

5. public class Welcome

6. {

7. // main method begins execution of Java Applications

8. public static void main (String args[ ])

9. {

10. System.out.println(“Welcome to IT151”);

11. System.out.println(“Enjoy programming with Java”);

12.

13. } // end main method

14. } // end class Welcome

Line 1 – Line 3

CommentsBegins with //Comments are used to improve the program’s

readabilityJava compiler ignores all commentsTo ways of indicating a comment

// end-of-line comment /* … */ multiple-line comments

Line 4, 12

White space Includes

Blank lines Space characters Tab characters

Makes the program easier to read Ignored by the compiler

Line 5

Class declaration Every program in Java consists of at least one class The keyword class is used to declare a class,

followed by the name of the class The name of the class is called it’s identifier

NOTE: YOU MUST DECLARE EVERYTHING IN JAVA

Reserved Words

Also known as keywords These words are reserved for use by Java Examples include:

class Public, and more to come

Don’t use any of the Java keywords for your own use. IT WILL CONFUSE THE COMPILER

Class Names Rules for naming your class

Contains only Letters Digits Underscore ( _ ) Dollar sign ( $ )

Does not contain spaces Does not begin with a digit

Convention All class names begin with a capital letter Capitalise the first letter of each word they include

Java is case-sensitive

That is:Uppercase and lowercase letters are distinctExamples:

TIHE is different from Tihe or tihe

Not using the correct letters for an identifier causes a compilation error

Public classes

Every class you declared public: Must be saved in its own file Class name must be the same as the filename Must be ended with the .java extension

Failing to follow these rules, result in errors Sometimes your class will not be compiled by

the compiler

Line 6, 14

Left brace, { Begins the body of the class

Right brace, } Ends the body of the class

It is a syntax error if braces do not occur in matching pairs

Line 8

The starting point of every Java applicationsYour program must have this line, exactly as it

is shown here!Otherwise, your program will not be compiled

by the compiler

Methods

A block of code in the program that performs a specific task

Methods are able to perform a task and returns information when they complete execution

Some methods will not return any information when they complete execution

All methods will have Return type Method name List of parameters (inside a pair of parentheses)

void methods

When the keyword void appears in front of the method name, it indicates that this method will NOT return anything when it completes execution

We’ll discuss later, the methods that will return information after execution

Line 9, 13

Left brace, {Begins the body of the method

Right brace, }The end of the method’s body

Whatever is between these braces, will be the actions to be performed by the method

Line 10

Instructs the computer to perform an action

That is:- To print a string of characters contained

between the double quotation marksWhite-space characters in strings are not

ignored by the compiler

Standard output

System.out is called the standard output object Allows Java to display sets of characters in

the window from which Java executes

System.out.println

This method displays a line of text in the command windowNeeds an argument, which is a string of

charactersOutputs its argument to the command windowAfter completion, it positions the output cursor

in the next line

Line 11

What does this line do?

Statement

The entire line 10, is called a statementEach statement MUST end with a semicolonA method is typically composed by one or

more statements that performs the method’s task

NOTE: omitting the semicolon at the end of a statement, is a syntax errorh

1. // Text-printing program

2. // Created by: Siua Fonua

3. // Date: 14 Feb 2006

4.

5. public class Welcome

6. {

7. // main method begins execution of Java Applications

8. public static void main (String args[ ])

9. {

10. System.out.print(“Welcome to IT151”);

11. System.out.println(“Enjoy programming with Java”);

12.

13. } // end main method

14. } // end class Welcome

1. // Text-printing program

2. // Created by: Siua Fonua

3. // Date: 14 Feb 2006

4.

5. public class Welcome

6. {

7. // main method begins execution of Java Applications

8. public static void main (String args[ ])

9. {

10. System.out.println(“Welcome\n to\n IT151”);

11. System.out.println(“Enjoy programming with Java”);

12.

13. } // end main method

14. } // end class Welcome

Escape characters

The backslash (\) is called an escape character in Java \n \t \r \\ \”

top related