SlideShare a Scribd company logo
Chapter 3
Syntax, Errors, and Debugging
Fundamentals of Java
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:
– <variable> = <expression>;
– 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
Graphics and GUIs: Drawing
Shapes and Text
 Defining a specialized graphics panel: Define
a new class that extends the JPanel class
– Inherits all of the properties and methods of a
JPanel, but can add additional instance
variables and methods
Fundamentals of Java
35
Graphics and GUIs: Drawing
Shapes and Text (cont.)
Example 3.5: Empty color panel
Fundamentals of Java
36
Graphics and GUIs: Drawing
Shapes and Text (cont.)
 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:
 The origin (point with coordinates (0,0)) located
at upper-left corner of a panel or frame
 Every window, frame, or other type of window
has own coordinate system
Fundamentals of Java
37
Graphics and GUIs: Drawing
Shapes and Text (cont.)
Figure 3-7: Orientation of Java’s coordinate system
Fundamentals of Java
38
Graphics and GUIs: Drawing
Shapes and Text (cont.)
 Graphics class: Used to draw on a panel
– Every panel maintains an instance of this class.
 The graphics context
– Shapes drawn on a panel by the Graphics
class have a foreground color.
 Change color via the setColor() method.
Fundamentals of Java
39
Graphics and GUIs: Drawing
Shapes and Text (cont.)
Table 3-9: Common method in the Graphics class
Fundamentals of Java
40
Graphics and GUIs: Drawing
Shapes and Text (cont.)
Table 3-9: Common method in the Graphics class (cont.)
Fundamentals of Java
41
Graphics and GUIs: Drawing
Shapes and Text (cont.)
Table 3-9: Common method in the Graphics class (cont.)
Fundamentals of Java
42
Graphics and GUIs: Drawing
Shapes and Text (cont.)
 Every panel instance has a
paintComponent() method
– 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
paintComponent() method, but must also
call the superclass’s paintComponent()
method
Fundamentals of Java
43
Graphics and GUIs: Drawing
Shapes and Text (cont.)
Example 3.6: Colored panel containing a
red text message in a blue rectangle
Fundamentals of Java
44
Graphics and GUIs: Drawing
Shapes and Text (cont.)
Figure 3-8: Displaying a shape and text in a panel
Fundamentals of Java
45
Graphics and GUIs: Drawing
Shapes and Text (cont.)
 The width and height of a panel can be found
using the getWidth() and getHeight()
methods, respectively.
 Font class: Information about a specific font
– Font name, size, and style
– Font font = new Font(“Arial”,
Font.BOLD, 10);
Fundamentals of Java
46
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
47
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
48
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
49
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.

More Related Content

What's hot

Ppt chapter12
Ppt chapter12Ppt chapter12
Ppt chapter12
Richard Styner
 
Pptchapter04
Pptchapter04Pptchapter04
Pptchapter04
Richard Styner
 
Ppt chapter11
Ppt chapter11Ppt chapter11
Ppt chapter11
Richard Styner
 
Ppt chapter09
Ppt chapter09Ppt chapter09
Ppt chapter09
Richard Styner
 
Big Java Chapter 1
Big Java Chapter 1Big Java Chapter 1
Big Java Chapter 1
Maria Joslin
 
Java Programming Assignment
Java Programming AssignmentJava Programming Assignment
Java Programming Assignment
Vijayananda Mohire
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
cesarmendez78
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
Intelligo Technologies
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
Hamad Odhabi
 
Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
Syed Afaq Shah MACS CP
 
Java OOP Concepts 1st Slide
Java OOP Concepts 1st SlideJava OOP Concepts 1st Slide
Java OOP Concepts 1st Slide
sunny khan
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
New York City College of Technology Computer Systems Technology Colloquium
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
Prarabdh Garg
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaAdan Hubahib
 
Chapter 2 java
Chapter 2 javaChapter 2 java
Chapter 2 java
ahmed abugharsa
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
Abdul Hannan
 

What's hot (18)

Ppt chapter12
Ppt chapter12Ppt chapter12
Ppt chapter12
 
Pptchapter04
Pptchapter04Pptchapter04
Pptchapter04
 
Ppt chapter11
Ppt chapter11Ppt chapter11
Ppt chapter11
 
Ppt chapter09
Ppt chapter09Ppt chapter09
Ppt chapter09
 
Big Java Chapter 1
Big Java Chapter 1Big Java Chapter 1
Big Java Chapter 1
 
Java Programming Assignment
Java Programming AssignmentJava Programming Assignment
Java Programming Assignment
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
 
Chap03
Chap03Chap03
Chap03
 
Chap03
Chap03Chap03
Chap03
 
Java OOP Concepts 1st Slide
Java OOP Concepts 1st SlideJava OOP Concepts 1st Slide
Java OOP Concepts 1st Slide
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
Chapter 2 java
Chapter 2 javaChapter 2 java
Chapter 2 java
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
 

Similar to Ppt chapter03

Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
SaqlainYaqub1
 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
Md Mofijul Haque
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
PriyadarshiniS28
 
In this page, we will learn about the basics of OOPs. Object-Oriented Program...
In this page, we will learn about the basics of OOPs. Object-Oriented Program...In this page, we will learn about the basics of OOPs. Object-Oriented Program...
In this page, we will learn about the basics of OOPs. Object-Oriented Program...
Indu32
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
Vasuki Ramasamy
 
Lecture 21 22
Lecture 21 22Lecture 21 22
Lecture 21 22
Najmul Hassan
 
C#
C#C#
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environments
J'tong Atong
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java
MayaTofik
 
Full CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java languageFull CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java language
ssuser2963071
 
c#.pptx
c#.pptxc#.pptx
presentation of java fundamental
presentation of java fundamentalpresentation of java fundamental
presentation of java fundamental
Ganesh Chittalwar
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
Vishwa Mohan
 
2021 icse reducedsylabiix-computer applications
2021 icse reducedsylabiix-computer applications2021 icse reducedsylabiix-computer applications
2021 icse reducedsylabiix-computer applications
Vahabshaik Shai
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdf
ssusera0bb35
 
Ase02 dmp.ppt
Ase02 dmp.pptAse02 dmp.ppt
Ase02 dmp.ppt
Yann-Gaël Guéhéneuc
 
java handout.doc
java handout.docjava handout.doc
java handout.doc
SOMOSCO1
 
Dot net programming concept
Dot net  programming conceptDot net  programming concept
Dot net programming concept
sandeshjadhav28
 

Similar to Ppt chapter03 (20)

Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
In this page, we will learn about the basics of OOPs. Object-Oriented Program...
In this page, we will learn about the basics of OOPs. Object-Oriented Program...In this page, we will learn about the basics of OOPs. Object-Oriented Program...
In this page, we will learn about the basics of OOPs. Object-Oriented Program...
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
 
Lecture 21 22
Lecture 21 22Lecture 21 22
Lecture 21 22
 
C#
C#C#
C#
 
Chap02
Chap02Chap02
Chap02
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environments
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java
 
Full CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java languageFull CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java language
 
c#.pptx
c#.pptxc#.pptx
c#.pptx
 
presentation of java fundamental
presentation of java fundamentalpresentation of java fundamental
presentation of java fundamental
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
Introduction to Visual Basic
Introduction to Visual Basic Introduction to Visual Basic
Introduction to Visual Basic
 
2021 icse reducedsylabiix-computer applications
2021 icse reducedsylabiix-computer applications2021 icse reducedsylabiix-computer applications
2021 icse reducedsylabiix-computer applications
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdf
 
Ase02 dmp.ppt
Ase02 dmp.pptAse02 dmp.ppt
Ase02 dmp.ppt
 
java handout.doc
java handout.docjava handout.doc
java handout.doc
 
Dot net programming concept
Dot net  programming conceptDot net  programming concept
Dot net programming concept
 

More from Richard Styner

OneNote for Students.pptx
OneNote for Students.pptxOneNote for Students.pptx
OneNote for Students.pptx
Richard Styner
 
Hopscotch.docx
Hopscotch.docxHopscotch.docx
Hopscotch.docx
Richard Styner
 
Lit Review + Case Study (1).docx
Lit Review + Case Study (1).docxLit Review + Case Study (1).docx
Lit Review + Case Study (1).docx
Richard Styner
 
udlspace.docx
udlspace.docxudlspace.docx
udlspace.docx
Richard Styner
 
Documentary Video UDL Lesson.docx
Documentary Video UDL Lesson.docxDocumentary Video UDL Lesson.docx
Documentary Video UDL Lesson.docx
Richard Styner
 
rubric.docx
rubric.docxrubric.docx
rubric.docx
Richard Styner
 
Ppt chapter04
Ppt chapter04Ppt chapter04
Ppt chapter04
Richard Styner
 
Ppt chapter03
Ppt chapter03Ppt chapter03
Ppt chapter03
Richard Styner
 
Richard Styner Issues and trends
Richard Styner Issues and trendsRichard Styner Issues and trends
Richard Styner Issues and trends
Richard Styner
 
Ppt chapter 01
Ppt chapter 01Ppt chapter 01
Ppt chapter 01
Richard Styner
 
Tech mentoring project presentation
Tech mentoring project presentationTech mentoring project presentation
Tech mentoring project presentation
Richard Styner
 
The Photoshop Effect
The Photoshop EffectThe Photoshop Effect
The Photoshop Effect
Richard Styner
 
Stanford Solar Center Joint Curriculum
Stanford Solar Center Joint CurriculumStanford Solar Center Joint Curriculum
Stanford Solar Center Joint Curriculum
Richard Styner
 
The Stanford Solar Lab
The Stanford Solar Lab The Stanford Solar Lab
The Stanford Solar Lab
Richard Styner
 

More from Richard Styner (14)

OneNote for Students.pptx
OneNote for Students.pptxOneNote for Students.pptx
OneNote for Students.pptx
 
Hopscotch.docx
Hopscotch.docxHopscotch.docx
Hopscotch.docx
 
Lit Review + Case Study (1).docx
Lit Review + Case Study (1).docxLit Review + Case Study (1).docx
Lit Review + Case Study (1).docx
 
udlspace.docx
udlspace.docxudlspace.docx
udlspace.docx
 
Documentary Video UDL Lesson.docx
Documentary Video UDL Lesson.docxDocumentary Video UDL Lesson.docx
Documentary Video UDL Lesson.docx
 
rubric.docx
rubric.docxrubric.docx
rubric.docx
 
Ppt chapter04
Ppt chapter04Ppt chapter04
Ppt chapter04
 
Ppt chapter03
Ppt chapter03Ppt chapter03
Ppt chapter03
 
Richard Styner Issues and trends
Richard Styner Issues and trendsRichard Styner Issues and trends
Richard Styner Issues and trends
 
Ppt chapter 01
Ppt chapter 01Ppt chapter 01
Ppt chapter 01
 
Tech mentoring project presentation
Tech mentoring project presentationTech mentoring project presentation
Tech mentoring project presentation
 
The Photoshop Effect
The Photoshop EffectThe Photoshop Effect
The Photoshop Effect
 
Stanford Solar Center Joint Curriculum
Stanford Solar Center Joint CurriculumStanford Solar Center Joint Curriculum
Stanford Solar Center Joint Curriculum
 
The Stanford Solar Lab
The Stanford Solar Lab The Stanford Solar Lab
The Stanford Solar Lab
 

Recently uploaded

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
ShivajiThube2
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 

Recently uploaded (20)

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 

Ppt chapter03

  • 1. Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java
  • 2. 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.
  • 3. 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.
  • 4. Fundamentals of Java 4 Objectives (cont.)  Understand the difference between Cartesian coordinates and screen coordinates.  Work with color and text properties.
  • 5. Fundamentals of Java 5 Vocabulary  Arithmetic expression  Comments  Coordinate system  Exception  Graphics context  Literal
  • 6. Fundamentals of Java 6 Vocabulary (cont.)  Logic error  Origin  Package  Pseudocode  Reserved words  Run-time error
  • 7. Fundamentals of Java 7 Vocabulary (cont.)  Screen coordinate system  Semantics  Syntax  Virus
  • 8. 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
  • 9. Fundamentals of Java 9 Language Elements (cont.) Table 3-1: Some Java vocabulary
  • 10. 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.
  • 11. 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.
  • 12. 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.
  • 13. Fundamentals of Java 13 Basic Java Syntax and Semantics (cont.) Table 3-2: Some Java numeric data types
  • 14. 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.
  • 15. Fundamentals of Java 15 Basic Java Syntax and Semantics (cont.) Figure 3-1: Changing the value of a variable
  • 16. 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;
  • 17. Fundamentals of Java 17 Basic Java Syntax and Semantics (cont.)  Assignment statements: – <variable> = <expression>; – 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.
  • 18. Fundamentals of Java 18 Basic Java Syntax and Semantics (cont.) Table 3-5: Common operators and their precedence
  • 19. 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.
  • 20. Fundamentals of Java 20 Basic Java Syntax and Semantics (cont.) Table 3-6: Examples of expressions and their values
  • 21. 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)
  • 22. 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);
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. Fundamentals of Java 26 Basic Java Syntax and Semantics (cont.) Table 3-7: Java’s reserved words
  • 27. Fundamentals of Java 27 Terminal I/O for Different Data Types Table 3-8: Methods in class Scanner
  • 28. Fundamentals of Java 28 Terminal I/O for Different Data Types (cont.) Example 3-1: Tests three types of input data
  • 29. 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 */
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. 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.
  • 34. Fundamentals of Java 34 Graphics and GUIs: Drawing Shapes and Text  Defining a specialized graphics panel: Define a new class that extends the JPanel class – Inherits all of the properties and methods of a JPanel, but can add additional instance variables and methods
  • 35. Fundamentals of Java 35 Graphics and GUIs: Drawing Shapes and Text (cont.) Example 3.5: Empty color panel
  • 36. Fundamentals of Java 36 Graphics and GUIs: Drawing Shapes and Text (cont.)  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:  The origin (point with coordinates (0,0)) located at upper-left corner of a panel or frame  Every window, frame, or other type of window has own coordinate system
  • 37. Fundamentals of Java 37 Graphics and GUIs: Drawing Shapes and Text (cont.) Figure 3-7: Orientation of Java’s coordinate system
  • 38. Fundamentals of Java 38 Graphics and GUIs: Drawing Shapes and Text (cont.)  Graphics class: Used to draw on a panel – Every panel maintains an instance of this class.  The graphics context – Shapes drawn on a panel by the Graphics class have a foreground color.  Change color via the setColor() method.
  • 39. Fundamentals of Java 39 Graphics and GUIs: Drawing Shapes and Text (cont.) Table 3-9: Common method in the Graphics class
  • 40. Fundamentals of Java 40 Graphics and GUIs: Drawing Shapes and Text (cont.) Table 3-9: Common method in the Graphics class (cont.)
  • 41. Fundamentals of Java 41 Graphics and GUIs: Drawing Shapes and Text (cont.) Table 3-9: Common method in the Graphics class (cont.)
  • 42. Fundamentals of Java 42 Graphics and GUIs: Drawing Shapes and Text (cont.)  Every panel instance has a paintComponent() method – 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 paintComponent() method, but must also call the superclass’s paintComponent() method
  • 43. Fundamentals of Java 43 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 44 Graphics and GUIs: Drawing Shapes and Text (cont.) Figure 3-8: Displaying a shape and text in a panel
  • 45. Fundamentals of Java 45 Graphics and GUIs: Drawing Shapes and Text (cont.)  The width and height of a panel can be found using the getWidth() and getHeight() methods, respectively.  Font class: Information about a specific font – Font name, size, and style – Font font = new Font(“Arial”, Font.BOLD, 10);
  • 46. Fundamentals of Java 46 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.
  • 47. Fundamentals of Java 47 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.
  • 48. Fundamentals of Java 48 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.
  • 49. Fundamentals of Java 49 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.