Java Building Elements Lecture 2 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Java Building Elements Lecture 2

Description:

Computer Science & Engineering. Tatung University. email: cheng_at_cse.ttu.edu.tw ... Numerical Data Types. byte 8 bits. short 16 bits (2 bytes) int 32 bits (4 bytes) ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 26
Provided by: yda1
Category:

less

Transcript and Presenter's Notes

Title: Java Building Elements Lecture 2


1
Java 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

2
Contents
  • Introducing Programming with an Example
  • Identifiers
  • Variables
  • Constants
  • Primitive Data Types
  • Operators
  • Expressions
  • Programming Errors
  • Style and Documentation
  • 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.

ComputeArea
Run
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 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)

5
Variables
  • //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)

6
Declaring 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

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 Initializingin One Step
  • int x 1
  • double d 1.4
  • float f 1.4

9
Constants
  • Format
  • 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
  • long l 1000000
  • float f 100.2f orfloat f 100.2F
  • double d 100.2d ordouble d 100.2D

12
Shortcut 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
13
Increment andDecrement Operators
  • x 1
  • 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 i34
  • double f i3.1l/2

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

16
Type Casting
  • float f (float)10.1
  • int i (int)f

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

18
Unicode Format
Character Escape Sequence ASCII Unicode Backspace
\b \u0008 Tab \t \u0009 Linefeed \n \u000a Carriag
e return \r \u000d
19
The boolean Data Type
  • boolean lightsOn true
  • boolean lightsOn false

20
Operator Precedence
  • Casting
  • , --
  • , /,
  • , -
  • lt, lt, gt, gt
  • , !
  • , , -, , /,

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

22
Naming 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.

23
Naming 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.

24
Computing 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
25
Computing 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
Write a Comment
User Comments (0)
About PowerShow.com