java building elements lecture 2 instructors: fu-chiung cheng ( 鄭福炯 ) associate professor...

25
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭鄭鄭 ) Associate Professor Computer Science & Engineering Tatung University email: [email protected] http:// www.cse.ttu.edu.tw/~fccheng

Upload: avice-mcdaniel

Post on 27-Dec-2015

240 views

Category:

Documents


0 download

TRANSCRIPT

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

Java Building ElementsLecture 2

Instructors: Fu-Chiung Cheng

(鄭福炯 )Associate Professor

Computer Science & EngineeringTatung University

email: [email protected]:// www.cse.ttu.edu.tw/~fccheng

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

Contents:

Introducing Programming with an ExampleIntroducing Programming with an Example IdentifiersIdentifiers VariablesVariables ConstantsConstants Primitive Data Types Primitive Data Types OperatorsOperators ExpressionsExpressions Programming ErrorsProgramming Errors Style and DocumentationStyle and Documentation The MyInput class The MyInput class

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

Introducing Programming with an Example

Example 2.1: Computing the Area of Example 2.1: Computing the Area of a Circlea Circle

This program reads the radius from This program reads the radius from the keyboard and computes the the keyboard and computes the area of the circle.area of the circle.

ComputeAreaComputeArea RunRun

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

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 An identifier must start with a letter, an underscore, or a dollar

sign.sign. An identifier cannot contain operators, such as +, -, and be An identifier cannot contain operators, such as +, -, and be

truetrue, , falsefalse, or null., or null. An identifier cannot be a reserved word. (See Appendix A, An identifier cannot be a reserved word. (See Appendix A,

“Java Keywords,”). “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 An identifier must start with a letter, an underscore, or a dollar sign.sign.

An identifier cannot contain operators, such as +, -, and be An identifier cannot contain operators, such as +, -, and be truetrue, , falsefalse, or null., or null.

An identifier cannot be a reserved word. (See Appendix A, An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,”). “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)

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

Variables

////Compute the first areaCompute the first arearadius = 1.0;radius = 1.0;area = radius*radius*3.14159;area = radius*radius*3.14159;System.out.println("The area isSystem.out.println("The area is

"+area+" for radius "+radius);"+area+" for radius "+radius);//Compute the second area//Compute the second arearadius = 2.0;radius = 2.0;area = radius*radius*3.14;area = radius*radius*3.14;System.out.println("The area is System.out.println("The area is

"+area+" for radius "+radius);"+area+" for radius "+radius);

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

Declaring Variables

int x; // declares x to be anint x; // declares x to be an // integer variable;// integer variable;

double radius; // declares radius todouble radius; // declares radius to // be a double variable;// be a double variable;

char a; // declares a to be achar a; // declares a to be a // character variable;// character variable;

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

Assignment Statements

x = 1; // Assign 1 to x;x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 to radius;radius = 1.0; // Assign 1.0 to radius;

a = 'A'; // Assign 'A' to a;a = 'A'; // Assign 'A' to a;

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

Declaring and Initializingin One Step

int x = 1;int x = 1;

double d = 1.4;double d = 1.4;

float f = 1.4;float f = 1.4;

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

Constants

Format:Format:

static final datatype CONSTANT = VALUE; static final datatype CONSTANT = VALUE;

Example:Example:

static final double PI = 3.14159; static final double PI = 3.14159;

static final int SIZE = 3;static final int SIZE = 3;

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

Numerical Data Types

bytebyte 8 bits8 bits

shortshort 16 bits (2 bytes)16 bits (2 bytes)

intint 32 bits (4 bytes)32 bits (4 bytes)

longlong 64 bits (8 bytes)64 bits (8 bytes)

floatfloat 32 bits (4 bytes)32 bits (4 bytes)

doubledouble 64 bits (8 bytes)64 bits (8 bytes)

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

Number Literals int i = 34;int i = 34;

long l = 1000000;long l = 1000000;

float f = 100.2f;float f = 100.2f; or orfloat f = 100.2F;float f = 100.2F;

double d = 100.2ddouble d = 100.2d or ordouble d = 100.2D;double d = 100.2D;

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

Shortcut Operators

Operator Example Equivalent

+= i+=8 i = i+8

-= f-=8.0 f = f-8.0

*= i*=8 i = i*8

/= i/=8 i = i/8

%= i%=8 i = i%8

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

Increment andDecrement 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;

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

Numeric Type Conversion

Consider the following statements:Consider the following statements:

byte i = 100;byte i = 100;

long l = i*3+4;long l = i*3+4;

double f = i*3.1+l/2;double f = i*3.1+l/2;

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

Primitive Data Type

doubledouble

floatfloat

longlong

IntInt

charchar

shortshort

bytebyte

boolean boolean

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

Type Casting float f = (float)10.1;float f = (float)10.1;

int i = (int)f;int i = (int)f;

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

Character Data Type char letter = 'A'; char letter = 'A';

char letter = '\u000A';char letter = '\u000A';

char numChar = '4';char numChar = '4';

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

Unicode Format

Character Escape Sequence ASCII Unicode

Backspace \b \u0008

Tab \t \u0009

Linefeed \n \u000a

Carriage return \r \u000d

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

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

boolean lightsOn = false;boolean lightsOn = false;

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

Operator Precedence CastingCasting ++, --++, -- *, /, %*, /, % +, -+, - <, <=, >, =><, <=, >, => ==, !=;==, !=; &&&& |||| =, +=, -=, *=, /=, %==, +=, -=, *=, /=, %=

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

Programming Errors

Syntax ErrorsSyntax Errors Syntax ErrorSyntax Error

Runtime ErrorsRuntime Errors devided by zero, exceptionsdevided by zero, exceptions

Logical ErrorsLogical Errors incorrect results, infinite loopincorrect results, infinite loop

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

Naming Conventions Variables and method names: Variables and method names:

Use lowercase. Use lowercase. If the name consists of several If the name consists of several

words, concatenate all in one, use words, concatenate all in one, use lowercase for the first word, and lowercase for the first word, and capitalize the first letter of each capitalize the first letter of each subsequent word in the name. subsequent word in the name.

For example, the variables For example, the variables radiusradius and and areaarea, and the method , and the method computeAreacomputeArea. .

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

Naming Conventions, cont.

Class names: Class names: Capitalize the first letter of each Capitalize the first letter of each

word in the name. word in the name. For example, the class name For example, the class name

TestComputeAreaTestComputeArea..

Constants: Constants: Capitalize all letters in constants.Capitalize all letters in constants. For example, the constant For example, the constant PIPI..

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

The MyInput Class

MyInputMyInput ComputeMortgageComputeMortgage RunRun

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.

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

The MyInput Class

BreakChangesBreakChanges RunRun

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.