Download presentation
Presentation is loading. Please wait.
Published byDominic Lewis Modified over 9 years ago
1
***** SWTJC STEM ***** Chapter 2-2 cg 21-22 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
2
***** SWTJC STEM ***** Chapter 2-2 cg 22 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
3
***** SWTJC STEM ***** Chapter 2-2 cg 22 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
4
***** SWTJC STEM ***** Chapter 2-2 cg 23 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
5
***** SWTJC STEM ***** Chapter 2-2 cg 23 How to Input Something? Calculate Area of Circle A = PI * r 2 Radius rCircle Area Input Data and Variables
6
***** SWTJC STEM ***** Chapter 2-2 cg 23 Handling Data 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 Java handles data as primitive data types.
7
***** SWTJC STEM ***** Chapter 2-2 cg 23 Primitive Data Types KeywordDescriptionSize/Format (integers) byteByte-length integer8-bit [-128 to 127] shortShort integer16-bit [-32768 to 32767] intInteger 32-bit [approx. 2*10 9 ] longLong integer 64-bit [approx. 9*10 18 ] (real numbers) float Single-precision floating point 32-bit [ approx. 3.4*10 38 ] double Double-precision floating point 64-bit [ approx. 1.7*10 308 ] (other types) charA single character16-bit Unicode character boolean A Boolean value (true or false) true or false
8
***** SWTJC STEM ***** Chapter 2-2 cg 24 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.
9
***** SWTJC STEM ***** Chapter 2-2 cg 24 Data Types Literals Examples LiteralData Type 178int 8864Llong 37.266double 37.266ffloat 37.266Ffloat 26.77e3double ' c 'char trueboolean falseboolean Examples:
10
***** SWTJC STEM ***** Chapter 2-2 cg 24 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’.
11
***** SWTJC STEM ***** Chapter 2-2 cg 24 Escapes Sequences \u0022\”Double quote \u0027\’Single quote \u005C\\Backslash \u000D\rCarriage return \u000A\nNew line \u0009\tTab \u0008\bBackspace UnicodeCharacter SequenceDescription Escape sequence is available for convenience. Example: ‘\’’ is the literal for a single quote. You cannot use ‘’’ ! ‘\u0022’ will also work.
12
***** SWTJC STEM ***** Chapter 2-2 cg 24 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.
13
***** SWTJC STEM ***** Chapter 2-2 cg 25 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.
14
***** SWTJC STEM ***** Chapter 2-2 cg 25 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 ;.
15
***** SWTJC STEM ***** Chapter 2-2 cg 25 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.
16
***** SWTJC STEM ***** Chapter 2-2 cg 26 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.
17
***** SWTJC STEM ***** Chapter 2-2 cg 26 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.
18
***** SWTJC STEM ***** Chapter 2-2 cg 26 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!
19
***** SWTJC STEM ***** Chapter 2-2 cg 26 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;
20
***** SWTJC STEM ***** Chapter 2-2 cg 26 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.
21
***** SWTJC STEM ***** Chapter 2-2 cg 27 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!
22
***** SWTJC STEM ***** Chapter 2-2 cg 22-27 How to Calculate Something? Calculate Area of Circle A = PI * r 2 Radius rCircle Area Data and Variables Calculate Assignment Statement
23
***** SWTJC STEM ***** Chapter 2-2 cg 22-27 How to Output Something? Calculate Area of Circle A = PI * r 2 Radius rCircle Area Data & Variables Expressions Output System.out Object
24
***** SWTJC STEM ***** Chapter 2-2 cg 28 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.
25
***** SWTJC STEM ***** Chapter 2-2 cg 28 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.
26
***** SWTJC STEM ***** Chapter 2-2 cg 21-28 Putting It All Together Calculate Area of Circle A = PI * r 2 Radius rCircle Area Data & Variables Expressions System.out Object
27
***** SWTJC STEM ***** Chapter 2-2 cg 21-28 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;
28
***** SWTJC STEM ***** Chapter 2-2 cg 21-28 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 + "."); }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.