【正文】
ation ? Applications begin executing at main ? Parentheses indicate main is a method (Ch. 3 and 6) ? Java applications contain one or more methods ? Exactly one method must be called main ? Methods can perform tasks and return information ?void means main returns no information ? For now, mimic main39。 9 ( Wele to Java Programming! )。 (“World”)。 ()。 ? Read Page 43 (“Wele to ”)。 The diffrence between print and println (“Wele to ”)。 (“Wele to ”)。 Wele to Java Programming Wele to Java Programming The difference between printf and println ? () ? keeps the cursor on the same line ? () ? Output at the beginning of the next line Task ? Write a program to output the following text with a single statement. Wele to Java Programming Our statement ?(“Wele to java Programming”)。 Escape Characters ? Reading Page 45 : (“Wele\n\to\nJava\nprogramming”)。 di sp l ay s i n q u o t e s Fig. | Some mon escape sequences. Displaying Text with printf ? ? New feature of J2SE ? Displays formatted data ? Format string ? Fixed text ? Format specifier – placeholder for a value ? Format specifier %s – placeholder for a string 9 ( %s\n%s\n, 10 Wele to, Java Programming! )。 ? ()。 Your job Output the following text ,with different print statements. Hello World! I am Coming! Now you are a programmer ? A Simple CAI program ? Enter two numbers ,we want to get the sum of these,then display the sum ? To solve this task ,we should ? Enter two number ? Get the sum of these number ? Output the sum New Things1 ? Input a number 1. Import 。 3. number1 = ()。 2. Using the variables ? Example 1. int number1。 3. number1=4。 ? assignment operator = ? assign result to variable sum ?Calculates sum of number1 and number2 (right hand side) You job ? Finish this program ? Page 68 ? Adding integers ? With this program,we will learn: ? Input statement ? Memory concept ? Variable Declaration ? Arithmetic operator Outline ? ? (1 of 2) ? import declaration ? Scanner ? nextInt 1 / / F i g . 2 . 7 : A d d i t i o n . j a v a 2 / / A d d i t i o n p r o g r a m t h a t d i s p l a y s t h e s u m o f t w o n u m b e r s . 3 i m p o r t j a v a . u t i l . S c a n n e r 。 12 13 int n u m b e r 1 。 / / s e c o n d n u m b e r t o a d d 15 int s u m 。 / / p r o m p t 18 n u m b e r 1 = i n p u t . n e x t I n t ( ) 。 / / p r o m p t 21 n u m b e r 2 = i n p u t . n e x t I n t ( ) 。 / / a d d n u m b e r s 24 25 S y s t e m . o u t . p r i n t f ( S u m i s % d \ n , s u m ) 。 import Scanner from java package: Question ? is the function of the import’s statement? ? we locate this statement? ? 3. For Line 11,What is and what is a Scanner? ? 4. How to create a Scanner object? ? Line1315, why declare the same type of variables in different lines? Question ? to identify a variable name? ? package are the class System in and why not import this class? ? to obtain a number from keyboard? ? is the function of statement? Another Java Application: Adding Integers (Cont.) ? import declarations ? Used by piler to identify and locate classes used in Java programs ? Tells piler to load class Scanner from package ? Begins public class Addition ? Recall that file name must be 3 import 。 ? Initialize variable in its declaration ? Equal sign ? Standard input object ? 10 // create Scanner to obtain input from mand window 11 Scanner input = new Scanner( )。 // first number to add 14 int number2。 // second number to add int number1, // first number to add number2, // second number to add sum。 // prompt 18 number1 = ()。 // prompt 21 number2 = ()。 // add numbers Another Java Application: Adding Integers (Cont.) ? Use to display results ? Format specifier %d ?Placeholder for an int value ? Calculations can also be performed inside printf ? Parentheses around the expression number1 + number2 are not required 25 ( Sum is %d\n: , sum )。 What we learn from this program? ? How should we input by keyboard? Import 。 Create an object of scanner to accept the input from user What we learn from this program? ? How should we input by keyboard? Import 。 int number = ()。 y x / y Arithmetic (Cont.) ? Operator precedence ? Some arithmetic operators act before others (., multiplication before addition) ?Use parenthesis when needed ? Example: Find the average of three variables a, b and c ?Do not use: a + b + c / 3 ?Use: ( a + b + c ) / 3 Fig. | Precedence of arithmetic operators. O pe ra to r (s ) O pe ra tion( s ) O rder of e v a lu a t io n (p rece de nc e) * / % Mu l t i p l i ca t i o n D i v i s i o n Rem ai n d er E v al u a t e d fir s t . If t h e re are s ev era l o p era t o rs o f t h i s ty p e, t h ey are ev a l u at ed fr o m left t o ri g h t . + A d d i t i o n Su b t ra ct i o n E v al u a t e d ne x t . If t h e re are s ev era l o p era t o rs o f t h i s ty p e, t h ey are ev a l u at ed fr o m left t o ri g h t . Fig. | Order in which a seconddegree polynomial is evaluated. You are a programmer ? Enter two numbers, then pare these numbers, and give the result of parison. ? Example1 ? two number : 300 23 ? Output 30023 300!=23 300=23 ? Example2 ? two number : 300 300 ? Output 300==300 300=23