Mrs. Rojalin Mallick Lecturer, CSA, CET
Mrs. Rojalin Mallick Lecturer, CSA, CET
ROJALIN MALLICK
Lecturer, CSA,CET
1
Computing the Area of a Circle
This program computes the area of the
circle.
2
animation
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}
3
animation
// Assign a radius
radius = 20;
allocate memory
// Compute area for area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}
4
animation
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}
5
animation
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}
6
animation
// Compute area
area = radius * radius * 3.14159; print a message to the
console
// Display results
System.out.println("The area for the circle of
radius " +
radius + " is " + area);
}
}
7
An identifier is a sequence of characters that
consist of letters, digits, underscores (_), and
dollar signs ($).
An identifier must start with a letter, an
underscore (_), or a dollar sign ($). It cannot
start with a digit.
An identifier cannot be a reserved word.
An identifier cannot be true, false, or
null.
An identifier can be of any length.
8
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ +
area + " for radius "+radius);
9
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;
10
x = 1; // Assign 1 to x;
11
int x = 1;
double d = 1.4;
12
1. Create a Scanner object
Scanner input = new Scanner(System.in);
13
java.util.* ; // Implicit import
No performance difference
14
Scanner input = new Scanner(System.in);
int value = input.nextInt();
Method Description
15
Name Meaning Example Result
+ Addition 34 + 1 35
% Remainder 20 % 3 2
16
+, -, *, /, and %
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
17
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
18
The double type values are more accurate than the
float type values. For example,
16 digits
19
Though Java has its own way to evaluate an
expression behind the scene, the result of a Java
expression and its corresponding arithmetic
expression are the same. Therefore, you can safely
apply the arithmetic rule for evaluating a Java
expression. 3 + 4 * 4 + 5 * (4 + 3) - 1
(1) inside parentheses first
3 + 4 * 4 + 5 * 7 – 1
(2) multiplication
3 + 16 + 5 * 7 – 1
(3) multiplication
3 + 16 + 35 – 1
(4) addition
19 + 35 – 1
(5) addition
54 - 1
(6) subtraction
53
20
Write a program that converts a Fahrenheit degree
to Celsius using the formula:
21
22
23
int i = 10; Same effect as
int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;
24