Windows Programming Using Java 1 Chapter 2 Introduction






























- Slides: 30

Windows Programming Using Java 1 Chapter 2: Introduction to Java Applications INSTRUCTOR: SHIH-SHINH HUANG

Contents 2 �Introduction �A First Program in Java �Text Displaying �Value Input: Integer Addition �Arithmetic �Equality and Relational Operators

Introduction 3 �Java Keywords abstract do import public throws boolean double instanceof return transient break else int short try byte extends interface static void case final long strictfp volatile catch finally native super while char float new switch class for package synchronized continue if private this default implements protected throw

Introduction 4 �Identifier Rule Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Identifier = (letter | '_' | ' $ ') {letter | digit | '_'}. �“Welcome 1”, “$value”, “_value”, “button 7” are valid �“ 7 button” is invalid Case �a 1 sensitive (capitalization matters) and A 1 are different

Introduction 5 �Primitive Data Type Purpose Contents Default Value* boolean Truth value true or false fales char Character Unicode characters u 0000 byte Signed integer 8 bit two's complement (byte) 0 short Signed integer 16 bit two's complement (short) 0 int Signed integer 32 bit two's complement 0 long Signed integer 64 bit two's complement 0 L float Real number 32 bit IEEE 754 floating point 0. 0 f double Real number 64 bit IEEE 754 floating point 0. 0 d

A First Program in Java 6 �Function: printing a line of text � � � � 1 2 3 4 5 6 7 8 9 10 11 12 13 // Fig. 2. 1: Welcome 1. java // Text-printing program. public class Welcome 1 { // main method begins execution of Java application public static void main( String args[] ) { System. out. println( "Welcome to Java Programming!" ); } // end method main } // end class Welcome 1 Welcome to Java Programming!

A First Program in Java 7 �Comments // remainder of line is comment �Comments ignored �Document and describe code 1 // Fig. 2. 1: Welcome 1. java Multiple line comments: /*. . . */ /* This is a multiple line comment. It can be split over many lines */

A First Program in Java 8 �Class Declaration 4 public class Welcome 1 { Every Java program has at least one defined class Keyword: class The words reserved for use by Java keyword followed by class name has to be an identifier Naming Convention: capitalize every word �Example: Sample. Class. Name

A First Program in Java 9 �Body Delimiter Left brace { �Begins body of every class �A corresponding right brace “}” ends definition (line 13) 4 public class Welcome 1 { 13}/* End of Class Welcome 1 */ Indentation Convention �Whenever you type an left brace “{“, immediately type the right brace “}”. �Then, indent to begin type the body.

A First Program in Java 10 �Program Entry Applications begin executing at main() �Exactly one method must be called main �Parenthesis indicate main is a method �Java applications contain one or more methods 5 public static void main( String args[] ) Methods can perform tasks and return result means main returns no information �args[]: input arguments in String data type. �void:

A First Program in Java 11 �Statements are instructions to commend hardware to perform some operations. It must end with semicolon “; ” 7 System. out. println("Welcome to Java Programming!" ); standard output object �System. out. println: displays line of text �System. out:

A First Program in Java 12 �Execution Steps JAVA PROGRAM EXECUTION Java source code Java compiler Welcome. javac Welcome. java byte-code . class byte-code interpreter JVM java Welcome EXECUTION

A First Program in Java 13 �Execution Steps Compiling a program �Open a command window, go to program’s directory. �Type javac Welcome. java �If no errors, Welcome. class created Executing a program �Type java Welcome to start JVM and then run the program Welcome. class �Interpreter calls method main

A First Program in Java 14 �Demonstration � 4 public class Welcome 1 { � 5 � 6 � 7 � 8 � 9 // main method begins execution of Java application public static void main( String args[] ) { System. out. println( "Welcome to Java Programming!" ); � 10 � 11 } // end method main � 12 � 13 } // end class Welcome 1

Text Displaying 15 �Displaying Methods System. out. println �Prints argument, puts cursor on new line System. out. print �Prints argument, keeps cursor on same line System. out. printf �Prints argument which is a format string 7 System. out. print("Welcome toto “); System. out. println("Welcome Java 8 System. out. println(“Java Programming!" );

Text Displaying 16 �Escape Sequences The backslash “” is called an escape character to indicate a “special character” is to be output. Backslash combined with character makes escape sequence. Escape Sequence Description n Newline t Horizontal Tab r Carriage Return. Position the cursor at the beginning of the current line \ Backslash ” Double Quote

Text Displaying 17 �Escape Sequences 7 System. out. println("Welcomenton. Javan Programming!" ); Welcome to Java Programming! 7 System. out. println(“”in quotes”" ); “in quotes”

Text Displaying 18 �Format String The first argument of printf() is a format string �Fixed Text �Format Specifier Format specifier is a placeholder for a value and specifies the type of data. �Percent Sign (“%”) �Data Type

Text Displaying 19 �Format String Type Character Input String Result %c character %d signed int signed decimal integer %f float real number, standard notation %s string 7 System. out. printf(“%sn”, “Welcome to”, “Java Programming!" ); Welcome to Java Programming!

Value Input: Integer Addition 20 �Requirements Read in two integers from users Compute the summation of them Print out the result on the screen Enter first integer: 1 Enter second integer: 3 Sum is: 4

Value Input: Integer Addition 21 �Variable Declaration Every variable has a name, a type, a size and a value �Name corresponds to location in memory When new value is placed into a variable, replaces (and destroys) previous value Reading them from memory does not change them int number 1=10; int number 1; number 1=10;

Value Input: Integer Addition 22 �Variable Declaration public class Addition { // main method begins execution of Java application public static void main( String args[] ){ int number 1; int number 2; int sum; …… …… }/* End of main */ }/* End of class Addition */

Value Input: Integer Addition 23 import java. util. Scanner; public class Addition { // main method begins execution of Java application public static void main( String args[] ){ …… // create Scanner to obtain input from command window Scanner input = new Scanner( System. in ); // read the first integer System. out. print("Enter first integer: "); number 1 = input. next. Int(); // read the second integer System. out. print("Enter second integer: "); number 2 = input. next. Int(); …… }/* End of main */ }/* End of class Addition */

Value Input: Integer Addition 24 import java. util. Scanner; public class Addition { // main method begins execution of Java application public static void main( String args[] ){ …… sum = number 1 + number 2; System. out. printf("Sum is: %dn", sum); }/* End of main */ }/* End of class Addition */

Arithmetic 25 �Description Arithmetic calculations used in most programs �Asterisk ‘*’ indicates multiplication �Percent sign ‘%’ is the remainder (modulus) operator Integer division truncates remainder 7 / 5 evaluates to 1 Modulus operator % returns the remainder 7 % 5 evaluates to 2

Arithmetic 26 �Operator precedence Some arithmetic operators act before others

Equality and Relational Operators 27 �Description A condition is an expression that can be either true or false. It is used in control statements (if, for, while) to change the execution flow of program Conditions �Equality can be formed by using Operators �Relational Operators

Equality and Relational Operators 28 �Equality/Relational Operators Standard Algebraic Java Equality Sample Meaning = == x == y x is equal to y? != x != y x is not equal to y ? > > x>y x is greater than y ? < < x<y x is less than y? >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y

Equality and Relational Operators 29 �Example import java. util. Scanner; public class Comparison { public static void main( String args[] ){ int number 1=100; int number 2=200; if(number 1 == number 2){ System. out. printf(“%d == %d n”, number 1, number 2); }/* End of if-condition */ if(number 1 != number 2){ System. out. printf(“%d != %d n”, number 1, number 2); }/* End of if-condition */ }/* End of main */ }/* End of class Addition */

30 www. themegallery. com