0% found this document useful (0 votes)
17 views5 pages

Assugn 2

Uploaded by

tonnybraxton64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Assugn 2

Uploaded by

tonnybraxton64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

import java.util.Scanner;

public class SquareCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the side length of the square: ");

double sideLength = scanner.nextDouble();

// Calculate area (sideLength * sideLength)

double area = sideLength * sideLength;

// Calculate circumference (4 * sideLength)

double circumference = 4 * sideLength;

System.out.println("Area of the square: " + area);

System.out.println("Circumference of the square: " + circumference);

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.

a. Comma Operator (,)

 The comma operator evaluates multiple expressions and returns the value of the last
expression.

 It is often used in for loops or to combine expressions.

 Example:

Java

int x = 10, y = 20, z;

z = (x++, y++, x + y); // z will be 30 (x + y)

AI-generated code. Review and use carefully.

b. Increment/Decrement Operators (++ and --)

 These operators increase (++) or decrease (--) the value of a variable by 1.

 They can be used as prefix or postfix operators.

 Example:

Java

int count = 5;

int incremented = ++count; // incremented is 6

int decremented = count--; // decremented is 6 (count becomes 5 after this line)

AI-generated code. Review and use carefully.

c. Assignment Operator (=)

 The assignment operator assigns a value to a variable.

 Example:

Java

int age = 25;

AI-generated code. Review and use carefully.

d. Relational Operators (<, >, <=, >=, ==, !=)


 These operators compare two values and return a boolean result (true or false).

 Example:

Java

int a = 10, b = 20;

boolean isGreater = (a > b); // false

boolean isEqual = (a == b); // false

AI-generated code. Review and use carefully.

e. Logical Operators (&&, ||, !)

 Logical operators combine boolean expressions.

 && (AND) returns true if both expressions are true.

 || (OR) returns true if at least one expression is true.

 ! (NOT) negates the value of an expression.

 Example:

Java

boolean hasPermission = true;

boolean isLoggedIn = false;

boolean canAccess = hasPermission && isLoggedIn; // false

AI-generated code. Review and use carefully.

f. Conditional Operator (Ternary Operator) (? :)

 The conditional operator evaluates a condition and returns one of two values based on the
result.

 Syntax: condition ? value_if_true : value_if_false

 Example:

Java

int score = 75;

String result = (score >= 60) ? "Pass" : "Fail"; // "Pass"

4.

1. Local Variables:

o Local variables are declared within a specific block of code (such as a method, loop,
or conditional statement).

o They are only accessible within that block.


o Example:

Java

public void calculateSum() {

int a = 10; // local variable

int b = 20;

int sum = a + b;

System.out.println("Sum: " + sum);

AI-generated code. Review and use carefully.

2. Method Parameters:

o Method parameters are also local variables.

o They are declared in the method signature and represent values passed to the
method.

o Example:

Java

public void greet(String name) {

System.out.println("Hello, " + name + "!");

AI-generated code. Review and use carefully. Instance Variables (Fields):

o Instance variables (also known as fields) are declared within a class but outside any
method.

o They hold values specific to an instance (object) of the class.

o Example:

Java

public class Student {

String name; // instance variable

int age;

AI-generated code. Review and use carefully

3. Class Variables (Static Variables):

o Class variables (static variables) are shared across all instances of a class.

o They are declared using the static keyword.


o Example:

Java

public class MathUtils {

public static final double PI = 3.14; // class variable

AI-generated code. Review and use carefully.

4. Block Scope:

o Variables declared within a block (such as a loop or conditional statement) have


block scope.

o They are accessible only within that block.

o Example:

Java

for (int i = 0; i < 5; i++) {

System.out.println(i); // i has block scope

AI-generated code. Review and use carefully.

5. Nested Scopes:

o Scopes can be nested (one inside another).

o Inner scopes can access variables from outer scopes, but not vice versa.

o Example:

Java

int x = 10; // outer scope

if (x > 5) {

int y = 20; // inner scope

System.out.println(y);

AI-generated code. Review and use carefully.

You might also like