SlideShare a Scribd company logo
Java Programming: From Problem Analysis to Program Design, 3e Chapter 2 Basic Elements of Java
Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols, and identifiers Explore primitive data types Discover how to use arithmetic operators Examine how a program evaluates arithmetic expressions Explore how mixed expressions are evaluated Java Programming: From Problem Analysis to Program Design, 3e
Chapter Objectives (continued) Learn about type casting Become familiar with the  String  type Learn what an assignment statement is and what it does Discover how to input data into memory by using input statements Become familiar with the use of increment and decrement operators Java Programming: From Problem Analysis to Program Design, 3e
Chapter Objectives (continued) Examine ways to output results using output statements Learn how to import packages and why they are necessary Discover how to create a Java application program Explore how to properly structure a program, including using comments to document a program Java Programming: From Problem Analysis to Program Design, 3e
Introduction Computer program :  a sequence of statements whose objective is to accomplish a task   Programming:   process of planning and creating a program Java Programming: From Problem Analysis to Program Design, 3e
The Basics of a Java Program Java program: collection of classes There is a main method in every Java application program Token: smallest individual unit of a program Java Programming: From Problem Analysis to Program Design, 3e
Special Symbols Java Programming: From Problem Analysis to Program Design, 3e
Reserved Words (Keywords) int float double char void public static throws  return Java Programming: From Problem Analysis to Program Design, 3e
Java Identifiers Names of things Consist of:  Letters Digits The underscore character ( _ ) The dollar sign ( $ ) Must begin with a letter, underscore, or the dollar sign Java Programming: From Problem Analysis to Program Design, 3e
Illegal Identifiers Java Programming: From Problem Analysis to Program Design, 3e
Data Types Data type:  set of values together with a set of operations Java Programming: From Problem Analysis to Program Design, 3e
Primitive Data Types Java Programming: From Problem Analysis to Program Design, 3e
Integral Data Types Java Programming: From Problem Analysis to Program Design, 3e
Values and Memory Allocation for Integral Data Types Java Programming: From Problem Analysis to Program Design, 3e
Primitive Data Types Floating-Point Data Types float : precision = 6 or 7 double : precision = 15 boolean : two values true false Java Programming: From Problem Analysis to Program Design, 3e
Arithmetic Operators and Operator Precedence Five Arithmetic Operators +  addition -  subtraction *  multiplication /  division %  mod (modulus) Unary operator: operator that has one operand Binary operator:   operator that has two operands Java Programming: From Problem Analysis to Program Design, 3e
Order of Precedence *  / % (same precedence) + - (same precedence) Operators in 1 have a higher precedence than operators in 2 When operators have the same level of precedence, operations are performed from left to right Java Programming: From Problem Analysis to Program Design, 3e
Expressions Integral expressions Floating-point or decimal expressions Mixed expressions Java Programming: From Problem Analysis to Program Design, 3e
Integral Expressions All operands are integers Examples 2 + 3 * 5 3 + x – y / 7 x + 2 * (y – z) + 18 Java Programming: From Problem Analysis to Program Design, 3e
Floating-Point Expressions All operands are floating-point numbers Examples 12.8 * 17.5 – 34.50 x * 10.5 + y - 16.2 Java Programming: From Problem Analysis to Program Design, 3e
Mixed Expressions Operands of different types Examples 2 + 3.5 6 / 4 + 3.9 Integer operands yield an integer result; floating-point numbers yield floating-point results If both types of operands are present, the result is a floating-point number Precedence rules are followed Java Programming: From Problem Analysis to Program Design, 3e
Type Conversion (Casting) Used to avoid implicit type coercion Syntax (dataTypeName) expression Expression evaluated first, then type converted to  dataTypeName Examples ( int )(7.9 + 6.7) = 14 ( int )(7.9) + ( int )(6.7) = 13 Java Programming: From Problem Analysis to Program Design, 3e
The  class  String Used to manipulate strings String Sequence of zero or more characters Enclosed in double quotation marks Null or empty strings have no characters Numeric strings consist of integers or decimal numbers Length is the number of characters in a string Java Programming: From Problem Analysis to Program Design, 3e
Input Named constant  Cannot be changed during program execution Declared by using the reserved word  final Initialized when it is declared Example 2-11 final double  CENTIMETERS_PER_INCH = 2.54; final int  NO_OF_STUDENTS = 20; final char   BLANK = ' '; final double   PAY_RATE = 15.75; Java Programming: From Problem Analysis to Program Design, 3e
Input (continued) Variable (name, value, data type, size) Content may change during program execution Must be declared before it can be used May not be automatically initialized  If new value is assigned, old one is destroyed Value can only be changed by an assignment statement or an input (read) statement Example 2-12  double  amountDue; int   counter; char   ch; int   num1, num2; Java Programming: From Problem Analysis to Program Design, 3e
Input (continued) The Assignment Statement variable = expression; Example 2-13 int  num1; int  num2; double  sale; char  first; String  str; num1 = 4; num2 = 4 * 5 - 11; sale = 0.02 * 1000; first = 'D'; str = "It is a sunny day."; Java Programming: From Problem Analysis to Program Design, 3e
Input (continued) Standard input stream object:  System.in Input numeric data to program Separate by blanks, lines, or tabs To read data: Create an input stream object of the  class   Scanner Use the methods such as  next ,  nextLine ,  nextInt , and  nextDouble Java Programming: From Problem Analysis to Program Design, 3e
Input (continued) static  Scanner console =  new  Scanner(System.in); Example 2-16 static Scanner console =  new  Scanner(System.in); int   feet; int  inches; Suppose the input is 23 7 feet = console.nextInt();   //Line 1 inches = console.nextInt(); //Line 2 Java Programming: From Problem Analysis to Program Design, 3e
Increment and Decrement Operators ++  increments the value of its operand by 1 --  decrements the value of its operand by 1 Syntax Pre-increment:  ++variable Post-increment:  variable++ Pre-decrement:  --variable Post-decrement:  variable-- Java Programming: From Problem Analysis to Program Design, 3e
Strings and the Operator + Operator + can be used to concatenate two strings or a string and a numeric value or character Example 2-20 String str; int   num1; int   num2; num1 = 12; num2 = 26; str = "The sum = " + num1 + num2; -After this statement executes, the string assigned to str is: "The sum = 1226"; Java Programming: From Problem Analysis to Program Design, 3e
Strings and the Operator + (continued) Consider the following statement: str = "The sum = " + (num1 + num2); In this statement, because of the parentheses, you first evaluate  num1 + num2 Because  num1  and  num2  are both  int  variables,  num1 + num2 = 12 + 26 = 38 After this statement executes, the string assigned to  str  is: "The sum = 38"; Java Programming: From Problem Analysis to Program Design, 3e
Output Standard output object:  System.out Methods print println   Syntax System.out.print(stringExp); System.out.println(stringExp); System.out.println(); Java Programming: From Problem Analysis to Program Design, 3e
Commonly Used Escape Sequences Java Programming: From Problem Analysis to Program Design, 3e
Packages, Classes, Methods, and the  import  Statement Package:   collection of related classes Class:  consists of methods Method:  designed to accomplish a specific task Java Programming: From Problem Analysis to Program Design, 3e
import  Statement Used to import the components of a package into a program Reserved word import   java.io.*; Imports the (components of the)  package   java.io  into the program Primitive data types and the  class   String Part of the Java language Don’t need to be imported Java Programming: From Problem Analysis to Program Design, 3e
Creating a Java Application Program Syntax of a class Syntax of the  main  method Java Programming: From Problem Analysis to Program Design, 3e
Programming Style and Form Know common syntax errors and rules Use blanks appropriately Semicolon: statement terminator Important to have well-documented code Good practice to follow traditional rules for naming identifiers Java Programming: From Problem Analysis to Program Design, 3e
More on Assignment Statements variable = variable * (expression); is equivalent to variable *= expression; Similarly, variable = variable + (expression); is equivalent to variable += expression;   Java Programming: From Problem Analysis to Program Design, 3e
Programming Examples Convert Length Program Input: length in feet and inches Output: equivalent length in centimeters Make Change Program Input: change in cents Output:  equivalent change in half-dollars, quarters, dimes, nickels, and pennies Java Programming: From Problem Analysis to Program Design, 3e
Chapter Summary Basic Elements of a Java program include: The main method Reserved words Special symbols Identifiers Data types Expressions Input Output Statements Java Programming: From Problem Analysis to Program Design, 3e
Chapter Summary (continued) To create a Java application, it is important to understand: Syntax rules Semantic rules How to manipulate strings and numbers How to declare variables and named constants How to receive input and display output Good programming style and form Java Programming: From Problem Analysis to Program Design, 3e

More Related Content

PDF
Solutions manual for c++ programming from problem analysis to program design ...
Warren0989
 
PPT
Chapter 4 5
ahmed22dg
 
PPT
Chapter2
Anees999
 
PDF
Problem solving methodology
Prof. Dr. K. Adisesha
 
DOC
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
Malikireddy Bramhananda Reddy
 
DOCX
C LANGUAGE NOTES
Malikireddy Bramhananda Reddy
 
PPT
Programming In C++
shammi mehra
 
DOC
Programming in c notes
Sudharasanam Babu
 
Solutions manual for c++ programming from problem analysis to program design ...
Warren0989
 
Chapter 4 5
ahmed22dg
 
Chapter2
Anees999
 
Problem solving methodology
Prof. Dr. K. Adisesha
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
Malikireddy Bramhananda Reddy
 
Programming In C++
shammi mehra
 
Programming in c notes
Sudharasanam Babu
 

What's hot (20)

DOCX
C notes
Raunak Sodhi
 
PDF
Handout#05
Sunita Milind Dol
 
PDF
best notes in c language
India
 
PDF
Handout#02
Sunita Milind Dol
 
PPT
9781439035665 ppt ch02
Terry Yoast
 
PPTX
OOP Poster Presentation
Md Mofijul Haque
 
DOC
Notes of c programming 1st unit BCA I SEM
Mansi Tyagi
 
PDF
C aptitude book
MadipadigaYashwanth
 
PPSX
C basics 4 std11(GujBoard)
indrasir
 
PDF
Handout#11
Sunita Milind Dol
 
PDF
Assignment11
Sunita Milind Dol
 
PPT
Literals,variables,datatype in C#
Prasanna Kumar SM
 
PDF
Handout#03
Sunita Milind Dol
 
PDF
Programming in c
ankitjain851
 
PDF
Handout#07
Sunita Milind Dol
 
PDF
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
Batra Centre
 
DOC
Project 2 the second project involves/tutorialoutlet
Messanz
 
PPT
Overview of c#
Prasanna Kumar SM
 
PDF
Handout#04
Sunita Milind Dol
 
PDF
Abc c program
Dayakar Siddula
 
C notes
Raunak Sodhi
 
Handout#05
Sunita Milind Dol
 
best notes in c language
India
 
Handout#02
Sunita Milind Dol
 
9781439035665 ppt ch02
Terry Yoast
 
OOP Poster Presentation
Md Mofijul Haque
 
Notes of c programming 1st unit BCA I SEM
Mansi Tyagi
 
C aptitude book
MadipadigaYashwanth
 
C basics 4 std11(GujBoard)
indrasir
 
Handout#11
Sunita Milind Dol
 
Assignment11
Sunita Milind Dol
 
Literals,variables,datatype in C#
Prasanna Kumar SM
 
Handout#03
Sunita Milind Dol
 
Programming in c
ankitjain851
 
Handout#07
Sunita Milind Dol
 
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
Batra Centre
 
Project 2 the second project involves/tutorialoutlet
Messanz
 
Overview of c#
Prasanna Kumar SM
 
Handout#04
Sunita Milind Dol
 
Abc c program
Dayakar Siddula
 
Ad

Similar to Chap02 (20)

PPT
9781111530532 ppt ch02
Terry Yoast
 
PPT
9781111530532 ppt ch02
Terry Yoast
 
PPT
Chapter 2 - Basic Elements of Java
Adan Hubahib
 
PPT
Basic elements of java
Ahmad Idrees
 
PPT
Chap03
Terry Yoast
 
PPT
Chap03
Terry Yoast
 
PPT
9781439035665 ppt ch03
Terry Yoast
 
PPTX
Java fundamentals
HCMUTE
 
PPT
Ap Power Point Chpt2
dplunkett
 
PDF
java basics - keywords, statements data types and arrays
mellosuji
 
PPT
9781111530532 ppt ch03
Terry Yoast
 
PPT
9781111530532 ppt ch03
Terry Yoast
 
PPT
Java: Primitive Data Types
Tareq Hasan
 
PPT
Ch02 primitive-data-definite-loops
James Brotsos
 
PPT
Comp102 lec 4
Fraz Bakhsh
 
PPT
ch02-primitive-data-definite-loops.ppt
ghoitsun
 
PPT
ch02-primitive-data-definite-loops.ppt
Mahyuddin8
 
PPTX
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
GANESHBABUVelu
 
PPT
Chapter 2 java
ahmed abugharsa
 
9781111530532 ppt ch02
Terry Yoast
 
9781111530532 ppt ch02
Terry Yoast
 
Chapter 2 - Basic Elements of Java
Adan Hubahib
 
Basic elements of java
Ahmad Idrees
 
Chap03
Terry Yoast
 
Chap03
Terry Yoast
 
9781439035665 ppt ch03
Terry Yoast
 
Java fundamentals
HCMUTE
 
Ap Power Point Chpt2
dplunkett
 
java basics - keywords, statements data types and arrays
mellosuji
 
9781111530532 ppt ch03
Terry Yoast
 
9781111530532 ppt ch03
Terry Yoast
 
Java: Primitive Data Types
Tareq Hasan
 
Ch02 primitive-data-definite-loops
James Brotsos
 
Comp102 lec 4
Fraz Bakhsh
 
ch02-primitive-data-definite-loops.ppt
ghoitsun
 
ch02-primitive-data-definite-loops.ppt
Mahyuddin8
 
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
GANESHBABUVelu
 
Chapter 2 java
ahmed abugharsa
 
Ad

More from Terry Yoast (20)

PPT
9781305078444 ppt ch12
Terry Yoast
 
PPT
9781305078444 ppt ch11
Terry Yoast
 
PPT
9781305078444 ppt ch10
Terry Yoast
 
PPT
9781305078444 ppt ch09
Terry Yoast
 
PPT
9781305078444 ppt ch08
Terry Yoast
 
PPT
9781305078444 ppt ch07
Terry Yoast
 
PPT
9781305078444 ppt ch06
Terry Yoast
 
PPT
9781305078444 ppt ch05
Terry Yoast
 
PPT
9781305078444 ppt ch04
Terry Yoast
 
PPT
9781305078444 ppt ch03
Terry Yoast
 
PPT
9781305078444 ppt ch02
Terry Yoast
 
PPT
9781305078444 ppt ch01
Terry Yoast
 
PPTX
9781337102087 ppt ch13
Terry Yoast
 
PPTX
9781337102087 ppt ch18
Terry Yoast
 
PPTX
9781337102087 ppt ch17
Terry Yoast
 
PPTX
9781337102087 ppt ch16
Terry Yoast
 
PPTX
9781337102087 ppt ch15
Terry Yoast
 
PPTX
9781337102087 ppt ch14
Terry Yoast
 
PPTX
9781337102087 ppt ch12
Terry Yoast
 
PPTX
9781337102087 ppt ch11
Terry Yoast
 
9781305078444 ppt ch12
Terry Yoast
 
9781305078444 ppt ch11
Terry Yoast
 
9781305078444 ppt ch10
Terry Yoast
 
9781305078444 ppt ch09
Terry Yoast
 
9781305078444 ppt ch08
Terry Yoast
 
9781305078444 ppt ch07
Terry Yoast
 
9781305078444 ppt ch06
Terry Yoast
 
9781305078444 ppt ch05
Terry Yoast
 
9781305078444 ppt ch04
Terry Yoast
 
9781305078444 ppt ch03
Terry Yoast
 
9781305078444 ppt ch02
Terry Yoast
 
9781305078444 ppt ch01
Terry Yoast
 
9781337102087 ppt ch13
Terry Yoast
 
9781337102087 ppt ch18
Terry Yoast
 
9781337102087 ppt ch17
Terry Yoast
 
9781337102087 ppt ch16
Terry Yoast
 
9781337102087 ppt ch15
Terry Yoast
 
9781337102087 ppt ch14
Terry Yoast
 
9781337102087 ppt ch12
Terry Yoast
 
9781337102087 ppt ch11
Terry Yoast
 

Recently uploaded (20)

PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
CDH. pptx
AneetaSharma15
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
CDH. pptx
AneetaSharma15
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 

Chap02

  • 1. Java Programming: From Problem Analysis to Program Design, 3e Chapter 2 Basic Elements of Java
  • 2. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols, and identifiers Explore primitive data types Discover how to use arithmetic operators Examine how a program evaluates arithmetic expressions Explore how mixed expressions are evaluated Java Programming: From Problem Analysis to Program Design, 3e
  • 3. Chapter Objectives (continued) Learn about type casting Become familiar with the String type Learn what an assignment statement is and what it does Discover how to input data into memory by using input statements Become familiar with the use of increment and decrement operators Java Programming: From Problem Analysis to Program Design, 3e
  • 4. Chapter Objectives (continued) Examine ways to output results using output statements Learn how to import packages and why they are necessary Discover how to create a Java application program Explore how to properly structure a program, including using comments to document a program Java Programming: From Problem Analysis to Program Design, 3e
  • 5. Introduction Computer program : a sequence of statements whose objective is to accomplish a task Programming: process of planning and creating a program Java Programming: From Problem Analysis to Program Design, 3e
  • 6. The Basics of a Java Program Java program: collection of classes There is a main method in every Java application program Token: smallest individual unit of a program Java Programming: From Problem Analysis to Program Design, 3e
  • 7. Special Symbols Java Programming: From Problem Analysis to Program Design, 3e
  • 8. Reserved Words (Keywords) int float double char void public static throws return Java Programming: From Problem Analysis to Program Design, 3e
  • 9. Java Identifiers Names of things Consist of: Letters Digits The underscore character ( _ ) The dollar sign ( $ ) Must begin with a letter, underscore, or the dollar sign Java Programming: From Problem Analysis to Program Design, 3e
  • 10. Illegal Identifiers Java Programming: From Problem Analysis to Program Design, 3e
  • 11. Data Types Data type: set of values together with a set of operations Java Programming: From Problem Analysis to Program Design, 3e
  • 12. Primitive Data Types Java Programming: From Problem Analysis to Program Design, 3e
  • 13. Integral Data Types Java Programming: From Problem Analysis to Program Design, 3e
  • 14. Values and Memory Allocation for Integral Data Types Java Programming: From Problem Analysis to Program Design, 3e
  • 15. Primitive Data Types Floating-Point Data Types float : precision = 6 or 7 double : precision = 15 boolean : two values true false Java Programming: From Problem Analysis to Program Design, 3e
  • 16. Arithmetic Operators and Operator Precedence Five Arithmetic Operators + addition - subtraction * multiplication / division % mod (modulus) Unary operator: operator that has one operand Binary operator: operator that has two operands Java Programming: From Problem Analysis to Program Design, 3e
  • 17. Order of Precedence * / % (same precedence) + - (same precedence) Operators in 1 have a higher precedence than operators in 2 When operators have the same level of precedence, operations are performed from left to right Java Programming: From Problem Analysis to Program Design, 3e
  • 18. Expressions Integral expressions Floating-point or decimal expressions Mixed expressions Java Programming: From Problem Analysis to Program Design, 3e
  • 19. Integral Expressions All operands are integers Examples 2 + 3 * 5 3 + x – y / 7 x + 2 * (y – z) + 18 Java Programming: From Problem Analysis to Program Design, 3e
  • 20. Floating-Point Expressions All operands are floating-point numbers Examples 12.8 * 17.5 – 34.50 x * 10.5 + y - 16.2 Java Programming: From Problem Analysis to Program Design, 3e
  • 21. Mixed Expressions Operands of different types Examples 2 + 3.5 6 / 4 + 3.9 Integer operands yield an integer result; floating-point numbers yield floating-point results If both types of operands are present, the result is a floating-point number Precedence rules are followed Java Programming: From Problem Analysis to Program Design, 3e
  • 22. Type Conversion (Casting) Used to avoid implicit type coercion Syntax (dataTypeName) expression Expression evaluated first, then type converted to dataTypeName Examples ( int )(7.9 + 6.7) = 14 ( int )(7.9) + ( int )(6.7) = 13 Java Programming: From Problem Analysis to Program Design, 3e
  • 23. The class String Used to manipulate strings String Sequence of zero or more characters Enclosed in double quotation marks Null or empty strings have no characters Numeric strings consist of integers or decimal numbers Length is the number of characters in a string Java Programming: From Problem Analysis to Program Design, 3e
  • 24. Input Named constant Cannot be changed during program execution Declared by using the reserved word final Initialized when it is declared Example 2-11 final double CENTIMETERS_PER_INCH = 2.54; final int NO_OF_STUDENTS = 20; final char BLANK = ' '; final double PAY_RATE = 15.75; Java Programming: From Problem Analysis to Program Design, 3e
  • 25. Input (continued) Variable (name, value, data type, size) Content may change during program execution Must be declared before it can be used May not be automatically initialized If new value is assigned, old one is destroyed Value can only be changed by an assignment statement or an input (read) statement Example 2-12 double amountDue; int counter; char ch; int num1, num2; Java Programming: From Problem Analysis to Program Design, 3e
  • 26. Input (continued) The Assignment Statement variable = expression; Example 2-13 int num1; int num2; double sale; char first; String str; num1 = 4; num2 = 4 * 5 - 11; sale = 0.02 * 1000; first = 'D'; str = "It is a sunny day."; Java Programming: From Problem Analysis to Program Design, 3e
  • 27. Input (continued) Standard input stream object: System.in Input numeric data to program Separate by blanks, lines, or tabs To read data: Create an input stream object of the class Scanner Use the methods such as next , nextLine , nextInt , and nextDouble Java Programming: From Problem Analysis to Program Design, 3e
  • 28. Input (continued) static Scanner console = new Scanner(System.in); Example 2-16 static Scanner console = new Scanner(System.in); int feet; int inches; Suppose the input is 23 7 feet = console.nextInt(); //Line 1 inches = console.nextInt(); //Line 2 Java Programming: From Problem Analysis to Program Design, 3e
  • 29. Increment and Decrement Operators ++ increments the value of its operand by 1 -- decrements the value of its operand by 1 Syntax Pre-increment: ++variable Post-increment: variable++ Pre-decrement: --variable Post-decrement: variable-- Java Programming: From Problem Analysis to Program Design, 3e
  • 30. Strings and the Operator + Operator + can be used to concatenate two strings or a string and a numeric value or character Example 2-20 String str; int num1; int num2; num1 = 12; num2 = 26; str = "The sum = " + num1 + num2; -After this statement executes, the string assigned to str is: "The sum = 1226"; Java Programming: From Problem Analysis to Program Design, 3e
  • 31. Strings and the Operator + (continued) Consider the following statement: str = "The sum = " + (num1 + num2); In this statement, because of the parentheses, you first evaluate num1 + num2 Because num1 and num2 are both int variables, num1 + num2 = 12 + 26 = 38 After this statement executes, the string assigned to str is: "The sum = 38"; Java Programming: From Problem Analysis to Program Design, 3e
  • 32. Output Standard output object: System.out Methods print println Syntax System.out.print(stringExp); System.out.println(stringExp); System.out.println(); Java Programming: From Problem Analysis to Program Design, 3e
  • 33. Commonly Used Escape Sequences Java Programming: From Problem Analysis to Program Design, 3e
  • 34. Packages, Classes, Methods, and the import Statement Package: collection of related classes Class: consists of methods Method: designed to accomplish a specific task Java Programming: From Problem Analysis to Program Design, 3e
  • 35. import Statement Used to import the components of a package into a program Reserved word import java.io.*; Imports the (components of the) package java.io into the program Primitive data types and the class String Part of the Java language Don’t need to be imported Java Programming: From Problem Analysis to Program Design, 3e
  • 36. Creating a Java Application Program Syntax of a class Syntax of the main method Java Programming: From Problem Analysis to Program Design, 3e
  • 37. Programming Style and Form Know common syntax errors and rules Use blanks appropriately Semicolon: statement terminator Important to have well-documented code Good practice to follow traditional rules for naming identifiers Java Programming: From Problem Analysis to Program Design, 3e
  • 38. More on Assignment Statements variable = variable * (expression); is equivalent to variable *= expression; Similarly, variable = variable + (expression); is equivalent to variable += expression; Java Programming: From Problem Analysis to Program Design, 3e
  • 39. Programming Examples Convert Length Program Input: length in feet and inches Output: equivalent length in centimeters Make Change Program Input: change in cents Output: equivalent change in half-dollars, quarters, dimes, nickels, and pennies Java Programming: From Problem Analysis to Program Design, 3e
  • 40. Chapter Summary Basic Elements of a Java program include: The main method Reserved words Special symbols Identifiers Data types Expressions Input Output Statements Java Programming: From Problem Analysis to Program Design, 3e
  • 41. Chapter Summary (continued) To create a Java application, it is important to understand: Syntax rules Semantic rules How to manipulate strings and numbers How to declare variables and named constants How to receive input and display output Good programming style and form Java Programming: From Problem Analysis to Program Design, 3e