0% found this document useful (0 votes)
14 views

Java Operators Excerise

Uploaded by

awanjiku156
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java Operators Excerise

Uploaded by

awanjiku156
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Operators Examples

Example 1 : Bitwise Operators


class BitwiseOperators {
public static void main(String args[]){
//!(logical not) It negates the result of the evaluation
System.out.println(!(3<10)); // True but note that (3>7) returns false
// bitwise exclusive OR
System.out.println((0)^(0)); //if one or the other of the operands is true but not both
// bitwise exclusive OR
System.out.println((3>2)^(2>7)); //if one or the other of the operands is true but not both
//bitwise inclusive OR
System.out.println((3>2)|(2>7)); //either op1 or op2 is true, always evaluates op1 and op2
}
}

Example 2: Tenary Operators


class TenaryOperators {
public static void main(String args[]){
//Syntax variable x = (expression)?value if true : value if false
int marks;
marks = 30;
char grade= (marks >= 40) ? 'P': 'F';
System.out.println( "The grade is : " + grade );
}
}

Example 3: Working with string object


class TenaryOperators {
public static void main(String args[]){
int marks;
marks = 60;
String grd;
// In this case we use String.valueOf() method to convert the passed argument to a string
grd= (marks >= 40) ? String.valueOf("Pass"): String.valueOf("Fail");
System.out.println( "The grade is : " + grd );
}
}

Example 4: Generating the marks randomly


class Operators {
public static void main(String args[]){
int marks;
// Generating the marks randomly between 20 and 95 %
marks =20+(int)(Math.ceil(Math.random()*95));
String grd;
grd= (marks >= 40) ? String.valueOf("Pass"): String.valueOf("Fail");
System.out.println( "The mark generated is : " + marks + " and the grade is"+ grd);
}
}

Example 5: SHIFT OPERATORS


class ShiftOperators {
public static void main(String args[]){
System.out.println((13 >>2));//13=1101 in binary Shifts 2 bits to the right becomes 11 which is 3
System.out.println((13 >>>2));//13=1101 in binary Shifts 2 bits to the right becomes 11 which is 3
System.out.println((78>>3));//78=1001110 in binary Shifts 3 bits to the right becomes 1001 which is 9 decimal
System.out.println((13<<2));//13=1101 in binary Shifts 3 bits to the left becomes 1101000 which is
104
System.out.println((78<<3));//78=1001110 in binary Shifts 3 bits to the left becomes 1001110000 which is 624 in
decimal
}
}

TASK EXERCISE
Evaluate the following expressions
1. System.out.println(((13<<3)<100));
2. System.out.println(((13<<3)<100));
3. System.out.println(Math.pow((13>>2),3));
4. System.out.println((Math.pow((13>>2),3)<100));

5. Given int x=13;


i. What is the output of System.out.println(x>>=2);
ii. What is the output of System.out.println(x>>>=2);
iii. What is the output of System.out.println(Math.pow((x>>=2),3))
iv. What is the output of System.out.println(Math.pow((x>>>=2),3)<=100));

6. Write Java program using the += operator and the while loop to generate the sum of 1 to 100
7. Write a program using the -= operator and the do while loop to generate the sum of 1 to 100
8. Write a program using /= operator and the do while loop and if selection structure to output the
number of times 2 is in 1024
9. Write a program using /= operator and the do while loop and if selection structure to output the
number of times is 200 divisible by 2
10. Write a program using %= operator and the do while loop and if selection structure to output all
the odd numbers and their sum between 0 and 100
11. Write a program using %= operator and the do while loop and if selection structure to output all
the odd numbers between -27 and 78
12. Implement the code below and write down its output. Explain what is happening
int x=1024;
for( int i=1;i<=10;i++)
System.out.println(x>>i);
13. Now Implement the code below and write down its output. Explain what is happening.
int x=1;
for( int i=1;i<=10;i++)
System.out.println(x<<i);
14. What lesson do you learn from number 12 and 13 above

You might also like