Java - II (Mathematical Operator and Comparision Operator) 737764
Java - II (Mathematical Operator and Comparision Operator) 737764
Addition
Subtraction
Multiplication
Division
Modulo
Operator Opeartion
+ Addition
- Subtraction
* Multiplication
/ Division
Manipulating the quantity or Adding the items affects the total price. This is how
Mathematical operators are used behind the picture.
Divison Operator(/)
The / operator is the division operator.
If we use the division operator with two integers, then the resulting quotient will also be
an integer. And, if one of the operands is a decimal (double / float dataType) number,
the result will also be in floating-point.
In Java,
(9 / 2) is 4
(9.0 / 2) is 4.5
(9 / 2.0) is 4.5
(9.0 / 2.0) is 4.5
quotient
divisor
dividend
remainder
Looking at the above picture, you will get clear about the above terms
Untitled 1 2 3 4
Untitled 5 6 7 8
Untitled 9 10 11 12
Let's take the analogy: There are many candidates who came to the company office
to give interviews.
Since there are more than 100 candidates, 4 interview panel setups where one
interviewer is used to take the interviews..
Result = 25 % 4 = 1
Also, Using the same analogy of 4 Google servers and more than 1000 requests are
coming in a sequence
Math.abs() : Math.abs(-2) = 2
Math.min() : Math.min(2,3) = 2
Math.max() : Math.max(2,3) = 3
For Ex :
expr = ((a+b)*(c+d))/e
For Ex :
int a = 3;
int b = 2;
int c = 4;
System.out.println(sum)
String Concatenation
Instructor Task (5 mins): Discuss examples of how to concatenate
multiple strings
Concatenation means a series of interconnected things.
Example 1 : Example 2 :
The problem statement is to store the following information into a unique variable i.e
House No
Area
City
int num1 = 2;
int num2 = 3;
int sum = num1 + num2;
System.out.println("Sum is ",sum);
For Ex :
For Ex :
When the user gets logged_in then in Thus, the Credit card flag is set to
the backend the logged_in flag got true, only when you use a credit card.
true.
Name
Age
Gender
has_driving_license
citizen_of_india
== (Equal To)
Title Condition
Output
1. First value is strictly greater than the second value. int a = 5; int b
Untitled
= 4; System.out.println(a>b); // Outputs True
2. First value is less than or equal to the second value. int a = 5; int b
Untitled
= 5; System.out.println(a>b); // Outputs False
Code 1 : Check whether the Ram age is greater than Mohan or not
Student Task
There is some problem with the above solution, what if the sunil marks is 35
In that case, above code will give the result false but in real it should be true
thus, we will use ≥ (greater than or equal to) to solve this problem
Note :
3 > 3 is False
2 > -3 is True
Title Condition
Output
1. First value is greater than or equal to the second value. For ex : int a =
Untitled
5; int b = 5; System.out.println(a>=b); // True
2. First value is less than the second value. For ex : int a = 3; int b =
Untitled
5; System.out.println(a>=b); // False
/*Question
10>6 10>=6 10>=10 10>10 -9>-8 */
//Solution
System.out.println(10>6);
System.out.println(10>=6);
System.out.println(10>=10);
System.out.println(10>10);
System.out.println(-9>-8);
Note :
3 >= 3 is True
2 >= -3 is True
2. First value is greater than or equal to the second value. For ex : int a = 5;
Untitled
int b = 2; System.out.println(a<b); // False
Note : In the above code, we can not use ≤ (lesser than or equal to), it will give the
wrong result
Title Condition
Output
1. First value is strictly less than or equal to the second value. For ex : int a
Untitled
= 4; int b = 5; System.out.println(a<=b); // True
2. First value is greater than the second value. For ex : int a = 6; int
Untitled
b = 5; System.out.println(a<=b); // False
Problems
Student Task
Code 5 : Those customer will be eligible for the amazon discount whose spending
is equal to or above 4000. Check whether a customer sam is eligible for discount
or not
Wrong Code
System.out.println(eligible_for_discount);
The problem with the above code , we have used only greater than but in the
question it was given that “eligible for the amazon discount whose spending is
equal to or above 4000”.
Correct Code
Student Task
Code 6 : Check whether heeralal is eligible for driving vehicle in India or not.
Equal to Operator ( == )
Checks if two values are equal or not . If values are equal it return true else return false
Student Task
Code 7 : Find the output
int a = 5;
int b = 6;
int c = 6
System.out.println(b==c);
System.out.prinln(a!=b);
System.out.println(b==a);
OJ Problem Solve
We will take two Problems Update & Compare and Compare Seven Numbers
Problem 1
Update & Compare
System.out.println(num1>num2);
num1 = num1 + num3;
Problem 2
Compare Seven Numbers
https://oj.masaischool.com/contest/2667/problem/04
public static void compareSevenNumbers(int a,int b,int c,int d,int e,int f,int g){
int sum1 = (a + b) * c;
int sum2 = (d + e + f + g);
System.out.println(sum1>sum2);
}