Chapter 2 Basic Elements of Java Programming From
Chapter 2: Basic Elements of Java Programming: From Problem Analysis to Program Design, Second Edition Java Programming: From Problem Analysis to Program Design,
Chapter Objectives s Become familiar with the basic components of a Java program, including methods, special symbols, and identifiers. s Explore primitive data types. s Discover how to use arithmetic operators. s Examine how a program evaluates arithmetic expressions. s Explore how mixed expressions are evaluated. Java Programming: From Problem Analysis to Program Design, Second Edition 2
Chapter Objectives s Learn about type casting. s Become familiar with the String type. s Learn what an assignment statement is and what it does. s Discover how to input data into memory by using input statements. s Become familiar with the use of increment and decrement operators. Java Programming: From Problem Analysis to Program Design, Second Edition 3
Chapter Objectives s Examine ways to output results using output statements. s Learn how to import packages and why they are necessary. s Discover how to create a Java application program. s Explore how to properly structure a program, including using comments to document a program. Java Programming: From Problem Analysis to Program Design, Second Edition 4
Introduction s Computer program: A sequence of statements designed to accomplish a task. s Programming: The process of planning and creating a program. Java Programming: From Problem Analysis to Program Design, Second Edition 5
The Basics of a Java Program s Java program: A collection of classes. s There is a main method in every Java application program. s Token: The smallest individual unit of a program. Java Programming: From Problem Analysis to Program Design, Second Edition 6
Special Symbols Java Programming: From Problem Analysis to Program Design, Second Edition 7
Word Symbols s s int float double char s s s void public static throws return Java Programming: From Problem Analysis to Program Design, Second Edition 8
Java Identifiers s Names of things. s Consists of: s Letters s Digits s The underscore character (_) s The dollar sign ($) s Must begin with a letter, underscore, or the dollar sign. Java Programming: From Problem Analysis to Program Design, Second Edition 9
Illegal Identifiers Java Programming: From Problem Analysis to Program Design, Second Edition 10
Data Types A set of values together with a set of operations. Java Programming: From Problem Analysis to Program Design, Second Edition 11
Primitive Data Types Java Programming: From Problem Analysis to Program Design, Second Edition 12
Primitive Data Types s Floating-point data types: s Float: Precision = 6 or 7 s Double: Precision = 15 s Boolean: s True s False Java Programming: From Problem Analysis to Program Design, Second Edition 13
Integral Data Types Java Programming: From Problem Analysis to Program Design, Second Edition 14
Values and Memory Allocation for Integral Data Types Java Programming: From Problem Analysis to Program Design, Second Edition 15
Arithmetic Operators and Operator Precedence s Five arithmetic operators: s + addition s - subtraction s * multiplication s / division s % mod (modulus) s Unary operator: An operator that has one operand. s Binary operator: An operator that has two operands. Java Programming: From Problem Analysis to Program Design, Second Edition 16
Order of Precedence * + s s / - % (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, Second Edition 17
Expressions s Integral expressions s Floating-point or decimal expressions s Mixed expressions Java Programming: From Problem Analysis to Program Design, Second Edition 18
Integral Expressions s All operands are integers. s Examples: 2 + 3 * 5 3 + x – y / 7 x + 2 * (y – z) + 18 Java Programming: From Problem Analysis to Program Design, Second Edition 19
Floating-Point Expressions s All operands are floating-point numbers. s Examples: 12. 8 * 17. 5 – 34. 50 x * 10. 5 + y - 16. 2 Java Programming: From Problem Analysis to Program Design, Second Edition 20
Mixed Expressions s Operands of different types. s Examples: 2 + 3. 5 6 / 4 + 3. 9 s Integer operands yield an integer result; floatingpoint numbers yield floating-point results. s If both types of operands are present, the result is a floating-point number. s Precedence rules are followed. Java Programming: From Problem Analysis to Program Design, Second Edition 21
Type Conversion (Casting) s Used to avoid implicit type coercion. s Syntax: (data. Type. Name) expression s Expression evaluated first, then type converted to: data. Type. Name s Examples: (int)(7. 9 + 6. 7) = 14 (int)(7. 9) + (int)(6. 7) = 13 Java Programming: From Problem Analysis to Program Design, Second Edition 22
The class String s Used to manipulate strings. s String: s s 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. s Length is the number of characters in a string. Java Programming: From Problem Analysis to Program Design, Second Edition 23
Input Named constant s s s 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) s s s 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 amount. Due; int counter; char ch; int x, y; Java Programming: From Problem Analysis to Program Design, Second Edition 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 s Standard input stream object is System. in. s Input numeric data to program. s s 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, next. Line, next. Int, and next. Double. Java Programming: From Problem Analysis to Program Design, Second Edition 27
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. next. Int(); inches = console. next. Int(); //Line 1 //Line 2 Java Programming: From Problem Analysis to Program Design, Second Edition 28
Increment and Decrement Operators s ++ increments the value of its operand by 1. s -- decrements the value of its operand by 1. s Syntax: Pre-increment: ++variable Post-increment: variable++ Pre-decrement: --variable Post-decrement: variable-Java Programming: From Problem Analysis to Program Design, Second Edition 29
Strings and the Operator + s Operator + can be used to concatenate two strings, or a string and a numeric value or character. Example 2 -20 String str; int num 1, num 2; num 1 = 12; num 2 = 26; str = "The sum = " + num 1 + num 2; After this statement executes, the string assigned to str is: "The sum = 1226"; Java Programming: From Problem Analysis to Program Design, Second Edition 30
Strings and the Operator + s Example 2 -20 String str; int num 1, num 2; num 1 = 12; num 2 = 26; str = "The sum = " + num 1 + num 2; After this statement executes, the string assigned to str is: "The sum = 1226"; s Consider the following statement: str = "The sum = " + (num 1 + num 2); s In this statement, because of the parentheses, you first evaluate num 1 + num 2. Because num 1 and num 2 are both int variables, num 1 + num 2 = 12 + 26 = 38. After this statement executes, the string assigned to str is: "The sum = 38"; Java Programming: From Problem Analysis to Program Design, Second Edition 31
Output s Standard output object is System. out. s Methods: println s Syntax: System. out. print(string. Exp); System. out. println(); Java Programming: From Problem Analysis to Program Design, Second Edition 32
Commonly Used Escape Sequences Java Programming: From Problem Analysis to Program Design, Second Edition 33
Packages, Classes, Methods, and the import Statement s Package: A collection of related classes. s Class: Consists of methods. s Method: Designed to accomplish a specific task. Java Programming: From Problem Analysis to Program Design, Second Edition 34
import Statement s Used to import the components of a package into a program. s Reserved word. s import java. io. *; Imports the (components of the) package java. io into the program. s Primitive data types and the class String: s Part of the Java language. s Don’t need to be imported. Java Programming: From Problem Analysis to Program Design, Second Edition 35
Creating a Java Application Program s Syntax of a class: s Syntax of the main method: Java Programming: From Problem Analysis to Program Design, Second Edition 36
Programming Style and Form s Know common syntax errors and rules. s Use blanks appropriately. s Use a semicolon as a statement terminator. s Important to have well-documented code. s Good practice to follow traditional rules for naming identifiers. Java Programming: From Problem Analysis to Program Design, Second Edition 37
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, Second Edition 38
Programming Examples s Convert Length program: s Input: Length in feet and inches. s Output: Equivalent length in centimeters. s Make Change program: s Input: Change in cents. s Output: Equivalent change in half-dollars, quarters, dimes, nickels, and pennies. Java Programming: From Problem Analysis to Program Design, Second Edition 39
Chapter Summary s Basic elements of a Java program include: s s s s s The main method Reserved words Special symbols Identifiers Data types Expressions Input Output Statements Java Programming: From Problem Analysis to Program Design, Second Edition 40
Chapter Summary s To create a Java application, it is important to understand: s Syntax rules. s Semantic rules. s How to manipulate strings and numbers. s How to declare variables and named constants. s How to receive input and display output. s Good programming style and form. Java Programming: From Problem Analysis to Program Design, Second Edition 41
- Slides: 41