Lab Manual - 4: CLO No. Learning Outcomes Assessment Item BT Level PLO
Lab Manual - 4: CLO No. Learning Outcomes Assessment Item BT Level PLO
Lab Manual - 4
OOP
3rdSemester Lab-4: Operators in Java
Laboratory 5:
Lab Objectives: After this lab, the students should be able to understand the following Operators in
Java:
• Conditional
• Arithmetic
• Arithmetic Assignment
• Relational
• Logical
• Bitwise
1.
2. Conditional Operator ( ? : ):
Conditional operator is also known as the ternary operator. This operator consists of three
operands and is used to evaluate Boolean expressions. The goal of the operator is to decide
which value should be assigned to the variable. The operator is written as:
variable x = (expression) ? value if true : value if false
Example:
import java.util.Scanner;
public class PassFail {
public static void main(String[] args) {
// take input from users
Scanner input = new Scanner(System.in);
System.out.println("Enter your marks: ");
double marks = input.nextDouble();
Output:1
Output:2
Exercise:1
What will be the value of b after the execution of the following program?
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
Modulus Operator:
Java has one important arithmetical operator you may not be familiar with, % , also known as
the modulus or remainder operator. The % operator returns the remainder of two numbers.
For instance, 10 % 3 is 1 because 10 divided by 3 leaves a remainder of 1.
Note: The % operator can also be used with integers and floating-point types.
int x2=42;
double y3=42.25;
Output:
Exercise:2
int i = 10;
int n = i++%5;
1. What are the values of i and n when the code is executed?
2. What are the final values of i and n if instead of using the postfix increment operator
(i++), you use the prefix version (++i)?
Exercise:3
In the following program, explain why the value "6" is printed twice in a row:
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
System.out.println(i); // "4"
++i;
System.out.println(i); // "5"
System.out.println(++i); // "6"
System.out.println(i++); // "6"
System.out.println(i); // "7"
}
}
Exercise:4
class ArithmeticDemo {
public static void main(String[] args) {
int result=2;
result = result + 1; // result is now 3
System.out.println(result);
result = result - 1; // result is now2
System.out.println(result);
result = result * 2; // result is now4
System.out.println(result);
result = result / 2; // result is now 2
System.out.println(result);
result = result + 8; // result is now 10
System.out.println(result);
result = result % 7; // result is now 3
System.out.println(result); }
}
Exercise:5
Exercise 6:
short x1 = 4;
// x1=x1+6.6;
x1 += 6.6;
System.out.println("x1 is " + x1 + "!"); //10
Because here 6.6 which is double is automatically converted to short type without explicit
type-casting.
// Another example
byte b = 10;
//b = b + 10;
b += 10;
System.out.println(b); //20
Any type in Java, including integers, floating-point numbers, characters, and Booleans can be
compared using the equality test, ==, and the inequality test, !=.
Only numeric types can be compared using the ordering operators. That is, only integer,
floating-point, and character operands may be compared to see which is greater or less than
the other.
The Boolean logical operators shown below operate only on boolean operands.
The following table lists the logical operators:
Operator Description
& Logical AND
| Logical OR
^ Logical XOR
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
These logical operators are used to check whether an expression is true or false.
Assume Boolean variables A holds true and variable B holds false, then:
(A&&B ) is false.
(A || B ) is true.
EXERCISE:
1. Evaluate the following Boolean expressions and prints their results. Boolean b1, b2,
b3=true;
b1 = true || false;
b2 = b1 && (5 > 3);
b3 = b2 && (13 == 6);
Java defines several bitwise operators, which can be applied to the integer types, long, int,
short, char, and byte. These operators act upon the individual bits of their operands. The
following table lists the bitwise operators:
Assume integer variable A holds 60 and variable B holds 13 then:
LAB TASKS
1. Write a Java Program to find the result of the following Expressions. (Assume a=10 and b=5).
Marks: 4
i) (a<<2)+(b>>2)
ii) (b>0)
iii) (a+b*100)/10
iv) a&b
2. Find the result of following expressions. Assume a=5 and b=4. Marks: 4
i. a & (-a);
ii. (a++ != b++) && (a++ ==b++);
iii. (--a != --b) | (--a == --b);
iv. (a < 0 ? -a : a);
3. Convert a negative integer -37 into a binary form. (Do it on paper). Mark: 2
*********