What is a Computer? Chapter 1-9, 12-13, 18, 20, 23 Review Slides A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer, and communication devices. CS1: Java Programming Bus Colorado State University Communication Input Output Storage Memory CPU Devices Devices Devices Devices e.g., Monitor, e.g., Disk, CD, e.g., Modem, e.g., Keyboard, Original slides by Daniel Liang Printer and Tape and NIC Mouse Modified slides by Chris Wilcox Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 1 2 rights reserved. rights reserved. Companion Characteristics of Java Website Declaring Variables ✦ Java Is Simple ✦ Java Is Object-Oriented int x; // Declare x to be an ✦ Java Is Distributed // integer variable; ✦ Java Is Interpreted double radius; // Declare radius to ✦ Java Is Robust // be a double variable; ✦ Java Is Secure char a; // Declare a to be a ✦ Java Is Architecture-Neutral // character variable; ✦ Java Is Portable ✦ Java's Performance ✦ Java Is Multithreaded ✦ Java Is Dynamic www.cs.armstrong.edu/liang/JavaCharacteristics.pdf Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 3 4 rights reserved. rights reserved.
Identifiers Assignment Statements ✦ An identifier is a sequence of characters that consist of x = 1; // Assign 1 to x; letters, digits, underscores (_), and dollar signs ($). radius = 1.0; // Assign 1.0 to radius; ✦ An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. a = 'A'; // Assign 'A' to a; ✦ An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words). ✦ An identifier cannot be true , false , or null . ✦ An identifier can be of any length. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 5 6 rights reserved. rights reserved. Numeric Operators Numerical Data Types Name Range Storage Size Name Meaning Example Result –27 to 27 – 1 (-128 to 127) byte 8-bit signed –215 to 215 – 1 (-32768 to 32767) short 16-bit signed + Addition 34 + 1 35 –231 to 231 – 1 (-2147483648 to 2147483647) int 32-bit signed –263 to 263 – 1 - Subtraction 34.0 – 0.1 33.9 long 64-bit signed (i.e., -9223372036854775808 to 9223372036854775807) * Multiplication 300 * 30 9000 float Negative range: 32-bit IEEE 754 -3.4028235E+38 to -1.4E-45 Positive range: / Division 1.0 / 2.0 0.5 1.4E-45 to 3.4028235E+38 double Negative range: 64-bit IEEE 754 % Remainder 20 % 3 2 -1.7976931348623157E+308 to -4.9E-324 Positive range: 4.9E-324 to 1.7976931348623157E+308 Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 7 8 rights reserved. rights reserved.
How to Evaluate an Expression Integer Division Though Java has its own way to evaluate an +, -, *, /, and % expression behind the scene, the result of a Java expression and its corresponding arithmetic expression are the same. Therefore, you can safely 5 / 2 yields an integer 2. apply the arithmetic rule for evaluating a Java 5.0 / 2 yields a double value 2.5 expression. 3 + 4 * 4 + 5 * (4 + 3) - 1 (1) inside parentheses first 3 + 4 * 4 + 5 * 7 – 1 (2) multiplication 3 + 16 + 5 * 7 – 1 (3) multiplication 5 % 2 yields 1 (the remainder of the division) 3 + 16 + 35 – 1 (4) addition 19 + 35 – 1 (5) addition 54 - 1 (6) subtraction 53 Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 9 10 rights reserved. rights reserved. Type Casting Conversion Rules Implicit casting When performing a binary operation involving two double d = 3; (type widening) operands of different types, Java automatically converts the operand based on the following rules: Explicit casting int i = (int)3.0; (type narrowing) 1. If one of the operands is double, the other is int i = (int)3.9; (Fraction part is truncated) converted into double. 2. Otherwise, if one of the operands is float, the other is What is wrong? int x = 5 / 2.0; converted into float. 3. Otherwise, if one of the operands is long, the other is range increases converted into long. byte, short, int, long, float, double 4. Otherwise, both operands are converted into int. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 11 12 rights reserved. rights reserved.
The boolean Type and Operators Relational Operators Often in a program you need to compare two Java Mathematics Name Example Result values, such as whether i is greater than j. Java Operator Symbol (radius is 5) provides six comparison operators (also known < < less than radius < 0 false <= ≤ less than or equal to radius <= 0 false as relational operators) that can be used to > > greater than radius > 0 true compare two values. The result of the >= ≥ greater than or equal to radius >= 0 true comparison is a Boolean value: true or false. == = equal to radius == 0 false != ≠ not equal to radius != 0 true boolean b = (1 > 2); Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 13 14 rights reserved. rights reserved. Multiple Alternative if Statements Multi-Way if-else Statements if (score >= 90.0 ) if (score >= 90.0 ) System.out.print( "A" ); System.out.print( "A" ); else else if (score >= 80.0 ) if (score >= 80.0 ) System.out.print( "B" ); Equivalent System.out.print( "B" ); else if (score >= 70.0 ) System.out.print( "C" ); else else if (score >= 60.0 ) if (score >= 70.0 ) System.out.print( "D" ); System.out.print( "C" ); else else System.out.print( "F" ); if (score >= 60.0 ) System.out.print( "D" ); This is better else System.out.print( "F" ); (a) (b) Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 15 16 rights reserved. rights reserved.
switch Statements Logical Operators switch (status) { case 0: compute taxes for single filers; Operator Name Description break; case 1: compute taxes for married file jointly; ! not logical negation break; case 2: compute taxes for married file separately; && and logical conjunction break; case 3: compute taxes for head of household; break; || or logical disjunction default: System.out.println("Errors: invalid status"); System.exit(1); ^ exclusive or logical exclusion } Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 17 18 rights reserved. rights reserved. switch Statement Flow Chart Operator Precedence ✦ () ✦ var++, var-- ✦ +, - (Unary plus and minus), ++var , --var ✦ (type) Casting ✦ ! (Not) ✦ * , / , % (Multiplication, division, and remainder) ✦ + , - (Binary addition and subtraction) ✦ < , <= , > , >= (Relational operators) ✦ == , !=; (Equality) ✦ ^ (Exclusive OR) ✦ && (Conditional AND) Short-circuit AND ✦ || (Conditional OR) Short-circuit OR ✦ = , += , -= , *= , /= , %= (Assignment operator) Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 19 20 rights reserved. rights reserved.
ASCII Code for Commonly Used The Math Class Characters ✦ Class constants: Characters Code Value in Decimal Unicode Value – PI – E '0' to '9' 48 to 57 \u0030 to \u0039 ✦ Class methods: 'A' to 'Z' 65 to 90 \u0041 to \u005A 'a' to 'z' 97 to 122 \u0061 to \u007A – Trigonometric Methods – Exponent Methods – Rounding Methods – min, max, abs, and random Methods Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 21 22 rights reserved. rights reserved. Appendix B: ASCII Character Set Escape Sequences for Special Characters ASCII Character Set is a subset of the Unicode from \u0000 to \u007f Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 23 24 rights reserved. rights reserved.
Recommend
More recommend