Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor...
-
Upload
avice-mcdaniel -
Category
Documents
-
view
244 -
download
0
Transcript of Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor...
![Page 1: Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University email: cheng@cse.ttu.edu.tw.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/1.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/2.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/3.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/4.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/5.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/6.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/7.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/8.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/9.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/10.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/11.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/12.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/13.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/14.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/15.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/16.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/17.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/18.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/19.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/20.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/21.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/22.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/23.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/24.jpg)
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.](https://reader033.fdocuments.net/reader033/viewer/2022061603/56649e2f5503460f94b1fe70/html5/thumbnails/25.jpg)
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.