Assugn 2
Assugn 2
import java.util.Scanner;
scanner.close();
2. import java.util.Scanner;
3.
4. public class CircleCalculator {
5. public static void main(String[] args) {
6. Scanner scanner = new Scanner(System.in);
7.
8. System.out.print("Enter the radius of the circle: ");
9. double radius = scanner.nextDouble();
10.
11. // Calculate area (π * radius^2)
12. double area = Math.PI * Math.pow(radius, 2);
13.
14. // Calculate circumference (2 * π * radius)
15. double circumference = 2 * Math.PI * radius;
16.
17. // Display results with two decimal places
18. System.out.printf("Area of the circle: %.2f%n", area);
19. System.out.printf("Circumference of the circle: %.2f%n", circumference);
20.
21. scanner.close();
22. }
23. }
3.
The comma operator evaluates multiple expressions and returns the value of the last
expression.
Example:
Java
Example:
Java
int count = 5;
Example:
Java
Example:
Java
Example:
Java
The conditional operator evaluates a condition and returns one of two values based on the
result.
Example:
Java
4.
1. Local Variables:
o Local variables are declared within a specific block of code (such as a method, loop,
or conditional statement).
Java
int b = 20;
int sum = a + b;
2. Method Parameters:
o They are declared in the method signature and represent values passed to the
method.
o Example:
Java
o Instance variables (also known as fields) are declared within a class but outside any
method.
o Example:
Java
int age;
o Class variables (static variables) are shared across all instances of a class.
Java
4. Block Scope:
o Example:
Java
5. Nested Scopes:
o Inner scopes can access variables from outer scopes, but not vice versa.
o Example:
Java
if (x > 5) {
System.out.println(y);