Professional Documents
Culture Documents
Lab 04 Java - 2k21
Lab 04 Java - 2k21
Lab 04 Java - 2k21
Lab Manual - 4
OOP
3rdSemester Lab-4: Operators in Java
Laboratory 4:
Lab Objectives: After this lab, the students should be able to understand Java Math class and the
following Operators:
Conditional
Unary Operators
Arithmetic
Arithmetic Assignment
Relational
Logical
Bitwise
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?
int a , b;
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 );
}
}
The Java unary operators require only one operand. Unary operators are used to perform
various operations i.e.:
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
System.out.println(i);
++i;
System.out.println(i); System.out.println(+
+i); System.out.println(i++);
System.out.println(i);
}
}
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:4
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 5:
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
5. Relational Operators:
The relational operators determine the relationship that one operand has to the other.
Specifically, they determine equality and ordering.
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.
Example Meaning
Expression1 && Expression2 True only if both Expression1 and Expression2 are true
Expression1 || Expression2 True if either Expression1 or Expression2 is true
Assume Boolean variables A holds true and variable B holds false, then:
(A&&B ) is false and (A || B ) is true.
Exercise 6:
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:
The class Math, which is a part of the package java.lang provides methods for mathematical
functions like sine, cosine, logarithm, and so on. Most of its functions should be familiar to
students who have had a course in trigonometry. Some of the more common methods there
include:
Method Description
abs(x) Absolute value of x
sin(x) Sine of x (in radians)
cos(x) Cosine of x (in radians)
pow(x,y) x raised to power y
sqrt(x) Square root of x
In each of these methods, the arguments x and y, are real (double) values. (Any value that can
be promoted to a double value can be used as well.)
The definitions of these methods are stored the package java.lang so there is no need to do an
import.
To use a method, we need to add the name of the class it belongs to. For example, to compute
the sine of the variable x and place the result in y we would need to use
y = Math.sin(x);
LAB TASKS
1. Write a Java Program to find the result of the following Expressions. (Assume a=5
and b=4). Marks: 3
i. (a<<2)+(b>>2)
ii. (a++ != b++) && (a++ ==b++);
iii. (--a != --b) | (--a == --b);
2. Write a Java program to input several values into an array, verify that the length of
an array is between 1 and 100. If outside of that range, output an error message and
end the program, otherwise continue. Process the array by passing it to a method
that will compute and return the standard deviation of the values in the array.
The standard deviation first requires that you compute the average. Assuming our
array is values, and the average is avg, the standard deviation is computed as:
However, if n is 0 there are no values at all and if n is 1, you will get an error. So if n
is not at least 2, return the value -1000. This will indicate that there were not enough
values in the array to compute the stddev. Marks: 7
Run the Code with the following 10 inputs and observe the result.
1
2
3
9
8
7
4
6
12
15
*********