Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
***** 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 ***** 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 ***** 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 ***** 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 ***** SWTJC STEM ***** Data and Input Variables Radius r How to Input Something? Calculate Area of Circle Circle Area A = PI * r2 Chapter 2-2 cg 23 ***** SWTJC STEM ***** Handling Data Java handles data as primitive data types. • Java provides for three primitive data types . . . 1. Numeric data - Numbers 2. Character data - Text 3. 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 ***** SWTJC STEM ***** Keyword Primitive Data Types 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] float double char boolean (real numbers) Single-precision 32-bit [ approx. 3.4*1038] floating point Double-precision 64-bit [ approx. 1.7*10308] floating point (other types) A single character A Boolean value (true or false) 16-bit Unicode character true or false Chapter 2-2 cg 23 ***** 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 ***** SWTJC STEM ***** Data Types Literals Examples Examples: Literal Data Type 178 int 8864L 37.266 37.266f 37.266F 26.77e3 long double float float double 'c' char true false boolean boolean Chapter 2-2 cg 24 ***** 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 ***** SWTJC STEM ***** Escapes Sequences Escape sequence is available for convenience. 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 Example: • ‘\’’ is the literal for a single quote. You cannot use ‘’’ ! • ‘\u0022’ will also work. Chapter 2-2 cg 24 ***** 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 true or 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 or false - closed. Chapter 2-2 cg 24 ***** SWTJC STEM ***** Handling Variables • Variables are used to store data. • Java provides for three basic variable types . . . 1. Numeric variable - Holds a numeric value. 2. Character variable - Holds a single character value. 3. Boolean variable - Holds a logical value, true or false. • Creating a Java variable involves two steps . . . 1. Declaring the name and data type of the variable 2. Assigning the variable a value • Note: Variables must be declared before used. Chapter 2-2 cg 25 ***** 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 ***** 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 are bulletSpeed, 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 ***** 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 ***** SWTJC STEM ***** • Creating a Variable - Examples Examples of assigning values to variables . . . • time = 4.0; assigns the value 4.0 to numeric variable time. • 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 ***** 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 ***** SWTJC STEM ***** Multiple Assignments 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; Chapter 2-2 cg 26 ***** SWTJC STEM ***** Combining Declare and Assign To declare and assign a value to a variable at 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; integer count is 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 ***** 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 constant PI the value 3.14159. • final int I_COUNT_MAX = 1000; assigns constant I_COUNT_MAX the value 1000. Remember that constant names are all caps with multiple names separated by underscores! Chapter 2-2 cg 27 ***** SWTJC STEM ***** How to Calculate Something? Assignment Calculate Statement Data and Variables Radius r Calculate Area of Circle Circle Area A = PI * r2 Chapter 2-2 cg 22-27 ***** SWTJC STEM ***** How to Output Something? Expressions Data & Variables Radius r Calculate Area System.out OutputObject of Circle Circle Area A = PI * r2 Chapter 2-2 cg 22-27 ***** SWTJC STEM ***** • • • • • print and println Methods To output something with Java: 1. Invoke the out object of the System class (program). 2. Use println method to print and move to the next line 3. System.out.println(“Hello World”); prints Hello World. 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); • Parameter is the piece of data we want to output. • Parameter can be a variable or literal. Chapter 2-2 cg 28 ***** SWTJC STEM ***** • print and println Examples Examples: 1. System.out.println(“Hello World”); Outputs the string literal Hello World to the PC monitor. 2. 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. 3. System.out.print(“Hello ”); System.out.println(“World”); Outputs identically to example 1. Chapter 2-2 cg 28 ***** SWTJC STEM ***** Putting It All Together Expressions Data & Variables Radius r Calculate Area System.out Object of Circle Circle Area A = PI * r2 Chapter 2-2 cg 21-28 ***** SWTJC STEM ***** Example Procedural Program 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; Chapter 2-2 cg 21-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