300 likes | 414 Views
Windows Programming Using Java. Instructor: Shih- Shinh Huang. Chapter2: Introduction to Java Applications. Contents. Introduction A First Program in Java Text Displaying Value Input: Integer Addition Arithmetic Equality and Relational Operators. Introduction. Java Keywords.
E N D
Windows Programming Using Java Instructor: Shih-Shinh Huang Chapter2: Introduction to Java Applications
Contents Introduction A First Program in Java Text Displaying Value Input: Integer Addition Arithmetic Equality and Relational Operators
Introduction Java Keywords
Introduction • Identifier Rule • Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) • “Welcome1”, “$value”, “_value”, “button7” are valid • “7button” is invalid • Case sensitive (capitalization matters) • a1 and A1 are different Identifier = (letter | '_' | ' $ ') {letter | digit | '_'}.
Introduction • Primitive Data Type
A First Program in Java • Function: printing a line of text • 1 // Fig. 2.1: Welcome1.java • 2 // Text-printing program. • 3 • 4 public class Welcome1 { • 5 • 6 // main method begins execution of Java application • 7 public static void main( String args[] ) • 8 { • 9 System.out.println( "Welcome to Java Programming!" ); • 10 • 11 } // end method main • 12 • 13 } // end class Welcome1 Welcome to Java Programming!
A First Program in Java • Comments • // remainder of line is comment • Comments ignored • Document and describe code • Multiple line comments: /* ... */ 1 // Fig. 2.1: Welcome1.java /* This is a multiple line comment. It can be split over many lines */
4 public class Welcome1 { A First Program in Java • Class Declaration • Every Java program has at least one defined class • Keyword: words reserved for use by Java • class keyword followed by class name • The class name has to be an identifier • Naming Convention: capitalize every word • Example: SampleClassName
4 public class Welcome1 { 13}/* End of Class Welcome1 */ A First Program in Java • Body Delimiter • Left brace { • Begins body of every class • A corresponding right brace “}” ends definition (line 13) • Indentation Convention • Whenever you type an left brace “{“, immediately type the right brace “}”. • Then, indent to begin type the body.
5 public static void main( String args[] ) A First Program in Java • 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 • Methods can perform tasks and return result • void: means main returns no information • args[]:input arguments in String data type.
7 System.out.println("Welcome to Java Programming!" ); A First Program in Java • Statements • Statements are instructions to commend hardware to perform some operations. • It must end with semicolon “;” • System.out: standard output object • System.out.println: displays line of text
A First Program in Java • Execution Steps JAVA PROGRAM EXECUTION Java source code Java compiler byte-code .class Welcome.java javac Welcome.java byte-code interpreter JVM java Welcome EXECUTION
A First Program in Java • 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 tostart JVM and then run the program Welcome.class • Interpreter calls method main
A First Program in Java Demonstration • 4 public class Welcome1 { • 5 • 6 // main method begins execution of Java application • 7 public static void main( String args[] ) • 8 { • 9 System.out.println( "Welcome to Java Programming!" ); • 10 • 11 } // end method main • 12 • 13 } // end class Welcome1
7 System.out.println("Welcome to Java Programming!" ); Text Displaying • 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 to “); 8System.out.println(“Java Programming!" );
Text Displaying • 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.
7 System.out.println("Welcome\nto\nJava\n Programming!" ); 7 System.out.println(“\”in quotes\”" ); Text Displaying • Escape Sequences Welcome to Java Programming! “in quotes”
Text Displaying • 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 Format String 7 System.out.printf(“%s\n%s\n”, “Welcome to”, “Java Programming!" ); Welcome to Java Programming!
Value Input: Integer Addition • 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 • 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 number1=10; int number1; number1=10;
Value Input: Integer Addition Variable Declaration • public class Addition { • // main method begins execution of Java application • public static void main( String args[] ){ • int number1; • int number2; • int sum; • …… • …… • }/* End of main */ • }/* End of class Addition */
Value Input: Integer Addition • 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:"); • number1 = input.nextInt(); • // read the second integer • System.out.print("Enter second integer:"); • number2 = input.nextInt(); • …… • }/* End of main */ • }/* End of class Addition */
Value Input: Integer Addition • import java.util.Scanner; • public class Addition { • // main method begins execution of Java application • public static void main( String args[] ){ • …… • sum = number1 + number2; • System.out.printf("Sum is: %d\n", sum);}/* End of main */ • }/* End of class Addition */
Arithmetic • 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 • Operator precedence • Some arithmetic operators act before others
Equality and Relational Operators • 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 can be formed by using • Equality Operators • Relational Operators
Equality and Relational Operators Equality/Relational Operators
Equality and Relational Operators Example • import java.util.Scanner; • public class Comparison { • public static void main( String args[] ){ • int number1=100; • int number2=200; • if(number1 == number2){ • System.out.printf(“%d == %d \n”, number1, number2); • }/* End of if-condition */ • if(number1 !=number2){ • System.out.printf(“%d != %d \n”, number1, number2); • }/* End of if-condition */ • }/* End of main */ • }/* End of class Addition */
Thank You ! www.themegallery.com