Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.

Similar presentations


Presentation on theme: "Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main."— Presentation transcript:

1

2 Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main 6 } // end class Hello Hello! A Simple Program: Printing a Line of Text Chapter 0 – Introduction to Programming

3 2 Data Types

4 3 Identifiers Identifiers are names for programming entities such as variables, classes, methods, etc. Rules for naming identifiers: 1.consists of letters (a-z, A-Z), digits (0-9), underscore (_)and dollar sign ($) 2.cannot start with a digit 3.cannot be reserved words (see next page) Example of Valid Identifiers: –a, aNumber, F101, Sales_2003$123, MyClass Example of Invalid Identifiers: –Go#, 3Hands, Star*Mars, final

5 4 Reserved Words abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized true false null

6 5 Constants A constant is an identifier that is similar to a variable except that its value cannot be changed. The compiler will issue an error if you try to change a constant In Java, we use the final modifier to declare a constant final int MEANING_OF_LIFE = 42; Constants: –give names to otherwise unclear literal values –facilitate changes to the code Avoid using literal constants (e.g. salary = 2*hourly_rate + 60; ) should use salary = OT_FACTOR*hourly_rate + BASIC_RATE;

7 6 Assignment An assignment statement changes the value of a variable The assignment operator is the = sign You can only assign a value to a variable that is consistent with the variable's declared type The expression on the right is evaluated and the result is stored in the variable on the left The value that was in total is overwritten total = 55*salary;

8 7 Arithmetic Operators

9 8

10 9

11 10 String Concatenation The plus operator (+) is used for arithmetic addition and string concatenation (joining two strings) The function to perform depends on the type of the information e.g. 2 + 35 gives 37 but "2" + "35" gives "235" How about 2 + "35" or "2" + 35? Ans: "235" If both operands are strings, or if one is a string and one is a number, it performs string concatenation If both operands are numeric, it adds them

12 11 Casting Casting - convert a type to another explicitly To cast, the type is put in parentheses in front of the value being converted For example, if total and count are integers, but we want a floating point result when dividing them, we can cast total : result = (float) total / count; To avoid loss of accuracy, use a = (double) 10/3; a = 10/(double) 3; but NOT a = (double) (10/3); Note: a=10/3.0 or a=10.0/3 also work

13 12 Equality and Relational Operators e.g. a=2; b=4; if (a>b) System.out.print("hot - "); System.out.prinln("but not humid"); The output is but not humid.

14 13 Logical Operators Logical operators –Allows for forming more complex conditions –Combines simple conditions Java logical operators –&& (logical AND) –& (boolean logical AND) –|| (logical OR) –| (boolean logical inclusive OR) –^ (boolean logical exclusive OR) –! (logical NOT)

15 14 The if/else Selection Structure Perform action ( statement 1 ) only when condition is true Perform different specified action ( statement 2 ) when condition is false if ( condition ) statement 1 else statement 2

16 15 Conditional Operator The conditional operator is similar to an if-else statement, except that it is an expression that returns a value. For example: if (num1 > num2) larger = num1; else larger = num2; is the same as larger = (num1 > num2) ? num1 : num2;

17 16 Nested if statements expression 1 statement 3 expression 2 statement 2 Statement 1 if (expression 1) if (expression 2) statement 1 else statement 2 else statement 3 true false

18 17 The switch Multiple-Selection Structure switch structure –Used for multiple selections Syntax expression must be byte, short, int or char but NOT long switch ( expression ) { case label 1 : statement(s); break; case label 2 : statement(s); break; default: statement(s); }

19 18 The for Repetition Structure for ( expression1; expression2; expression3 ) { statement; } can easily be rewritten as: expression1; while ( expression2 ) { statement; expression3; }

20 19 The while Repetition Structure The structure is: while ( condition ) { statement 1 ; statement 2 ; statement 3 ;... } Loop Body

21 20 The do/while Repetition Structure do/while structure –Similar to while structure –Tests loop-continuation after performing body of loop i.e., loop body always executes at least once Syntax do { statements;... } while ( condition );

22 21 Nested Loop A loop inside the loop body of another loop. Example: Print the following pattern using nested for-loop. public class NestedLoop1 { public static void main( String args[] ) { for ( int i = 1; i <= 7; i++ ) { // print each row for ( int j = 1; j <= 5; j++ ) { System.out.print( j ); } System.out.println(); } Outer Loop Inner Loop 12345

23 22 Arrays Consider the declaration int[] c = new int[ 5 ]; -c is the array name –c.length accesses array c ’s length (5) –c has 5 elements ( c[0], c[1], … c[4] ) –c[1] = 20 assigns integer 20 to the second location of c An array of size N is indexed from zero to N-1 We can use initializer list to initialize array elements as int[] n = { 10, 20, 30, 40, 50 }; The operator new is not needed.

24 23 Arrays Declaring and Allocating arrays –Arrays are objects that occupy memory –Allocated dynamically with operator new int[] c = new int[ 12 ]; –Equivalent to int[] c; // declare array c = new int[ 12 ]; // allocate array We can allocate arrays of objects too String[] b = new String[ 100 ]; Bear in mind –All primitive data type variables are allocated automatically. –All object data type variables (including array) must be created by the programmer (using the new operator) explicitly.

25 24 Arrays Multiple-subscripted arrays –Tables with rows and columns Double-subscripted (two-dimensional) array Declaring double-subscripted array b[2][2] int b[][] = { { 1, 2 }, { 3, 4 } }; –1 and 2 initialize b[0][0] and b[0][1] –3 and 4 initialize b[1][0] and b[1][1] 12 34 01 0 1

26 25 Arrays Allocating multiple-subscripted arrays –Can be allocated dynamically 3 -by- 4 array int b[][]; b = new int[ 3 ][ 4 ]; Rows can have different number of columns int c[][]; c = new int[ 2 ][ ]; // allocate rows c[ 0 ] = new int[ 5 ]; // allocate row 0 c[ 1 ] = new int[ 3 ]; // allocate row 1 b c

27 26 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row 0 Row 2 Row 1 Column 0Column 1Column 2Column 3 Column subscript (or index) Row Subscript (or index) Array name Fig. 7.14 A double-subscripted array with three rows and four columns. Arrays

28 27 Method Definitions General format of a method: return-value-type method-name( parameter-list ) { declarations and statements } Method can also return values: return expression ; Parameters must be correct –When a method starts running, it must have the right number of parameters, and each parameter must be of the required type. public int square( int y ) { return y * y; } Access Modifier (discussed later)

29 28 – Specify how to convert types without data loss Promotion Rules

30 29 Scope Rules A method can see local variables and parameters that are within it’s scope (inside the box). A method can see out of the glass that surrounds it. But no outsider can see into the box.

31 30 Method Overloading Method overloading –Several methods of the same name –Different parameter set for each method Number of parameters Parameter types –Return type cannot be used to distinguish overloaded methods. Examples from Java Library –The class Arrays has many overloaded methods


Download ppt "Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main."

Similar presentations


Ads by Google