java building elements lecture 2 instructors: fu-chiung cheng ( 鄭福炯 ) associate professor...
TRANSCRIPT
Java Building ElementsLecture 2
Instructors: Fu-Chiung Cheng
(鄭福炯 )Associate Professor
Computer Science & EngineeringTatung University
email: [email protected]:// www.cse.ttu.edu.tw/~fccheng
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
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
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)
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);
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;
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;
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;
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;
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)
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;
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
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;
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;
Primitive Data Type
doubledouble
floatfloat
longlong
IntInt
charchar
shortshort
bytebyte
boolean boolean
Type Casting float f = (float)10.1;float f = (float)10.1;
int i = (int)f;int i = (int)f;
Character Data Type char letter = 'A'; char letter = 'A';
char letter = '\u000A';char letter = '\u000A';
char numChar = '4';char numChar = '4';
Unicode Format
Character Escape Sequence ASCII Unicode
Backspace \b \u0008
Tab \t \u0009
Linefeed \n \u000a
Carriage return \r \u000d
The boolean Data Type boolean lightsOn = true;boolean lightsOn = true;
boolean lightsOn = false;boolean lightsOn = false;
Operator Precedence CastingCasting ++, --++, -- *, /, %*, /, % +, -+, - <, <=, >, =><, <=, >, => ==, !=;==, !=; &&&& |||| =, +=, -=, *=, /=, %==, +=, -=, *=, /=, %=
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
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. .
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..
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.
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.