Chapter 3 Syntax, Errors, and Debugging

Slides:



Advertisements
Similar presentations
Chapter 8 Improving the User Interface
Advertisements

Chapter 2 First Java Programs
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
JavaScript, Third Edition
Introduction to C Programming
Basic Elements of C++ Chapter 2.
COMPUTER SCIENCE I C++ INTRODUCTION
A First Book of ANSI C Fourth Edition
JAVA PROGRAMMING Chapter 3 SYNTAX, ERRORS, AND DEBUGGING
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Introduction to Python
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Lesson 3: Syntax, Errors, and Debugging. Objectives: –Construct and use numeric and string literals. –Name and use variables and constants. –Create arithmetic.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Computer Science 101 Introduction to Programming.
Chapter 2: Using Data.
Week 1 Algorithmization and Programming Languages.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
3.1 Language Elements Language elements: Vocabulary: The words and symbols in the language. Syntax: The rules for combining words into statements. Semantics:
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
1 Sections 3.3 – 3.4 Terminal I/O and Comments Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2 Introduction to C++ Programming
Chapter 3 Syntax, Errors, and Debugging
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Lesson 3: Syntax, Errors, and Debugging
Introduction to Scripting
Section 3.2c Strings and Method Signatures
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

Objectives 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. Know how and when to use comments in a program. 2 2

Objectives (continued) Tell the difference between syntax errors, run-time errors, and logic errors. Insert output statements to debug a program. Understand the difference between Cartesian coordinates and screen coordinates. Work with color and text properties. 3 3

Vocabulary arithmetic expression comments coordinate system exception graphics context keywords literal logic error method signature origin package 4 4

Vocabulary (continued) pseudocode reserved words run-time error screen coordinate system semantics syntax syntax errors variable declaration statement virus 5 5

Language Elements Programming and natural languages share three elements. Vocabulary: Words and symbols Syntax: Rules for combining statements. Use of operators, parentheses, etc. Semantics: Rules for interpreting statements. Order of precedence 6 6

Language Elements (continued) Programming Vs. Natural Languages: Programming and natural languages have three differences. Size: small vocabulary, simple syntax. Rigidity: Syntax must be absolutely correct. Literalness: Computers follow exact instructions. It is difficult to express complex ideas using limited syntax and semantics of programming languages. 7 7

Basic Java Syntax and Semantics Data Types: Primitive: numbers, characters, Booleans Objects: scanners, strings, and more Syntax: Syntax differs for objects and primitive data. Primitive: data types are combined in expressions with operators (addition, multiplication) Objects: sent messages, must be instantiated before use 8 8

Basic Java Syntax and Semantics (continued) Numbers: Numeric data types: Most programs input numeric data, perform calculations, output numeric results. Java includes six numeric data types but we use two: 9 9

Basic Java Syntax and Semantics (continued) Literals: Literals are items in a program whose values do not change. Restricted to primitive data types and strings. Examples of numeric literals 10 10

Basic Java Syntax and Semantics (continued) Variables and Their Declarations: Variables are items whose values can change during execution. Changing the value of a variable 11 11

Basic Java Syntax and Semantics (continued) Variables and Their Declarations (cont): Before using a variable, the program must declare its type. Variable declaration statement Type on left; variable name on right Several variables and values can be in the same statement. 12 12

Basic Java Syntax and Semantics (continued) Variables and Their Declarations (cont): Instantiation creates objects. Constants are variables that, once initialized, cannot change. 13 13

Basic Java Syntax and Semantics (continued) Assignment statements have variables on the left and values on the right. Arithmetic expressions are operands and operators. Multiplication/division before addition/subtraction. Equal operators calculated from left to right. Use parentheses to change the order. 14 14

Basic Java Syntax and Semantics (continued) Explanation about points concerning operators: Division has different semantics for integer and floating-point operands. % yields the remainder of one number divided by another. Java applies operators of higher precedence over lower precedence. Parentheses must occur in pairs, but can be nested. 15 15

Basic Java Syntax and Semantics (continued) Maximum, Minimum, and Arithmetic Overflow: Numeric data types support a finite range of values. Programmers use constants to represent value range. Arithmetic overflow: when values are outside of range. JVM inverts the sign of the number and allows the number to continue. 16 16

Basic Java Syntax and Semantics (continued) Mixed-Mode Arithmetic: In Java, integers are converted to doubles (floating-point) when mixed. Casting to int and double: Casting allows data types to be converted. The cast operator has higher precedence. Cast operators are useful for rounding floating-point numbers to integers. 17 17

Basic Java Syntax and Semantics (continued) String Expressions and Methods: Strings can be literals or assigned to variables. Strings can also be combined using concatenation operator and be sent messages. Combine field names “first name” and “last name” to produce Bill Smith. Strings can be concatenated to numbers. 18 18

Basic Java Syntax and Semantics (continued) String Expressions and Methods (cont): Escape character (\) is used to indicate that a quotation mark is to be taken literally, not as a delimiter. Used to have commas and quotations in output. Escape also used to indicate tabs (\t) and more. If \ is needed in a string, use two (\\). A string returns its length in response to a length message. 19

Basic Java Syntax and Semantics (continued) Methods, Messages, and Signatures: An object can respond to a message only if its class implements a corresponding message (same name). To use a method, you must know: What type of value it returns Its name The number and type of parameters it expects A method’s name and the types and numbers of its parameters are called the method’s signature. 20 20

Basic Java Syntax and Semantics (continued) User-Defined Symbols: Variable and program names are examples of user-defined symbols. User-defined symbols consist of a letter (A …Z), (a … z), (_ and $), followed by a sequence of letters and/or digits (0 … 9). Names are case-sensitive. Keywords and reserved words cannot be used as they have special meaning. Else, byte, char, do, return, and more 21 21

Basic Java Syntax and Semantics (continued) Packages and the import Statement: Packages allow programmers to share code. Packages are collections of classes that can be imported into a program. An import statement form is: x is the package name y is the subsection in the package z is the class in the package 22 22

Terminal I/O for Different Data Types Objects support terminal I/O. An instance of the Scanner class supports input and the object System.out supports output. System.out is an instance of the class PrintStream. This class and others are available to Java programmers without using import statements. 23 23

Terminal I/O for Different Data Types (continued) When a program encounters an input statement, it pauses and waits for the user to press Enter, then processes the user’s input. Interaction with user (bold) looks like this: 24 24

Comments Comments are explanatory sentences inserted in a program used to clarify code and are ignored by the compiler. End of line comments (followed by //) Multi-line comments (opened by /* and closed by */) 25 25

Comments (continued) To make a program readable: 26 26 Begin with a statement of purpose. Use comments to explain variables’ purposes. Precede major segments of code with brief comments. Include comments to explain complex or tricky code sections. 26 26

Programming Errors 27 27 The Three Types of Errors: Syntax errors are detected at compile time. Missing semi-colon or misspelling. Run-time errors are when a computer is asked to do something illegal. Divide by 0. Null pointer: sending a message to uninstantiated object. Java is case-sensitive, so Main and main are different. 27 27

Programming Errors (continued) The Three Types of Errors (cont): Logic errors occur when we fail to express ourselves accurately. Tell someone to turn left, but we mean to say right. Detected by incorrect output. Use test data to compare output with expected results. Desk checking: rereading code carefully. 28 28

Debugging After a bug is detected, you must find it. 29 29 Could reread the code, but the logic error is hard to find and detect. Debug: Add lines of code to print values of variables in the terminal window, then run the program again. Incorrect output from the temperature conversion program 29 29

Graphics and GUIs: Drawing Shapes and Text Defining a Specialized Panel Class: An application window in a GUI program has a defined set of responsibilities. Before we create and display other objects, ask which object will be responsible for them. The application window or the panel in which they appear Divide the labor and delegate responsibility. Define a new panel by creating a new class 30 30

Graphics and GUIs: Drawing Shapes and Text (continued) Coordinate Systems: Positions in this system are specified in terms of points with x and y coordinates relative to the origin (0,0). Java uses a screen coordinate system. 31 31

Graphics and GUIs: Drawing Shapes and Text (continued) The Graphics Class: The package java.awt provides a Graphics class for drawing in a panel. A panel maintains an instance of this class, called a graphics context. Allows program to access and modify a panel’s bitmap Each shape is drawn in a graphics context with a foreground color (black default). 32 32

Graphics and GUIs: Drawing Shapes and Text (continued) Sample method in the Graphics class: 33 33

Graphics and GUIs: Drawing Shapes and Text (continued) Drawing Shapes with the Method paintComponent: The responsibilities of a panel class: Draw images in response to messages from the application Redraw images when window is refreshed When a window opens, the JVM sends the message paintComponent to each object. Objects with images to draw do so The application does not call paintComponent; it is triggered by the JVM in response to events. 34 34

Graphics and GUIs: Drawing Shapes and Text (continued) Finding the Width and Height of a Panel: Find the width and height to center an image on a panel and keep it centered when window resizes. Use getWidth() and getHeight(). Text Properties and the Font Class: In a bitmapped display, text is drawn like an image. 35 35

Summary In this chapter, you learned: Java programs use the int data type for whole numbers (integers) and double for floatingpoint numbers (numbers with decimals). Java variable and method names consist of a letter followed by additional letters or digits. Java keywords cannot be used as names. 36 36

Summary (continued) Final variables behave as constants; their values cannot change after they are declared. Arithmetic expressions are evaluated according to precedence. Some expressions yield different results for integer and floating-point operands. Strings may be concatenated to form a new string. 37 37

Summary (continued) The compiler catches syntax errors. The JVM catches run-time errors. Logic errors, if they are caught, are detected by the programmer or user of the program at run time. A useful way to find and remove logic errors is to insert debugging output statements to view the values of variables. 38 38

Summary (continued) Java uses a screen coordinate system to locate the positions of pixels in a window or panel. The origin of this system is in the upper-left corner or the drawing area, and the x and y axes increase to the right and downward, respectively. The programmer can modify the color with which images are drawn and the properties of text fonts for a given graphics object. 39 39