02 Slide
02 Slide
Chapter 2 Elementary
Programming
2
Introducing Programming
with an Example
Listing 2.1 Computing the
Area of a Circle
This program computes the
area of the circle.
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle
of radius " +
radius + " is " + area);
}
}
animation 4
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle
of radius " +
radius + " is " + area);
}
}
animation 6
// Compute area
area = radius * radius * 3.14159;
print a message to
// Display results the console
System.out.println("The area for the circle
of radius " +
radius + " is " + area);
}
}
8
Identifier
• sequence of characters that consist of
letters, digits, underscores (_), and dollar
signs ($).
• cannot start with a digit.
▫ An identifier cannot be a reserved word. (See
Appendix A, “Java Keywords,” for a list of
reserved words).
• An identifier cannot be true, false, or
null.
• An identifier can be of any length.
9
Variables
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ +
area + " for radius "+radius);
Declaring Variables
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
11
Assignment Statements
x = 1; // Assign 1 to x;
Constants
final datatype CONSTANTNAME = VALUE;
+ Addition 34 + 1 35
% Remainder 20 % 3 2
16
Integer Division
+, -, *, /, and %
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
2.5 will be truncated (not rounded) if
assigned to an integer
DisplayTime Run
18
Number Literals
A literal is a constant value that
appears directly in the program. For
example, 34, 1,000,000, and 5.0 are
literals in the following statements:
int i = 34;
long x = 1000000;
double d = 5.0;
19
Arithmetic Expressions
is translated to
Increment / Decrement
++var preincrement
var++ postincrement
--var predecrement
var-- postdecrement
Conversion Rules
When performing a binary operation
involving two operands of different types,
Java automatically converts the operand
based on the following rules:
Type Conversion
• Widening: convert numeric type of
smaller range to type of larger range
▫ Done implicitly
• Narrowing: convert numeric type of
larger range to type of smaller range
▫ May result in overflow
▫ Explicit casts
25
Type Casting
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (truncate decimal)
What is wrong? int x = 5 / 2.0;
range increases
operator)
// Three strings are concatenated
String message = "Welcome " + "to " + "Java";
Appropriate Comments
Naming Conventions
• Choose meaningful and descriptive
names.
• Variables and method names:
▫ Lowercase for first word
▫ Camelcase if several words
• Class names:
▫ Capitalize first letter of each word
• Constants:
▫ Capitalize all letters, use underscores to
connect words.
42
Block Styles
Chose next-line or end-of-line style for braces.
End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}
43
Programming Errors
• Syntax Errors
▫ Detected by the compiler
• Runtime Errors
▫ Causes the program to abort
• Logic Errors
▫ Produces incorrect result
44
Syntax Errors
public class ShowSyntaxErrors {
public static void main(String[] args) {
i = 30;
System.out.println(i + 4);
}
}
45
Runtime Errors
public class ShowRuntimeErrors {
public static void main(String[] args) {
int i = 1 / 0;
}
}
46
Logic Errors
public class ShowLogicErrors {
// Determine if a number is between 1 and 100 inclusively
public static void main(String[] args) {
// Prompt the user to enter a number
String input = JOptionPane.showInputDialog(null,
"Please enter an integer:",
"ShowLogicErrors", JOptionPane.QUESTION_MESSAGE);
int number = Integer.parseInt(input);
System.exit(0);
}
}
47
Debugging
Logic errors are called bugs.
Debugging: process of finding and correcting.