Chapter 2 Basic Elements of Java
Chapter 2 Basic Elements of Java
Java Programming:
From Problem Analysis to Program Design,
Second Edition
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, Second Edition
Chapter Objectives
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, Second Edition
Chapter Objectives
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, Second Edition
Introduction
Computer program: A sequence of statements designed to accomplish a task.
Programming: The process of planning and creating a program.
Special Symbols
Word Symbols
int float double char
Java Identifiers
Names of things. Consists of:
Letters
Digits
The underscore character (_) The dollar sign ($)
Illegal Identifiers
10
Data Types
A set of values together with a set of operations.
11
12
Boolean:
True False
Java Programming: From Problem Analysis to Program Design, Second Edition
13
14
15
* multiplication
/ division % mod (modulus)
Order of 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.
17
Expressions
Integral expressions
Floating-point or decimal expressions Mixed expressions
18
Integral Expressions
All operands are integers. Examples:
2 + 3 * 5 3 + x y / 7 x + 2 * (y z) + 18
19
Floating-Point Expressions
All operands are floating-point numbers. Examples: 12.8 * 17.5 34.50 x * 10.5 + y - 16.2
20
Mixed Expressions
Operands of different types. Examples: 2 + 3.5 6 / 4 + 3.9 Integer operands yield an integer result; floatingpoint 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, Second Edition
21
22
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, Second Edition
24
Input
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.
25
Input
The Assignment Statement
variable = expression;
Example 2-13
int i, j; double sale; char first; String str; i = 4; j = 4 * 5 - 11; sale = 0.02 * 1000; first = 'D'; str = "It is a sunny day.";
Java Programming: From Problem Analysis to Program Design, Second Edition
26
Input
Standard input stream object is System.in.
To read data:
1. Create an input stream object of the class Scanner.
Input
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(); inches = console.nextInt(); //Line 1 //Line 2
28
Pre-decrement: --variable
Post-decrement: variable-Java Programming: From Problem Analysis to Program Design, Second Edition
29
30
31
Output
Standard output object is System.out. Methods:
print println
Syntax:
System.out.print(stringExp); System.out.println(stringExp); System.out.println();
32
33
34
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.
35
36
37
is equivalent to:
variable *= expression; Similarly,
38
Programming Examples
Convert Length program:
Input: Length in feet and inches. Output: Equivalent length in centimeters.
39
Chapter Summary
Basic elements of a Java program include:
The main method Reserved words Special symbols Identifiers Data types Expressions Input Output Statements
40
Chapter Summary
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.
41