chapter 1 9 12 13 18 20 23 review slides
play

Chapter 1-9, 12-13, 18, 20, 23 Review Slides CS1: Java Programming - PDF document

Chapter 1-9, 12-13, 18, 20, 23 Review Slides CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education,


  1. Chapter 1-9, 12-13, 18, 20, 23 Review Slides CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 1 rights reserved. What is a Computer? A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer, and communication devices. Bus Communication Input Output Storage Memory CPU Devices Devices Devices Devices e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor, and Tape Mouse Printer and NIC Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 2 rights reserved. Companion Characteristics of Java Website Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral 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 3 rights reserved.

  2. Declaring Variables int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable; Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 4 rights reserved. Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 5 rights reserved. Identifiers An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. 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 6 rights reserved.

  3. Numerical Data Types Name Range Storage Size – 27 to 27 – 1 (-128 to 127) byte 8-bit signed – 215 to 215 – 1 (-32768 to 32767) short 16-bit signed – 231 to 231 – 1 (-2147483648 to 2147483647) int 32-bit signed – 263 to 263 – 1 long 64-bit signed (i.e., -9223372036854775808 to 9223372036854775807) float Negative range: 32-bit IEEE 754 -3.4028235E+38 to -1.4E-45 Positive range: 1.4E-45 to 3.4028235E+38 double Negative range: 64-bit IEEE 754 -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 7 rights reserved. Numeric Operators Name Meaning Example Result + Addition 34 + 1 35 - Subtraction 34.0 – 0.1 33.9 * Multiplication 300 * 30 9000 / Division 1.0 / 2.0 0.5 % Remainder 20 % 3 2 Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 8 rights reserved. Integer Division +, -, *, /, and % 5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division) Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 9 rights reserved.

  4. How to Evaluate an Expression Though Java has its own way to evaluate an expression behind the scene, the result of a Java expression and its corresponding arithmetic expression are the same. Therefore, you can safely apply the arithmetic rule for evaluating a Java 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 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 10 rights reserved. Conversion Rules When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules: 1. If one of the operands is double, the other is converted into double. 2. Otherwise, if one of the operands is float, the other is converted into float. 3. Otherwise, if one of the operands is long, the other is converted into long. 4. Otherwise, both operands are converted into int. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 11 rights reserved. Type Casting Implicit casting double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) What is wrong? int x = 5 / 2.0; range increases byte, short, int, long, float, double Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 12 rights reserved.

  5. The boolean Type and Operators Often in a program you need to compare two values, such as whether i is greater than j. Java provides six comparison operators (also known as relational operators) that can be used to compare two values. The result of the comparison is a Boolean value: true or false. boolean b = (1 > 2); Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 13 rights reserved. Relational Operators Java Mathematics Name Example Result Operator Symbol (radius is 5) < < less than radius < 0 false <= ≤ less than or equal to radius <= 0 false > > greater than radius > 0 true >= ≥ greater than or equal to radius >= 0 true == = equal to radius == 0 false != ≠ not equal to radius != 0 true Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 14 rights reserved. Multiple Alternative if Statements if (score >= 90.0 ) if (score >= 90.0 ) System.out.print( "A" ); System.out.print( "A" ); else if (score >= 80.0 ) else if (score >= 80.0 ) System.out.print( "B" ); Equivalent System.out.print( "B" ); else if (score >= 70.0 ) System.out.print( "C" ); else if (score >= 70.0 ) else if (score >= 60.0 ) System.out.print( "C" ); System.out.print( "D" ); else else if (score >= 60.0 ) System.out.print( "F" ); 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 15 rights reserved.

  6. Multi-Way if-else Statements Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 16 rights reserved. Logical Operators Operator Name Description ! not logical negation && and logical conjunction || or logical disjunction ^ exclusive or logical exclusion Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 17 rights reserved. switch Statements switch (status) { case 0: compute taxes for single filers; break; case 1: compute taxes for married file jointly; break; case 2: compute taxes for married file separately; break; case 3: compute taxes for head of household; break; default: System.out.println("Errors: invalid status"); System.exit(1); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 18 rights reserved.

  7. switch Statement Flow Chart Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 19 rights reserved. 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 20 rights reserved. The Math Class Class constants: – PI – E Class methods: – 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 21 rights reserved.

  8. ASCII Code for Commonly Used Characters Characters Code Value in Decimal Unicode Value '0' to '9' 48 to 57 \u0030 to \u0039 'A' to 'Z' 65 to 90 \u0041 to \u005A 'a' to 'z' 97 to 122 \u0061 to \u007A Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 22 rights reserved. Escape Sequences for Special Characters Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 23 rights reserved. Appendix B: ASCII Character Set 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 24 rights reserved.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend