Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Lab Manual - 4: CLO No. Learning Outcomes Assessment Item BT Level PLO

This document discusses various operators in Java including: 1. Conditional operators which evaluate Boolean expressions like the ternary operator. 2. Arithmetic operators for mathematical expressions like addition, subtraction, multiplication, division. 3. Relational operators that determine relationships between operands like equality and ordering. 4. Logical operators that operate on Boolean values like AND, OR, NOT. It provides examples of using each operator type and exercises for readers to practice applying the operators.

Uploaded by

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

Lab Manual - 4: CLO No. Learning Outcomes Assessment Item BT Level PLO

This document discusses various operators in Java including: 1. Conditional operators which evaluate Boolean expressions like the ternary operator. 2. Arithmetic operators for mathematical expressions like addition, subtraction, multiplication, division. 3. Relational operators that determine relationships between operands like equality and ordering. 4. Logical operators that operate on Boolean values like AND, OR, NOT. It provides examples of using each operator type and exercises for readers to practice applying the operators.

Uploaded by

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

UET TAXILA

CLO Learning Outcomes Assessment Item BT Level PLO


No.

1 Construct the experiments / projects of Lab Task, Mid Exam, Final


varying complexities. Exam, Quiz, Assignment, P2 3
Semester Project

2 Use modern tool and languages. Lab Task, Semester Project P2 5

3 Demonstrate an original solution of Lab Assignment, Lab Task,


A2 8
problem under discussion. Semester Project

4 Work individually as well as in teams Lab Task, Semester Project A2 9

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();

// ternary operator checks if marks is greater than 40


String result = (marks > 40) ? "pass" : "fail";

System.out.println("You " + result + " the exam.");


input.close();
}
}

Output:1

Output:2

Exercise:1

What will be the value of b after the execution of the following program?

public class Test {


public static void main(String args[]){
int a , b;

Engr. Sidra Shafi Lab-4 1


3rdSemester Lab-4: Operators in Java

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 );
}
}

2.The Arithmetic Operators:


Arithmetic operators are used in mathematical expressions in the same way that they are used
in algebra. The following table lists the arithmetic operators: Assume integer variable A
holds 10 and variable B holds 20, then:

Operator Description Example


+ Addition - Adds values on either side of the operator A + B will give 30
- Subtraction - Subtracts right hand operand from left hand operand A - B will give -10
* Multiplication - Multiplies values on either side of the operator A * B will give 200
/ Division - Divides left hand operand by right hand operand B / A will give 2
% Modulus - Divides left hand operand by right hand operand and returns remainder B % A will give 0
++ Increment - Increases the value of operand by 1 B++ gives 21
-- Decrement - Decreases the value of operand by 1 B-- gives 19

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;

System.out.println("x2 mod 10 is " + x2 % 10);


System.out.println("y3 mod 10 is " + y3 % 10);

Output:

Exercise:2

Consider the following code snippet:

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)?

Engr. Sidra Shafi Lab-4 2


3rdSemester Lab-4: Operators in Java

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"
}
}

Compound assignment operators in Java:


Compound-assignment operators provide a shorter syntax for assigning the result of an
arithmetic or bitwise operator. They perform the operation on the two operands before
assigning the result to the first operand.

The following are all possible assignment operator in java:

Exercise:4

Change the following program to use compound assignments:

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

Engr. Sidra Shafi Lab-4 3


3rdSemester Lab-4: Operators in Java

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

What will be the output of the following program?

public class Operatorsex {


public static void main(String[] args) {
int x=16;
int y=x++;
int z=++y;
System.out.println("x, y, z are " + x + "," + y + "," + z );
}
}

Exercise 6:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op


(E2)), where T is the type of E1, except that E1 is evaluated only once.

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

When is the Type-conversion required?


When using normal assignment operator, we have to do type-casting to get the result.
In compound assignment operator type-casting is automatically done by compiler. In this
case, we don’t have to do type-casting to get the result.

3.The Relational Operators:


The relational operators determine the relationship that one operand has to the other.
Specifically, they determine equality and ordering.

Assume integer variable A holds 10 and variable B holds 20, then:

Engr. Sidra Shafi Lab-4 4


3rdSemester Lab-4: Operators in Java

The result of these operations is a boolean value.

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.

4. The Logical Operators:

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

Short-Circuit Logical Operators:

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.

Engr. Sidra Shafi Lab-4 5


3rdSemester Lab-4: Operators in Java

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);

2. What is the difference between following two statements:

Boolean b= (100>5) || (6*8>5);


Boolean b= (100>5) | (6*8>5);

5.The Bitwise Operators:

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
*********

Engr. Sidra Shafi Lab-4 6

You might also like