JAVA PROGRAMMING: FROM PROBLEM
ANALYSIS TO PROGRAM DESIGN, 4E 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
2
DESIGN, 4E
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
3
DESIGN, 4E
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
4
DESIGN, 4E
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
5
DESIGN, 4E
A JAVA PROGRAM
Sample Run:
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
6
DESIGN, 4E
THE BASICS OF A JAVA PROGRAM Keywords
Java program: collection of classes
There is a main method in every Java Literals Separators
application program
In a Java program, all characters are Tokens
grouped into symbols called tokens
Programming language: A set of rules,
symbols, and special words used to
Identifiers Operators
construct
Java Programming: From Problem Analysis to Program Design, 4e 7
COMMENTS
• used to identify the authors of the program or give the
date when the program is written or modified.
• give a brief explanation of the program
• explain the meaning of key statements in a program.
• it begins with //
Java Programming: From Problem Analysis to Program Design, 4e 8
SPECIAL SYMBOLS
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
9
DESIGN, 4E
RESERVED WORDS (KEYWORDS)
void
int
public
float
static
double
throws
char
return
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
10
DESIGN, 4E
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
11
DESIGN, 4E
ILLEGAL IDENTIFIERS
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
12
DESIGN, 4E
DATA TYPES
Data type: set of values together with a set of operations
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
13
DESIGN, 4E
PRIMITIVE DATA TYPES
•Integral - which is a data type that deals with integers, or
numbers without a decimal part (and characters)
•Floating-point - which is a data type that deals with decimal
numbers
•Boolean - which is a data type that deals with logical values
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
14
DESIGN, 4E
INTEGRAL DATA TYPES
•char
•byte
•short
•int
•long
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
15
DESIGN, 4E
VALUES AND MEMORY ALLOCATION FOR INTEGRAL
DATA TYPES
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
16
DESIGN, 4E
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
17
DESIGN, 4E
LITERALS (CONSTANTS)
Integer literals, integer constants, or integers: 23 and -67
Floating-point literals, floating-point constants, floating-point
numbers: 12.34 and 25.60
Character literals, character constants, or characters: 'a' and '5'
Java Programming: From Problem Analysis to Program Design, 4e 18
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
19
DESIGN, 4E
ORDER OF PRECEDENCE
1. * / % (same precedence)
2. + - (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
20
DESIGN, 4E
USING THE ORDER OF PRECEDENCE RULES,
3*7-6+2*5/4+6
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
21
DESIGN, 3E
EXPRESSIONS
Integral expressions
Floating-point or decimal expressions
Mixed expressions
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
22
DESIGN, 4E
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
23
DESIGN, 4E
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
24
DESIGN, 4E
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
25
DESIGN, 4E
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)
(int)(7.9) + (int)(6.7)
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
26
DESIGN, 4E
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
27
DESIGN, 4E
STRINGS AND THE OPERATOR +
Operator + can be used to concatenate two strings
or a string and a numeric value or character
Example 2-10
"The sum = " + 12 + 26
-After this statement executes, the string assigned to str is:
"The sum = 1226";
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
28
DESIGN, 4E
STRINGS AND THE OPERATOR + (CONTINUED)
Consider the following statement:
"The sum = " + (12 + 26)
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
29
DESIGN, 4E
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
30
DESIGN, 4E
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
31
DESIGN, 4E
INPUT (CONTINUED)
The assignment statement
variable = expression;
num1 = 4;
Example 2-13
num2 = 4 * 5 - 11;
int num1; sale = 0.02 * 1000;
int num2; first = 'D';
double sale; str = "It is a sunny day.";
char first;
String str;
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
32
DESIGN, 4E
INPUT (CONTINUED)
Example 2-14
1. num1 = 18;
2. num1 = num1 + 27;
3. num2 = num1;
4. num3 = num2 / 5;
5. num3 = num3 / 4;
Java Programming: From Problem Analysis to Program Design, 4e 33
INPUT (CONTINUED)
Java Programming: From Problem Analysis to Program Design, 4e 34
INPUT (CONTINUED)
Java Programming: From Problem Analysis to Program Design, 4e 35
INPUT (CONTINUED)
Standard input stream object: System.in
Input numeric data to program
Separate by blanks, lines, or tabs
To read data:
1. Create an input stream object of the class Scanner
2. Use the methods such as next, nextLine, nextInt, and
nextDouble
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
36
DESIGN, 4E
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
37
DESIGN, 4E
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
38
DESIGN, 4E
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
39
DESIGN, 4E
COMMONLY USED ESCAPE SEQUENCES
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
40
DESIGN, 4E
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
41
DESIGN, 4E
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
42
DESIGN, 4E
CREATING A JAVA APPLICATION PROGRAM
Syntax of a class
Syntax of the main method
JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM
43
DESIGN, 4E
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
44
DESIGN, 4E
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
45
DESIGN, 4E
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
46
DESIGN, 4E
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
47
DESIGN, 4E
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
48
DESIGN, 4E