ELEMENTARY PROGRAMMING
INTRODUCING PROGRAMMING WITH AN EXAMPLE
Computing the Area of a Circle
.
TRACE A PROGRAM EXECUTION
public class ComputeArea {
/** Main method */
public static void main(String[] args) {
double radius;
double area;
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.print("The area for the circle of radius “);
System.out.println(radius + " is " + area);
}
}
IDENTIFIERS
a sequence of characters that consist of letters,
digits, underscores (_), and dollar signs ($).
must start with a letter, an underscore (_), or a
dollar sign ($). It cannot start with a digit.
cannot be a reserved word.
(REFER to list of reserved words).
cannot be true, false, or
null.
can be of any length.
VARIABLES
a data item that may take on more than one value
during the runtime of a program
VARIABLES
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ + area
+ " for radius "+radius);
// Compute the second area
radius = 2.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 as
// double variable;
char a; // Declare a to be a
// character variable;
ASSIGNMENT STATEMENTS
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;
DECLARING AND INITIALIZING
int x = 1;
double d = 1.4;
NAMED CONSTANTS
final datatype CONSTANTNAME = VALUE;
final double PI = 3.14159;
final int SIZE = 3;
NAMING CONVENTIONS
Choose meaningful and descriptive names.
Variables and method names:
Use lowercase. If the name consists of several words,
concatenate all in one, use lowercase for the first
word, and capitalize the first letter of each subsequent
word in the name.
For example,
Names of variables radius and area,
Names of method computeArea.
NAMING CONVENTIONS, CONT.
Class names:
Capitalize the first letter of each word in the name.
For example, the class name ComputeArea.
Constants:
Capitalize all letters in constants, and use underscores to
connect words.
For example, the constant PI and MAX_VALUE
NUMERICAL DATA TYPES
Name Range Storage Size
byte –27 to 27 – 1 (-128 to 127) 8-bit signed
short –215 to 215 – 1 (-32768 to 32767) 16-bit signed
int –231 to 231 – 1 (-2147483648 to 2147483647) 32-bit signed
long –263 to 263 – 1 64-bit signed
(i.e., -9223372036854775808 to 9223372036854775807)
float Negative range: 32-bit IEEE 754
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to -4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308
READING INPUT FROM THE CONSOLE
1. Create a Scanner object
Scanner input = new Scanner(System.in);
2. Use the method nextDouble() to obtain to a double
value.
For example,
System.out.print("Enter a double value: ");
Scanner input = new Scanner(System.in);
double d = input.nextDouble();
READING NUMBERS FROM THE KEYBOARD
Scanner input = new Scanner(System.in);
int value = input.nextInt();
Method Description
nextByte() reads an integer of the byte type.
nextShort() reads an integer of the short type.
nextInt() reads an integer of the int type.
nextLong() reads an integer of the long type.
nextFloat() reads a number of the float type.
nextDouble() reads a number of the double type.
NUMERIC OPERATORS
Name Meaning Example Result
+ Addition 34 + 1 35
- Subtraction 34.0 – 0.1 33.9
* Multiplication 300 * 30 9000
/ Division 1.0 / 2.0 0.5
% Remainder 20 % 3 2
INTEGER DIVISION
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
5 % 2 yields 1 (the remainder of the division)
REMAINDER OPERATOR %
very useful in programming.
For example,
number % 2 is always 0 – even number
number % 2 is always 1 – odd number.
Suppose today is Saturday. What day is in 10 days? :
Saturday is the 6th day in a week
A week has 7 days
(6 + 10) % 7 is 2
The 2nd day in a week is Tuesday
After 10 days
EXPONENT OPERATIONS
Using Math.pow (x, y) to calculate xy
System.out.println(Math.pow(2, 3));
// Displays 8.0
System.out.println(Math.pow(4, 0.5));
// Displays 2.0
System.out.println(Math.pow(2.5, 2));
// Displays 6.25
System.out.println(Math.pow(2.5, -2));
// Displays 0.16
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;
INTEGER LITERALS
An integer literal can be assigned to an integer variable as long as it
can fit into the variable.
A compilation error would occur if the literal were too large for the
variable to hold.
For example, the statement byte b = 1000 would cause a compilation
error, because 1000 cannot be stored in a variable of the byte type.
An integer literal is assumed to be of the int type, whose value is
between -231 (-2147483648) to 231–1 (2147483647). To denote an
integer literal of the long type, append it with the letter L or l. L is
preferred because l (lowercase L) can easily be confused with 1 (the
digit one).
FLOATING-POINT LITERALS
Written with a decimal point.
By default, a floating-point literal is treated as a double
type value.
For example, 5.0 is considered a double value, not a
float value.
Appending with F or f make a number a float and
appending the letter d or D make a number a double by
For example,
100.2f or 100.2F for a float number,
100.2d or 100.2D for a double number
AUGMENTED ASSIGNMENT OPERATORS
INCREMENT & DECREMENT OPERATORS
USING INCREMENT & DECREMENT OPERATOR
int i = 10; Same effect as
int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;
int i = 10; Same effect as
int newNum = 10 * (++i); i = i + 1;
int newNum = 10 * i;
NUMERIC TYPE CONVERSION
Consider the following statements:
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
CONVERSION RULES
1. If one of the operands is double, the other is
converted into double.
2. Otherwise, if one of the operands is float, the
other is converted into float.
3. Otherwise, if one of the operands is long, the
other is converted into long.
4. Otherwise, both operands are converted into int.
TYPE CASTING
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is
truncated)
What is wrong? int x = 5 / 2.0;
END OF SLIDES