1 / 22

IT151: Introduction to Programming

Learn Java programming fundamentals with an introductory text-printing program. Understand comments, white space, class declaration, reserved words, method structure, and more.

Download Presentation

IT151: Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IT151: Introduction to Programming Introduction to Java Applications

  2. // 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[ ]) • { • System.out.println(“Welcome to IT151”); • System.out.println(“Enjoy programming with Java”); • } // end main method • } // end class Welcome

  3. Line 1 – Line 3 • Comments • Begins with // • Comments are used to improve the program’s readability • Java compiler ignores all comments • To ways of indicating a comment • // end-of-line comment • /* … */ multiple-line comments

  4. Line 4, 12 • White space • Includes • Blank lines • Space characters • Tab characters • Makes the program easier to read • Ignored by the compiler

  5. 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

  6. 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

  7. 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

  8. Java is case-sensitive • That is: • Uppercase and lowercase letters are distinct • Examples: • TIHE is different from Tihe or tihe • Not using the correct letters for an identifier causes a compilation error

  9. 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

  10. 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

  11. Line 8 • The starting point of every Java applications • Your program must have this line, exactly as it is shown here! • Otherwise, your program will not be compiled by the compiler

  12. 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)

  13. 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

  14. 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

  15. Line 10 • Instructs the computer to perform an action • That is:- • To print a string of characters contained between the double quotation marks • White-space characters in strings are not ignored by the compiler

  16. Standard output • System.out is called the standard output object • Allows Java to display sets of characters in the window from which Java executes

  17. System.out.println • This method displays a line of text in the command window • Needs an argument, which is a string of characters • Outputs its argument to the command window • After completion, it positions the output cursor in the next line

  18. Line 11 • What does this line do?

  19. Statement • The entire line 10, is called a statement • Each statement MUST end with a semicolon • A 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

  20. // 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[ ]) • { • System.out.print(“Welcome to IT151”); • System.out.println(“Enjoy programming with Java”); • } // end main method • } // end class Welcome

  21. // 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[ ]) • { • System.out.println(“Welcome\n to\n IT151”); • System.out.println(“Enjoy programming with Java”); • } // end main method • } // end class Welcome

  22. Escape characters • The backslash (\) is called an escape character in Java • \n • \t • \r • \\ • \”

More Related