Title: Java Building Elements Lecture 2
1Java Building ElementsLecture 2
- Instructors
- Fu-Chiung Cheng
- (???)
- Associate Professor
- Computer Science Engineering
- Tatung University
- email cheng_at_cse.ttu.edu.tw
- http// www.cse.ttu.edu.tw/cheng
2Contents
- Introducing Programming with an Example
- Identifiers
- Variables
- Constants
- Primitive Data Types
- Operators
- Expressions
- Programming Errors
- Style and Documentation
- The MyInput class
3Introducing 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.
ComputeArea
Run
4Identifiers
- 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 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 can be of any length.
- Java is case sensitive, (e.g. Total and total)
5Variables
- //Compute the first area
- radius 1.0
- area radiusradius3.14159
- System.out.println("The area is"area" for
radius "radius) - //Compute the second area
- radius 2.0
- area radiusradius3.14
- System.out.println("The area is "area" for
radius "radius)
6Declaring Variables
- int x // declares x to be an
- // integer variable
- double radius // declares radius to
- // be a double variable
- char a // declares a to be a
- // character variable
7Assignment Statements
- x 1 // Assign 1 to x
- radius 1.0 // Assign 1.0 to radius
- a 'A' // Assign 'A' to a
8Declaring and Initializingin One Step
- int x 1
- double d 1.4
- float f 1.4
9Constants
- Format
- static final datatype CONSTANT VALUE
- Example
- static final double PI 3.14159
- static final int SIZE 3
10Numerical 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)
11Number Literals
- int i 34
- long l 1000000
- float f 100.2f orfloat f 100.2F
- double d 100.2d ordouble d 100.2D
12Shortcut Operators
Operator Example Equivalent i8 i
i8 - f-8.0 f f-8.0 i8 i i8 / i/8 i
i/8 i8 i i8
13Increment andDecrement Operators
- x 1
- y 1 x
- y 1 x
- y 1 x--
- y 1 --x
14Numeric Type Conversion
- Consider the following statements
- byte i 100
- long l i34
- double f i3.1l/2
15Primitive Data Type
- double
- float
- long
- Int
- char
- short
- byte
- boolean
16Type Casting
- float f (float)10.1
- int i (int)f
17Character Data Type
- char letter 'A'
- char letter '\u000A'
- char numChar '4'
18Unicode Format
Character Escape Sequence ASCII Unicode Backspace
\b \u0008 Tab \t \u0009 Linefeed \n \u000a Carriag
e return \r \u000d
19The boolean Data Type
- boolean lightsOn true
- boolean lightsOn false
20Operator Precedence
- Casting
- , --
- , /,
- , -
- lt, lt, gt, gt
- , !
-
-
- , , -, , /,
21Programming Errors
- Syntax Errors
- Syntax Error
- Runtime Errors
- devided by zero, exceptions
- Logical Errors
- incorrect results, infinite loop
22Naming Conventions
- 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.
23Naming Conventions, cont.
- Class names
- Capitalize the first letter of each word in the
name. - For example, the class name TestComputeArea.
- Constants
- Capitalize all letters in constants.
- For example, the constant PI.
24Computing Mortgage
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.
MyInput
ComputeMortgage
Run
25Computing Changes
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.
BreakChanges
Run