1 / 28

Identifiers - Naming

***** SWTJC STEM *****. Identifiers - Naming. Identifier , the name of a Java programming component. Identifier naming rules . . . Must start with letter, underscore (_) or dollar sign ($) Can be of any length Cannot contain “+” or “-”

Download Presentation

Identifiers - Naming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ***** SWTJC STEM ***** Identifiers - Naming • Identifier, the name of a Java programming component. • Identifier naming rules . . . • Must start with letter, underscore (_) or dollar sign ($) • Can be of any length • Cannot contain “+” or “-” • Cannot be a keyword used by Java (see Appendix A of text) • Should have meaning (“radius” preferred to “r”) • Identifier naming guidelines must be followed . . . • Package Names - A collection of classes • First word lower case • Additional words lower case, separated by “.” • Example geometry.areas Chapter 2-2 cg 21-22

  2. ***** SWTJC STEM ***** Identifiers - Naming cont. • Identifier naming guidelines cont. • Class Names - A collection of data and methods • First word upper case • Additional words upper case • Example ShellSort • Method Names - A collection of statements • Are verbs that represent actions • First word lower case • Additional words upper case • Example linearSearch Chapter 2-2 cg 22

  3. ***** SWTJC STEM ***** Identifiers - Naming cont. • Identifier naming guidelines cont. • Variables - Used to store data • Are nouns that represent items or things • First word lower case • Additional words upper case, • Example circleArea • Constants - Used to store data that does not change • All characters upper case • Additional words separated by underscore _ • Example PI, FLOW_FACTOR Chapter 2-2 cg 22

  4. ***** SWTJC STEM ***** Programs with Simple Algorithms • Our first programs will implement simple algorithms. • Example: Find the area of a circle. • Given radius = 2 and PI = 3.14156 • Find circleArea = PI * radius * radius • We will need to learn how Java handles . . . • Inputting data, variables, and constants • Calculations with expressions • Outputting to PC Monitor - System.out object Chapter 2-2 cg 23

  5. Data and Variables Input ***** SWTJC STEM ***** How to Input Something? Calculate Area of Circle A = PI * r2 Radius r Circle Area Chapter 2-2 cg 23

  6. ***** SWTJC STEM ***** Java handles data as primitive data types. Handling Data • Java provides for three primitive data types . . . • Numeric data - Numbers • Character data - Text • Boolean data - Logical states, true or false • Data typing provides increased . . . • Efficiency of memory use • Ease of programming • Speed of program execution Chapter 2-2 cg 23

  7. Keyword Description Size/Format (integers) byte Byte-length integer 8-bit [-128 to 127] short Short integer 16-bit [-32768 to 32767] int Integer 32-bit [approx.  2*109] long Long integer 64-bit [approx.  9*1018] (real numbers) float Single-precision floating point 32-bit [ approx. 3.4*1038] double Double-precision floating point 64-bit [ approx. 1.7*10308] (other types) char A single character 16-bit Unicode character boolean A Boolean value (true or false) true or false ***** SWTJC STEM ***** Primitive Data Types Chapter 2-2 cg 23

  8. ***** SWTJC STEM ***** Data Types Literals • A literal is a primitive data type appearing directly in a program. • 123.4, ‘b’,“Hello World”,false are literals. • The type of a literal is as follows: • The type of an integer literal that ends with L or l is long; the type of any other integer literal is int; e.g. 10. • The type of a floating-point literal that ends with F or f is float; the type of any other floating-point literal is double. (Floating-point literals contain a decimal point; e.g., 2.0) • The type of a character literal is char; e.g., ‘p’. • The type of a Boolean literal is Boolean; e.g., true. Chapter 2-2 cg 24

  9. Literal Data Type 178 int 8864L long 37.266 double 37.266f float 37.266F float 26.77e3 double ' c ' char true boolean false boolean ***** SWTJC STEM ***** Examples: Data Types Literals Examples Chapter 2-2 cg 24

  10. ***** SWTJC STEM ***** Unicode Characters • Character data holds a single character value like ‘H’ or ‘5’. • Character data is stored as unicode, a 16 bit encoding scheme. • Includes ASCII character set plus many other alphabets and symbols - See appendix B of text. • “\u0041” represents the letter “A” and is stored in memory as two hexadecimal bytes, “00” & “41”. • Examples: • \u0041 is equivalent to the character ‘A’. • \u0042 is equivalent to the character ‘B’. • \u0041 + 1 is equivalent to the character ‘B’. Chapter 2-2 cg 24

  11. Description Character Sequence Unicode Backspace \b \u0008 Tab \t \u0009 New line \n \u000A Carriage return \r \u000D Backslash \\ \u005C Single quote \’ \u0027 Double quote \” \u0022 ***** SWTJC STEM ***** Escape sequence is available for convenience. Escapes Sequences • Example: • ‘\’’ is the literal for a single quote. You cannot use ‘’’ ! • ‘\u0022’ will also work. Chapter 2-2 cg 24

  12. ***** SWTJC STEM ***** Boolean Data and Literals • Boolean data holds only one of two values, true or false. • Boolean representations are useful to compare two values. • Is iValue less than jValue? • The answer is either trueor false. • And, is representable as Boolean. • Boolean is also used to represent logical states. • Is the intrusion detector switch open? • It is either, true - open orfalse - closed. Chapter 2-2 cg 24

  13. ***** SWTJC STEM ***** Handling Variables • Variables are used to store data. • Java provides for three basic variable types . . . • Numeric variable - Holds a numeric value. • Character variable - Holds a single character value. • Boolean variable - Holds a logical value, true or false. • Creating a Java variable involves two steps . . . • Declaringthe name and data type of the variable • Assigning the variable a value • Note: Variables must be declared before used. Chapter 2-2 cg 25

  14. ***** SWTJC STEM ***** Creating a Variable - Declaring • Step 1 - To declare a variable . . . • Use the declaration statement . . .datatype varName1, varName2, . . ., varNamen; • The declaration statement consists of two parts . . . • The datatype (byte, int, float, char, etc.). • Name(s) for variable(s) separated by commas. • Meaningful noun • First word lower case • Additional words upper case, • Note that all Java statements end with a semicolon ;. Chapter 2-2 cg 25

  15. ***** SWTJC STEM ***** Declaring Variables - Examples • Examples of declaring numeric variables . . . • int count;where data type is integer and name is count. • float bulletSpeed, time, distance;where data type is floating point and multiple variables names arebulletSpeed, time, and distance. • char startingLetter;where data type is character and name is startingLetter • boolean doorAjar, trunkOpen; where data type is Boolean and names are doorAjar and trunkOpen. Chapter 2-2 cg 25

  16. ***** SWTJC STEM ***** Creating a Variable - Assigning • Step 2 - To assign a value to a variable, • Use the assignment statement. • variable = expression; • The assignment statement consists of three parts . . . • The variable whose value is to be assigned. • The assignment symbol, =. • An expression to be evaluated or a literal value to be assigned to the variable. Chapter 2-2 cg 26

  17. ***** SWTJC STEM ***** Creating a Variable - Examples • Examples of assigning values to variables . . . • time = 4.0;assigns the value 4.0 to numeric variabletime. • distance = time * 60.0 + 15.0;evaluates the expression to 255.0 and assigns the value to numeric variable distance. • firstLetter = ‘F’;assigns the character F to the character variable firstLetter. • gateOpen = true;assigns the logical condition true to the Boolean variable gateOpen. Chapter 2-2 cg 26

  18. ***** SWTJC STEM ***** Note About the “=“ Sign • Note: The = sign means assign a value, not equals. • Variable (on the left) is replaced by evaluated result of the expression (on the right). • Consider the code below.count = 4;count = count + 1; After executing, count is 5. • It may look like an equation, but it is not! Chapter 2-2 cg 26

  19. ***** SWTJC STEM ***** • To assign a value to a several variables, use . . . • variable1 = variable2 = . . . = variablen = expression; • Example: iCount = jCount = kCount = 4;is equivalent to . . .iCount = 4;jCount = 4;kCount = 4; Multiple Assignments Chapter 2-2 cg 26

  20. ***** SWTJC STEM ***** Combining Declare and Assign • To declare and assign a value to a variableat the same time, combine the declaration and assignments statements. • datatype variable = expression; • variable is . . . • Declared as datatype • Assigned the value of expression • Examples: • int count = 4;integercountis assigned value of 4. • double rate = 0.15, maxRate = 2.0 * rate;rate and maxRate are declared as double and assigned values 0.15 and 0.30 (calculated) respectively. Chapter 2-2 cg 26

  21. ***** SWTJC STEM ***** Declaring a Constant • To declare and assign a value to a constant, use . . .final datatype CONSTANT_NAME = VALUE; • final means exactly that. Once the constant is assigned a value, it cannot change! • Examples: • final double PI = 3.14159;assigns double constantPIthe value 3.14159. • final int I_COUNT_MAX = 1000;assigns constantI_COUNT_MAXthe value 1000. • Remember that constant names are all caps with multiple names separated by underscores! Chapter 2-2 cg 27

  22. Calculate Assignment Statement ***** SWTJC STEM ***** How to Calculate Something? Calculate Area of Circle A = PI * r2 Data and Variables Radius r Circle Area Chapter 2-2 cg 22-27

  23. Expressions Output System.out Object ***** SWTJC STEM ***** How to Output Something? Calculate Area of Circle A = PI * r2 Data & Variables Radius r Circle Area Chapter 2-2 cg 22-27

  24. ***** SWTJC STEM ***** print and println Methods • To output something with Java: • Invoke the out object of the System class (program). • Use println method to print and move to the next line • System.out.println(“Hello World”); prints HelloWorld. • By default out outputs to the PC monitor. • The println method is the action (or service) the object out performs for us. • Method print is similar to println but without next line. • The actual Java statement looks like this . . . • System.out.println(parameter); • Parameteris the piece of data we want to output. • Parameter can be a variable or literal. Chapter 2-2 cg 28

  25. ***** SWTJC STEM ***** print and println Examples • Examples: • System.out.println(“Hello World”); Outputs the string literal HelloWorld to the PC monitor. • Assume iCount is 10,System.out.println(“iCount = ” + iCount);Outputs the string literal iCount = 10 to the PC monitor.Note: The plus + concatenates(ties together) the character literal and the numeric variable value. • System.out.print(“Hello ”);System.out.println(“World”);Outputs identically to example 1. Chapter 2-2 cg 28

  26. Expressions System.out Object ***** SWTJC STEM ***** Putting It All Together Calculate Area of Circle A = PI * r2 Data & Variables Radius r Circle Area Chapter 2-2 cg 21-28

  27. ***** SWTJC STEM ***** public class CircleArea { public static void main(String[] args) { // Declare variables double radius, circleArea; final double PI = 3.14159; // Input data radius = 2.0; // Process data circleArea = PI * radius * radius; Example Procedural Program Chapter 2-2 cg 21-28

  28. ***** SWTJC STEM ***** Example Procedural Program // Output result System.out.println("Let’s begin."); System.out.print("A circle with radius = " + radius); System.out.println(" has an area = " + circleArea + "."); } } Chapter 2-2 cg 21-28

More Related