Ameya 1
Ameya 1
Grade :
Class/Roll No.:
-
Name : Ameya
Nikhil Kalgutkar
Kadam
D6ADA/26 7
Theory: Variable: Variables are the data containers that save the data values during
Java program execution. Every Variable in Java is assigned a data type that
designates the type and quantity of value it can hold.
Types of variables:
1. Local Variables: A variable defined within a block or method or constructor is called a local
variable. These variables are created when the block is entered, or the function is called and
destroyed after exiting from the block or when the call returns from the function. The scope of
these variables exists only within the block in which the variables are declared, i.e., we can
access these variables only within that block. Initialization of the local variable is mandatory
before using it in the defined scope.
2. Instance Variables: Instance variables are non-static variables and are declared in a class
outside of any method, constructor, or block. As instance variables are declared in a class,
these variables are created when an object of the class is created and destroyed when the
object is destroyed. Unlike local variables, we may use access specifies for instance
variables. If we do not specify any access specifies, then the default access specified will be
used. Initialization of an instance variable is not mandatory. Its default value is dependent on
the data type of variable. For String it is null, for float it is 0.0f, for int it is 0, for Wrapper
classes like Integer it is null, etc. Instance variables can be accessed only by creating
objects. We initialize instance variables using constructors while creating an object. We can
also use instance blocks to initialize the instance variables.
3. Static Variables: Static variables are also known as class variables. These variables are
declared similarly to instance variables. The difference is that static variables are declared
using the static keyword within a class outside of any method, constructor, or block. Unlike
instance variables, we can only have one copy of a static variable per class, irrespective of
how many objects we create. Static variables are created at the start of program execution
and destroyed automatically when execution ends. Initialization of a static variable is not
mandatory. Its default value is dependent on the data type of variable. For String it is null, for
float it is 0.0f, for int it is 0, for Wrapper classes like Integer it is null, etc. If we access a static
variable like an instance variable (through an object), the compiler will show a warning
message, which won’t halt the program. The compiler will replace the object name with the
class name automatically. If we access a static variable without the class name, the compiler
will automatically append the class name. But for accessing the static variable of a different
class, we must mention the class name as 2 different classes might have a static variable
with the same name. Static variables cannot be declared locally inside an instance method.
Static blocks can be used to initialize static variables.
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("The roots of the quadratic equation are: " + root1 + " and " +
root2);
} else {
System.out.println("The quadratic equation has complex roots.");
}
}
}
Artificial
Output Screenshots :
Algorithm:
1. Start the program.
2. Display a user-friendly message asking the user to enter a positive integer.
3. Read the input number 'num' from the user.
4. Check if the 'num' is less than 2. If yes, display "The number must be greater than 1 to
be considered a prime number" and end the program.
5. Initialize a boolean variable 'isPrime' as true.
6. For each integer 'i' from 2 to the square root of 'num' (inclusive): a. If 'num' is divisible by
'i', set 'isPrime' as false and break the loop.
7. If 'isPrime' is true, display "The entered number is a prime number."
8. If 'isPrime' is false, display "The entered number is not a prime number."
9. End the program.
Program :
import java.util.Scanner;
if (isPrime) {
System.out.println("The entered number is a prime number.");
} else {
System.out.println("The entered number is not a prime number.");
}
}
}
}
Artificial
Conclusion :
The Java program for the Prime Number Checker successfully determines whether the entered
number is a prime number or not. The program uses a simple primality test, checking if the
number has any divisors other than 1 and itself.
Artificial
• *: Multiplication
• / : Division
• % : Modulo
• + : Addition
• -: Subtraction
2. Unary Operators
Unary operators need only one operand. They are used to increment, decrement, or negate a
value.
3. Assignment Operator
Assignment operator is used to assign a value to any variable. It has right-to-left associativity, i.e.
value given on the right-hand side of the operator is assigned to the variable on the left, and
therefore right-hand side value must be declared before using it or should be a constant.
• +=, for adding the left operand with the right operand and then assigning it to the variable
on the left.
• -=, for subtracting the right operand from the left operand and then assigning it to the
variable on the left.
• *=, for multiplying the left operand with the right operand and then assigning it to the
variable on the left.
Artificial
4. Relational Operators
These operators are used to check for relations like equality, greater than, and less than. They
return boolean results after the comparison and are extensively used in looping statements as
well as conditional if-else statements.
• ==, Equal to returns true if the left-hand side is equal to the right-hand side.
• !=, Not Equal to returns true if the left-hand side is not equal to the right-hand side.
• <, less than: returns true if the left-hand side is less than the right-hand side.
• <=, less than or equal to returns true if the left-hand side is less than or equal to the right-
hand side.
• >, Greater than: returns true if the left-hand side is greater than the right-hand side.
• >=, Greater than or equal to returns true if the left-hand side is greater than or equal to the
right-hand side.
5. Logical Operators
These operators are used to perform “logical AND” and “logical OR” operations, i.e., a function
similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second
condition is not evaluated if the first one is false, i.e., it has a short-circuiting effect. Used
extensively to test for several conditions for making a decision. Java also has “Logical NOT”,
which returns true when the condition is false and vice-versa
• &&, Logical AND: returns true when both conditions are true.
• ||, Logical OR: returns true if at least one condition is true.
• !, Logical NOT: returns true when a condition is false and vice-versa
6. Ternary operator
The ternary operator is a shorthand version of the if-else statement. It has three operands and
hence the name Ternary.
The above statement means that if the condition evaluates to true, then execute the statements
after the ‘?’ else execute the statements after the ‘:’.
Artificial
7. Bitwise Operators
These operators are used to perform the manipulation of individual bits of a number. They can be
used with any of the integer types. They are used when performing update and query operations
of the Binary indexed trees.
• &, Bitwise AND operator: returns bit by bit AND of input values.
• |, Bitwise OR operator: returns bit by bit OR of input values.
• ^, Bitwise XOR operator: returns bit-by-bit XOR of input values.
• ~, Bitwise Complement Operator: This is a unary operator which returns the one’s
complement representation of the input value, i.e., with all bits inverted.
8. Shift Operators
These operators are used to shift the bits of a number left or right, thereby multiplying or dividing
the number by two, respectively. They can be used when we have to multiply or divide a number
by two.
• <<, Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a
result. Similar effect as multiplying the number with some power of two.
• >>, Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids
left as a result. The leftmost bit depends on the sign of the initial number. Similar effect to
dividing the number with some power of two.
Artificial
Program :
import java.util.Scanner;
public class OperatorDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("Menu:");
System.out.println("1. Arithmetic Operators (+, -, *, /, %)");
System.out.println("2. Shift Operators (Left Shift, Right Shift, Unsigned Right Shift)");
System.out.println("3. Bitwise Operators (AND, OR, XOR, NOT)");
System.out.println("4. Logical Operators (AND, OR, NOT)");
System.out.println("5. Relational Operators (>, <, >=, <=, ==, !=)");
System.out.println("6. Exit");
System.out.print("Enter your choice (1-6): ");
choice = scanner.nextInt();
System.out.print(“Enter value ‘a’: “);
double a = scanner.nextDouble();
System.out.print(“Enter value ‘b’: “);
double b = scanner.nextDouble();
switch (choice) {
case1:
System.out.println("Result of addition: " + (a + b));
System.out.println("Result of subtraction: " + (a - b));
System.out.println("Result of multiplication: " + (a * b));
System.out.println("Result of division: " + (a / b));
System.out.println("Result of modulus: " + (a % b));
break;
case 2:
System.out.print("Enter an integer value 'num': ");
int num = scanner.nextInt();
System.out.print("Enter the number of shifts 'n': ");
int n = scanner.nextInt();
System.out.println("Result of left shift: " + (num << n));
System.out.println("Result of right shift: " + (num >> n));
System.out.println("Result of unsigned right shift: " + (num >>> n));
break;
case 3:
Artificial
case 4:
System.out.print("Enter boolean value 'bool1' (true or false): ");
boolean bool1 = scanner.nextBoolean();
System.out.print("Enter boolean value 'bool2' (true or false): ");
boolean bool2 = scanner.nextBoolean();
System.out.println("Result of logical AND: " + (bool1 && bool2));
System.out.println("Result of logical OR: " + (bool1 || bool2));
System.out.println("Result of logical NOT of 'bool1': " + (!bool1));
System.out.println("Result of logical NOT of 'bool2': " + (!bool2));
break;
case 5:
System.out.print("Enter value 'a': ");
int aRelational = scanner.nextInt();
System.out.print("Enter value 'b': ");
int bRelational = scanner.nextInt();
System.out.println("Result of greater than (a > b): " + (aRelational > bRelational));
System.out.println("Result of less than (a < b): " + (aRelational < bRelational));
System.out.println("Result of greater than or equal to (a >= b): " + (aRelational >= bRelational));
System.out.println("Result of less than or equal to (a <= b): " + (aRelational <= bRelational));
System.out.println("Result of equality (a == b): " + (aRelational == bRelational));
System.out.println("Result of inequality (a != b): " + (aRelational != bRelational));
break;
case 6:
System.out.println("Exiting the program.");
break;
default:
System.out.println("Invalid choice. Please select a valid option.");
}
Artificial
Output:
Example 1
Artificial
Example 3
Artificial
Example 4
Conclusion: The Java program for operator demonstration demonstrates various types of
operators interactively, using a switch-case statement to efficiently handle user choices. It
covers Arithmetic, Shift, Bitwise, Logical, and Relational operators, providing prompts and
accurate results. The user-friendly implementation allows exploration and understanding at
their own pace. The program returns accurate results and handles invalid choices with error
messages. Overall, it is suitable for educational purposes and programming practice,
enhancing understanding of Java operators.