INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter2: Introduction to Java Applications 1.

Slides:



Advertisements
Similar presentations
Introduction to Java Applications
Advertisements

IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Chapter 1 Introduction to JAVA. Why Learn JAVA? Java is one of the fastest growing programming language in the world. Java is one of the fastest growing.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Introduction to C Programming
Chapter 2 Introduction to C Programming
Chapter 2 - Introduction to Java Applications
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CMT Programming Software Applications
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Chapter 2 - Introduction to Java Applications
Introduction to C Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Java Applications & Program Design
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Java™ How to Program, 10/e Late Objects Version © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Introduction to Java Applications
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 2 Introduction to Java Applications
Chapter 2 Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Fundamentals of Java Programs, Input/Output, Variables, and Arithmetic
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Java™ How to Program, 10/e Late Objects Version
MSIS 655 Advanced Business Applications Programming
Chapter 2 - Introduction to C Programming
Introduction to Java Applications
Chapter 2 - Introduction to C Programming
Java How to Program, 11/e Questions?
Computer Programming-1 CSC 111
Introduction to C Programming
Presentation transcript:

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

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

Introduction Java Keywords abstractdoimportpublicthrows booleandoubleinstanceofreturntransient breakelseintshorttry byteextendsinterfacestaticvoid casefinallongstrictfpvolatile catchfinallynativesuperwhile charfloatnewswitch classforpackagesynchronized continueifprivatethis defaultimplementsprotectedthrow 3

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 | '_'}. 4

Introduction Primitive Data Type Data TypePurposeContentsDefault Value* booleanTruth valuetrue or falsefales charCharacterUnicode characters\u0000 byteSigned integer8 bit two's complement(byte) 0 shortSigned integer16 bit two's complement(short) 0 intSigned integer32 bit two's complement0 longSigned integer64 bit two's complement0L floatReal number32 bit IEEE 754 floating point0.0f doubleReal number64 bit IEEE 754 floating point0.0d 5

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!" ); } // end method main } // end class Welcome1 Welcome to Java Programming! 6

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 */ 7

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 4public class Welcome1 { 8

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. 4public class Welcome1 { 13}/* End of Class Welcome1 */ 9

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. 5 public static void main( String args[] ) 10

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 7System.out.println("Welcome to Java Programming!" ); 11

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

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 to start JVM and then run the program Welcome.class  Interpreter calls method main 13

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!" ); } // end method main } // end class Welcome1 14

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 7System.out.println("Welcome to Java Programming!" ); 7System.out.print("Welcome to “); 8System.out.println(“Java Programming!" ); 15

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. Escape SequenceDescription \nNewline \tHorizontal Tab \rCarriage Return. Position the cursor at the beginning of the current line \\Backslash \”Double Quote 16

Text Displaying Escape Sequences 7System.out.println("Welcome\nto\nJava\n Programming!" ); Welcome to Java Programming! 7System.out.println(“\”in quotes\”" ); “in quotes” 17

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 18

Text Displaying Format String Type Character InputString Result %ccharcharacter %dsigned intsigned decimal integer %ffloatreal number, standard notation %sstring 7System.out.printf(“%s\n%s\n”, “Welcome to”, “Java Programming!" ); Welcome to Java Programming! 19

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 20

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; 21

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 */ 22

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 */ 23

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 */ 24

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 25

Arithmetic Operator precedence  Some arithmetic operators act before others 26

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 27

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

Equality and Relational Operators Example 29 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 */

30