Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Chapter 3 Syntax, Errors, and Debugging
Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
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”
Data types and variables
Fundamentals of Java Lesson 3: Syntax, Errors, and Debugging
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Java Chapter 2 Creating Classes. Class Defines structure of an object or set of objects; Includes variables (data) and methods (actions) which determine.
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.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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.
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2: Using Data.
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.
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.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 2 Variables.
 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.
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
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?
End of the beginning Let’s wrap up some details and be sure we are all on the same page Good way to make friends and be popular.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
3.1 Language Elements Language elements: Vocabulary: The words and symbols in the language. Syntax: The rules for combining words into statements. Semantics:
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 4 Assignment Statement
Chapter 2 Introduction to C++ Programming
Chapter 3 Syntax, Errors, and Debugging
Completing the Problem-Solving Process
Java Primer 1: Types, Classes and Operators
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Lesson 3: Syntax, Errors, and Debugging
Chapter 3 Assignment Statement
Section 3.2c Strings and Method Signatures
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Review of Java Fundamentals
Presentation transcript:

Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java

2 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.

Fundamentals of Java 3 Objectives (cont.) 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 4 Objectives (cont.) Understand the difference between Cartesian coordinates and screen coordinates. Work with color and text properties.

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

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

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

Fundamentals of Java 8 Language Elements Every language, including Java has: – 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 9 Language Elements (cont.) Table 3-1: Some Java vocabulary

Fundamentals of Java 10 Language Elements (cont.) 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. Every detail must be present.

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

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

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

Fundamentals of Java 14 Basic Java Syntax and Semantics (cont.) Literals: Items whose values do not change. – The number 5.0 or the string “Java” Variable is a named location in memory. – 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 15 Basic Java Syntax and Semantics (cont.) Figure 3-1: Changing the value of a variable

Fundamentals of Java 16 Basic Java Syntax and Semantics (cont.) Variable declaration statement: Declares the identifier and data type for a variable – 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 17 Basic Java Syntax and Semantics (cont.) Assignment statements: – = ; – Value of expression assigned to variable Arithmetic expressions: – 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 18 Basic Java Syntax and Semantics (cont.) Table 3-5: Common operators and their precedence

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

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

Fundamentals of Java 21 Basic Java Syntax and Semantics (cont.) 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 – Lower-precision data types ( int ) temporarily converted to high-precision data types ( double )

Fundamentals of Java 22 Basic Java Syntax and Semantics (cont.) 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. int x = (int)(d + 1.6);

Fundamentals of Java 23 Basic Java Syntax and Semantics (cont.) String concatenation: Append a String or value to another String – Use the + operator – String s = “string1” + “string2”; – String s2 = “String1” + intVariable1; Escape character ( \ ): Used in codes to represent characters that cannot be directly typed into a program – “ \t ” is a tab character

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

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

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

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

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

Fundamentals of Java 29 Comments 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 30 Comments (cont.) Typical uses of comments: – 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 31 Programming Errors Three types of programming errors: – Syntax errors: When a syntax rule is violated Detected during compilation Compiler helps identify error – Run-time errors: Occurs during execution Dividing by 0 Detected when program runs JVM indicates type of error and location

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

Fundamentals of Java 33 Debugging 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 34 Summary Use the int data type for whole numbers and double for floating-point numbers. 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.

Fundamentals of Java 35 Summary (cont.) 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 36 Summary (cont.) 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 37 Summary (cont.) Java uses a screen coordinate system to locate the positions of pixels in a window or panel. – Origin is the upper-left corner of the drawing area. – x and y axes increase to the right and downward.