22
IT151: Introduction to Programming Introduction to Java Applications

IT151: Introduction to Programming

  • Upload
    judson

  • View
    30

  • Download
    0

Embed Size (px)

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

Citation preview

Page 1: IT151: Introduction to Programming

IT151: Introduction to Programming

Introduction to Java Applications

Page 2: IT151: Introduction to Programming

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

Page 3: IT151: Introduction to Programming

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

Page 4: IT151: Introduction to Programming

Line 4, 12

White space Includes

Blank lines Space characters Tab characters

Makes the program easier to read Ignored by the compiler

Page 5: IT151: Introduction to Programming

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

Page 6: IT151: Introduction to Programming

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

Page 7: IT151: Introduction to Programming

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

Page 8: IT151: Introduction to Programming

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

Page 9: IT151: Introduction to Programming

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

Page 10: IT151: Introduction to Programming

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

Page 11: IT151: Introduction to Programming

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

Page 12: IT151: Introduction to Programming

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)

Page 13: IT151: Introduction to Programming

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

Page 14: IT151: Introduction to Programming

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

Page 15: IT151: Introduction to Programming

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

Page 16: IT151: Introduction to Programming

Standard output

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

the window from which Java executes

Page 17: IT151: Introduction to Programming

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

Page 18: IT151: Introduction to Programming

Line 11

What does this line do?

Page 19: IT151: Introduction to Programming

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

Page 20: IT151: Introduction to Programming

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

Page 21: IT151: Introduction to Programming

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

Page 22: IT151: Introduction to Programming

Escape characters

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