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

Java - II (Mathematical Operator and Comparision Operator) 737764

Uploaded by

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

Java - II (Mathematical Operator and Comparision Operator) 737764

Uploaded by

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

Java - II ( Mathematical Operator

and Comparision Operator )

What are the basic mathematical operations


The common operators in Java are :

Addition

Subtraction

Multiplication

Division

Modulo

Operator Opeartion

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulo Operation (Remainder after division)

Student Task: Student need to solve a problem [ 3 min ]


Problem: Given a = 15, b = 25 . Calculate sum, difference, product, and division.

Real world example:

Java - II ( Mathematical Operator and Comparision Operator ) 1


Visit amazon.com, add some products to the cart.

Adding products leads to an increase in the cart price.

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

Modulo Operator(%) [Remainder]

Java - II ( Mathematical Operator and Comparision Operator ) 2


When we divide a number into another, there are 4 terms that we need to
understand

quotient

divisor

dividend

remainder

Looking at the above picture, you will get clear about the above terms

The remainder represents by %.

Java - II ( Mathematical Operator and Comparision Operator ) 3


For Ex : int result = 75 % 4 [ Read as 75 Modulus 4 ]

Instructor Task: Show demo to students, Explaining Modulus [10 min ]

Interview Panel Interview Panel Interview Panel Interview Panel


Title
1 2 3 4

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

Unique Id is assigned to each candidate, starting from 1,2,3........to 100. If Interview


Panel 1 is assigned to ID 1, Interview Panel is assigned to ID 2, and so on.

Now, the Question is Which Interview Panel will be assigned to Candidate 25 ?

The Solution Lies in Modulus: Just calculate 25 % 4 [ Since there are 4


Interview Panel]

Result = 25 % 4 = 1

Candidate 25 is assigned to Interview Panel 1.

Power of % lies in distributing

Java - II ( Mathematical Operator and Comparision Operator ) 4


Student Task: Google Server Analogy [ 5 min ]

Also, Using the same analogy of 4 Google servers and more than 1000 requests are
coming in a sequence

Which server will process the 63rd request?

Note : In Java The % operator is mainly used with integers.

Java Maths Library


Math.pow() : Math.pow(2,2) = 4.0

Math.sqrt() : Math.sqrt(16) = 4.0

Math.abs() : Math.abs(-2) = 2

Math.floor() : Math.floor(2.6) = 2.0

Math.ceil() : Math.ceil(2.456) = 3.0

Math.min() : Math.min(2,3) = 2

Math.max() : Math.max(2,3) = 3

Java - II ( Mathematical Operator and Comparision Operator ) 5


How to Solve long-expression? [ 5 min ]
Instructor Task (5 mins): Discuss the steps to solve long-
expression using brackets
If you want to solve long-expression containing multiple operations then use
brackets.

Putting brackets also gives readability to code.

For Ex :

expr = ((a+b)*(c+d))/e

Student Task: Solve the Expression [ 5 min ]

For Ex :

int a = 3;

int b = 2;

int c = 4;

int sum = (a*2) + (b*2) + (c*2);

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.

Use + to join two or more string

Example 1 : Example 2 :

Java - II ( Mathematical Operator and Comparision Operator ) 6


String name = "Shubhendu"; String a = "Masai School";
System.out.println("Hello "+name+" Welco String line = a+" "+b;
me To Masai"); System.out.println(line);

Student Task: Task to students to print their address. [ 5 min ]

The problem statement is to store the following information into a unique variable i.e

House No

Area

City

The task is to print the complete address.

Concatenation with Numbers

int num1 = 2;
int num2 = 3;
int sum = num1 + num2;
System.out.println("Sum is ",sum);

Boolean Data Type


Discuss boolean data type with examples.
The datatype which has only two values i.e True and False;

Some Questions only answered in True or False :

For Ex :

Whether Student is Pass or Fail: Either True or False

Whether Age is above 18 or not

For Ex :

boolean driving = true;

Java - II ( Mathematical Operator and Comparision Operator ) 7


boolean smoke = false;

Real-World Example [5 min]

When the user logged in to any During Shopping on the e-commerce


platform like Amazon, Facebook, platform. Some platform offers a 10%
Gmail, etc then the user need to log discount on the product only when
in. you will use a credit card.

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.

If user logged_in, then logged_in


= true

If user logged_out , then


logged_in = false

Student Task: Task to print the following information [ 5 min ]

Name

Age

Gender

has_driving_license

citizen_of_india

What is Java Relational Operators ?


Below are the relationship operators

> (Greater Than)

>= (Greater Than Equal To)

< (Less Than)

Java - II ( Mathematical Operator and Comparision Operator ) 8


<= (Less Than Equal To)

== (Equal To)

! = (Not Equal To)

The operator of the Relational operator is always a boolean value.

Operator Explanation Example

== Is Equal To 3 == 5 returns false

!= Not Equal To 3 != 5 returns true

> Greater Than 3 > 5 returns false

< Less Than 3 < 5 returns true

>= Greater Than or Equal To 3 >= 5 returns false

<= Less Than or Equal To 3 <= 5 returns true

Greater Than Operator ( > )

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

Java - II ( Mathematical Operator and Comparision Operator ) 9


Instructor Task

Code 1 : Check whether the Ram age is greater than Mohan or not

int age_of_ram = 25;


int age_of_mohan = 30;

System.out.println(age_of_ram > age_of_mohan);

Student Task

Code 2 : Check whether sunil is passed or not, where passing marks is 35

int sunil_marks = 36;


int passing_marks = 35;

boolean is_passed = sunil_marks>passing_marks;


System.out.println(is_passed); // true : Sunil is passed

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

int sunil_marks = 35;


int passing_marks = 35;

boolean is_passed = sunil_marks>passing_marks;


System.out.println(is_passed); // false : Sunil is failed

thus, we will use ≥ (greater than or equal to) to solve this problem

int sunil_marks = 35;


int passing_marks = 35;

boolean is_passed = sunil_marks>=passing_marks;


System.out.println(is_passed); // true : Sunil is passed

Note :

3 > 3 is False

2 > -3 is True

Java - II ( Mathematical Operator and Comparision Operator ) 10


Greater Than Operator ( >= )

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

Student Task (2 mins)


Code 3 : Try these question on replit

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

Less Than Operator ( < )

Java - II ( Mathematical Operator and Comparision Operator ) 11


Title Condition
Output
1. First value is strictly less than the second value. For ex : int a = 4;
Untitled
int b = 5; System.out.println(a<b); // 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

Instructor Task (5 mins)


Code 4 : Check whether sunil is failed or not, where passing marks is 35

int sunil_marks = 34;


int passing_marks = 35;

boolean is_failed = sunil_marks<passing_marks;

System.out.println(is_failed); // true : Sunil is failed

Note : In the above code, we can not use ≤ (lesser than or equal to), it will give the
wrong result

Less Than Equal to Operator ( <= )

Title Condition
Output

Java - II ( Mathematical Operator and Comparision Operator ) 12


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

int minimum_purchase = 4000;


int sam_purchase = 5000;

boolean eligible_for_discount = sam_purchase>minimum_purchase;

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

int minimum_purchase = 4000;


int sam_purchase = 5000;

booleans eligible_for_discount = sam_purchase>=minimum_purchase;


System.out.println(eligible_for_discount);

Student Task
Code 6 : Check whether heeralal is eligible for driving vehicle in India or not.

Legal age in india of driving vehicle 18 +

Java - II ( Mathematical Operator and Comparision Operator ) 13


int age_of_heeralal = 18;
int legal_age = 18;

boolean is_eligible_for_driver_license = age_of_heeralal>=legal_age;


System.out.println(is_eligible_for_driver_license);

Equal to Operator ( == )
Checks if two values are equal or not . If values are equal it return true else return false

Not Equal to Operator ( != )


Checks if two values are equal or not . If values are not 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

Java - II ( Mathematical Operator and Comparision Operator ) 14


https://oj.masaischool.com/contest/2667/problem/01

public static void updateCompare(int num1,int num2,int num3){

System.out.println(num1>num2);
num1 = num1 + num3;

System.out.println(num1 > num2);


}

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

Java - II ( Mathematical Operator and Comparision Operator ) 15

You might also like