Chapter 3 Syntax Errors and Debugging Fundamentals of

Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java

Do Now l 1. 2. 3. 4. 5. 2 Evaluate the following expressions 2/3 6. “ 9” + (1 + 4) 15 % 4 7. 5 + 2 / 4 + 2 “ 45” + “ 2” 8. 5 + 2. 0 / 4 + 2 “ 9” + 1 9. 5 + 2 / 4 * 2. 0 “ 9” + 1 + 4 10. 5 + 2 / (4* 2. 0) Fundamentals of Java

Objectives l l l 3 Construct and use numeric and string literals. Name and use variables and constants. Create arithmetic expressions. Understand the precedence of different arithmetic operators. Concatenate two strings or a number and a string. Fundamentals of Java

Objectives (cont. ) l l l 4 Know how and when to use comments in a program. Tell the difference between syntax errors, run -time errors, and logic errors. Insert output statements to debug a program. Fundamentals of Java

Objectives (cont. ) l l 5 Understand the difference between Cartesian coordinates and screen coordinates. Work with color and text properties. Fundamentals of Java

Vocabulary l l l 6 Arithmetic expression Comments Coordinate system Exception Graphics context Literal Fundamentals of Java

Vocabulary (cont. ) l l l 7 Logic error Origin Package Pseudocode Reserved words Run-time error Fundamentals of Java

Vocabulary (cont. ) l l 8 Screen coordinate system Semantics Syntax Virus Fundamentals of Java

Language Elements l Every language, including Java has: – – – 9 Vocabulary: Set of all of the words and symbols in the language Syntax: Rules for combining words into sentences (statements) Semantics: Rules for interpreting the meaning of statements Fundamentals of Java

Language Elements (cont. ) Table 3 -1: Some Java vocabulary 10 Fundamentals of Java

Language Elements (cont. ) l Programming vs. natural languages – – – Programming languages have small vocabularies and simple syntax and semantics. Programming language syntax must be absolutely correct. Programming language statements are interpreted literally. l Every 11 detail must be present. Fundamentals of Java

Basic Java Syntax and Semantics l Two categories of data types: – – l Syntax for manipulating primitive data types differs than for objects – – 12 1. Primitive data types: Numbers, characters, and Booleans 2. Objects Primitive data types are combined in expressions with operators. Objects are sent messages. Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) l Objects must be instantiated before use. – – l Unlike primitives String objects are a little different. Six numeric data types – int and double are most commonly used l Also – short, long, byte, and float Each uses a different number of bytes for storage. l Each 13 represents a different range of values. Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) Table 3 -2: Some Java numeric data types 14 Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) l Literals: Items whose values do not change. – l Variable is a named location in memory. – – 15 The number 5. 0 or the string “Java” Changing a variable’s value is equivalent to replacing the value at the memory location. A variable’s data type cannot change. Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) Figure 3 -1: Changing the value of a variable 16 Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) l Variable declaration statement: Declares the identifier and data type for a variable – – – l 17 int age; (declares one int variable) int a, b, c; (declares three int variables) double d = 2. 45; (declares and initializes a variable) Constants are variables whose value cannot change. – final double PI = 3. 14; Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) l l Assignment statements: – <variable> = <expression>; – Value of expression assigned to variable Arithmetic expressions: – – – 18 Multiplication and division have higher precedence than addition and subtraction. Operators of same precedence evaluated from left to right. Parentheses are used to change evaluation order. Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) 19 Table 3 -5: Common operators and their precedence Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) l The semantics of division (/) differ for integers and floating-point operators. – – l The modulus operator (%) yields a remainder. – 20 int / int yields an int. double / double yields a double. 11 % 3 yields 2. Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) 21 Table 3 -6: Examples of expressions and their values Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) l l Arithmetic overflow: Assigning a value to a variable that is outside of the ranges of values that the data type can represent Mixed-mode arithmetic: Expressions involving integer and floating-point values – 22 Lower-precision data types (int) temporarily converted to high-precision data types (double) Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) l Type casting: Temporarily converting one data type to another – – Can type cast a single variable or an entire expression Place the desired data type within parentheses before the variable or expression that will be cast to another data type. l int 23 x = (int)(d + 1. 6); Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) l String concatenation: Append a String or value to another String – – – l Escape character (): Used in codes to represent characters that cannot be directly typed into a program – 24 Use the + operator String s = “string 1” + “string 2”; String s 2 = “String 1” + int. Variable 1; “t” is a tab character Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) l The String class’s length method gives the number of ch`aracters in a String. l Classes implement methods, and objects are instances of classes. – Objects can respond to a message only if their class implements the method. l Must implement a method with a matching signature 25 Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) l Method signature: – – l Method and variable names are user defined symbols. – l 26 Method name Number and data types of method parameters Cannot use Java keywords (reserved words) Packages: Used to organize related classes into a single unit for distribution Fundamentals of Java

Basic Java Syntax and Semantics (cont. ) 27 Table 3 -7: Java’s reserved words Fundamentals of Java

Terminal I/O for Different Data Types Table 3 -8: Methods in class Scanner 28 Fundamentals of Java

Terminal I/O for Different Data Types (cont. ) 29 Example 3 -1: Tests three types of input data Fundamentals of Java

Comments l l 30 Explanatory sentences inserted in a program Compiler ignores them Purpose is to make program more readable Two varieties: – End of line comments: All text following a double slash (//) on a single line – Multiline comments: All text occurring between a /* and a */ Fundamentals of Java

Comments (cont. ) l Typical uses of comments: – – 31 Begin a program with a statement of its purpose Explain the purpose of a variable declaration Explain the purpose of a major segment of code Explain the workings of complex or tricky sections of code Fundamentals of Java

Programming Errors l Three types of programming errors: – Syntax errors: When a syntax rule is violated l Detected during compilation l Compiler helps identify error – Run-time errors: Occurs during execution l Dividing by 0 l Detected when program runs l JVM indicates type of error and location 32 Fundamentals of Java

Programming Errors (cont. ) l Three types of programming errors (cont. ): – Logic errors (design errors or bugs): Incorrect logic implemented in the program l Code may be correct in every other way, but does not do what it is supposed to do. l Must thoroughly test and debug the program when an error is found. – 33 Desk checking: Examine code immediately after it is written Fundamentals of Java

Debugging l 34 One debugging method is to add extra lines of code to print values of selected variables at strategic points in the program. Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text l Defining a specialized graphics panel: Define a new class that extends the JPanel class – 35 Inherits all of the properties and methods of a JPanel, but can additional instance variables and methods Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) Example 3. 5: Empty color panel 36 Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) l Every graphics application uses a coordinate system. – – Positions of items on a window specified in terms of two-dimensional points Java uses the screen coordinate system: l The origin (point with coordinates (0, 0)) located at upper-left corner of a panel or frame l Every window, frame, or other type of window has own coordinate system 37 Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) Figure 3 -7: Orientation of Java’s coordinate system 38 Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) l Graphics class: Used to draw on a panel – Every panel maintains an instance of this class. l The – graphics context Shapes drawn on a panel by the Graphics class have a foreground color. l Change 39 color via the set. Color() method. Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) Table 3 -9: Common method in the Graphics class 40 Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) Table 3 -9: Common method in the Graphics class (cont. ) 41 Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) 42 Table 3 -9: Common method in the Graphics class (cont. ) Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) l Every panel instance has a paint. Component() method – – – 43 Called by the JVM when the panel needs to be drawn on the screen Contains instructions for how to draw the panel For custom panels, can write own paint. Component() method, but must also call the superclass’s paint. Component() method Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) Example 3. 6: Colored panel containing a red text message in a blue rectangle 44 Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) Figure 3 -8: Displaying a shape and text in a panel 45 Fundamentals of Java

Graphics and GUIs: Drawing Shapes and Text (cont. ) l l The width and height of a panel can be found using the get. Width() and get. Height() methods, respectively. Font class: Information about a specific font – – 46 Font name, size, and style Font font = new Font(“Arial”, Font. BOLD, 10); Fundamentals of Java

Summary l Use the int data type for whole numbers and double for floating-point numbers. l Variable and method names consist of a letter followed by additional letters or digits. Keywords cannot be used as names. Final variables behave as constants; their values cannot change after they are declared. l l 47 Fundamentals of Java

Summary (cont. ) l l l 48 Arithmetic expressions are evaluated according to precedence. Some expressions yield different results for integer and floating-point operands. Strings may be concatenated. The compiler catches syntax errors. The JVM catches run-time errors. Fundamentals of Java

Summary (cont. ) l l l 49 Logic errors, if caught, are detected by the programmer or user at run-time. Can find and remove logic errors by inserting debugging output statements to view the values of variables. The programmer can modify the color with which images are drawn and the properties of text fonts for a given graphics object. Fundamentals of Java

Summary (cont. ) l Java uses a screen coordinate system to locate the positions of pixels in a window or panel. – – 50 Origin is the upper-left corner of the drawing area. x and y axes increase to the right and downward. Fundamentals of Java
- Slides: 50