Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Similar presentations


Presentation on theme: "Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University"— Presentation transcript:

1 Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University email: cheng@cse.ttu.edu.tw http:// www.cse.ttu.edu.tw/~fccheng

2 Contents: Introducing Programming with an Example Introducing Programming with an Example Identifiers Identifiers Variables Variables Constants Constants Primitive Data Types Primitive Data Types Operators Operators Expressions Expressions Programming Errors Programming Errors Style and Documentation Style and Documentation The MyInput class The MyInput class

3 Introducing Programming with an Example Example 2.1: Computing the Area of a Circle This program reads the radius from the keyboard and computes the area of the circle. ComputeAreaRun

4 Identifiers Identifiers: class name, method name, variables, key words. An identifier can be made up of letters, digits, the underscore character (_), and the dollar sign An identifier must start with a letter, an underscore, or a dollar sign. An identifier must start with a letter, an underscore, or a dollar sign. An identifier cannot contain operators, such as +, -, and be true, false, or null. An identifier cannot contain operators, such as +, -, and be true, false, or null. An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,”). An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,”). An identifier can be of any length. An identifier can be of any length. Java is case sensitive, (e.g. Total and total) Identifiers: class name, method name, variables, key words. An identifier can be made up of letters, digits, the underscore character (_), and the dollar sign An identifier must start with a letter, an underscore, or a dollar sign. An identifier must start with a letter, an underscore, or a dollar sign. An identifier cannot contain operators, such as +, -, and be true, false, or null. An identifier cannot contain operators, such as +, -, and be true, false, or null. An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,”). An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,”). An identifier can be of any length. An identifier can be of any length. Java is case sensitive, (e.g. Total and total)

5 Variables //Compute the first area radius = 1.0; area = radius*radius*3.14159; System.out.println("The area is "+area+" for radius "+radius); //Compute the second area radius = 2.0; area = radius*radius*3.14; System.out.println("The area is "+area+" for radius "+radius);

6 Declaring Variables int x; // declares x to be an // integer variable; // integer variable; double radius; // declares radius to // be a double variable; // be a double variable; char a; // declares a to be a // character variable; // character variable;

7 Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a;

8 Declaring and Initializing in One Step int x = 1; int x = 1; double d = 1.4; double d = 1.4; float f = 1.4; float f = 1.4;

9 ConstantsFormat: static final datatype CONSTANT = VALUE; Example: static final double PI = 3.14159; static final int SIZE = 3;

10 Numerical Data Types byte 8 bits short 16 bits (2 bytes) int 32 bits (4 bytes) long 64 bits (8 bytes) float 32 bits (4 bytes) double 64 bits (8 bytes)

11 Number Literals int i = 34; int i = 34; long l = 1000000; long l = 1000000; float f = 100.2f; or float f = 100.2F; float f = 100.2f; or float f = 100.2F; double d = 100.2d or double d = 100.2D; double d = 100.2d or double d = 100.2D;

12 Shortcut Operators OperatorExampleEquivalent +=i+=8i = i+8 -=f-=8.0f = f-8.0 *=i*=8i = i*8 /=i/=8i = i/8 %=i%=8i = i%8

13 Increment and Decrement Operators x = 1; x = 1; y = 1 + x++; y = 1 + x++; y = 1 + ++x; y = 1 + ++x; y = 1 + x--; y = 1 + x--; y = 1 + --x; y = 1 + --x;

14 Numeric Type Conversion Consider the following statements: byte i = 100; long l = i*3+4; double f = i*3.1+l/2;

15 Primitive Data Type double double float float long long Int Int char char short short byte byte boolean boolean

16 Type Casting float f = (float)10.1; float f = (float)10.1; int i = (int)f; int i = (int)f;

17 Character Data Type char letter = 'A'; char letter = 'A'; char letter = '\u000A'; char letter = '\u000A'; char numChar = '4'; char numChar = '4';

18 Unicode Format Character Escape SequenceASCIIUnicode Backspace\b\u0008 Tab\t\u0009 Linefeed\n\u000a Carriage return\r\u000d

19 The boolean Data Type boolean lightsOn = true; boolean lightsOn = true; boolean lightsOn = false; boolean lightsOn = false;

20 Operator Precedence Casting Casting ++, -- ++, -- *, /, % *, /, % +, - +, -, =>, => ==, !=; ==, !=; && && || || =, +=, -=, *=, /=, %= =, +=, -=, *=, /=, %=

21 Programming Errors Syntax Errors Syntax Errors  Syntax Error Runtime Errors Runtime Errors  devided by zero, exceptions Logical Errors Logical Errors  incorrect results, infinite loop

22 Naming Conventions Variables and method names: Variables and method names:  Use lowercase.  If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name.  For example, the variables radius and area, and the method computeArea.

23 Naming Conventions, cont. Class names: Class names:  Capitalize the first letter of each word in the name.  For example, the class name TestComputeArea. Constants: Constants:  Capitalize all letters in constants.  For example, the constant PI.

24 The MyInput Class MyInput ComputeMortgageRun Example 2.2: Computing Mortgage This program lets the user enter the interest rate, year, and loan amount and computes monthly payment and total payment.

25 The MyInput Class BreakChangesRun Example 2.3: Breaking Money Changes This program lets the user enter the amount in decimal user enter the amount in decimal representing dollars and cents and output a report listing the monetary equivalent in single dollars, quarters, dimes, nickels, and pennies.


Download ppt "Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University"

Similar presentations


Ads by Google