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

Module1 - Chapter6 - Core Java

Module for core java, module for core hava jbbvjnjn

Uploaded by

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

Module1 - Chapter6 - Core Java

Module for core java, module for core hava jbbvjnjn

Uploaded by

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

CORE JAVA

MANUAL V8.3

MODULE CODE:
DL.A.01.01

ANUDIP FOUNDATION
Trainer
Manual Core Java

1
Trainer
Manual Core Java

Module 1: Java Fundamental and Programming Concepts

Chapter 6

Objective: After completing this lesson you will be Materials Required:


able to :
* Get familiar with Java operators 1. Computer
* Gain an idea about some conditional statements 2. Internet access

Theory Duration: 60 minutes Practical Duration: 60 minutes

Total Duration: 120 minutes

2
Trainer
Manual Core Java

Chapter 6

Operators (Arithmetic, Logical, Relational),

Operators in Java

In Java, an operator is a symbol used for performing operations on values and variables. Some examples of operators

include +, -, *, /.

The value on which an operator is applied is known as an operand. The operator plays a defining role in determining

what action will be performed between two operands.

Three of the most common operators in Java are -

i) Arithmetic Operators

ii) Logical Operators

iii) Relational Operators

i) Java Arithmetic Operators

Arithmetic operators in Java are utilised for performing actions like addition (+), multiplication (*), subtraction (-) and

division (/) and modulus (%). Hence, this operator can be used to perform some of the most fundamental

mathematical calculation tasks.

* Example of Java Arithmetic Operator

class OperatorExample{
public static void main(String args[]){

int a=20;

int b=10;

3
Trainer
Manual Core Java

System.out.println(a+b); → 30

System.out.println(a-b); → 10

System.out.println(a*b); → 200

System.out.println(a/b); → 2

}}

Output:

30

10

200

* Example of Java Arithmetic Operator: Expression

class OperatorExample{
public static void main(String args[]){

System.out.println(10*10/5+3-1*4/2);

}}

Output:

21

ii) Logical Operators

Logical operators in Java are used for carrying out “logical AND” and “logical OR” operations. This operator is

used to enable the checking of multiple conditions simultaneously. They are also known as Boolean operators as they

use Boolean operand.

4
Trainer
Manual Core Java

* Logical Operator Types and Functions

&& (Logical AND) - If two operands are true, only then can a ‘logical AND operator’ be considered as ‘true’.

|| (Logical OR) - This operator produces a ‘true’ result only if one of its operands is true. The result remains

‘true’ if one or both expressions are true.

! (Logical Not) - It is a Unary operator and functions with individual operands. This operator is used for reversing

operand values. It produces a false result for a true value, and vice versa.

* A program demonstrating Java logical operators -

import java.util.*;

public class operators {


public static void main(String[] args)

{
String x = "Bob";

String y = "Cat";

Scanner s = new Scanner(System.in);

System.out.print("Enter username:");

String uuid = s.next();

System.out.print("Enter password:");

String upwd = s.next();

Verifying whether password and username match -

if ((uuid.equals(x) && upwd.equals(y))

|| (uuid.equals(y) && upwd.equals(x))) {

System.out.println("Welcome");

5
Trainer
Manual Core Java

else {
System.out.println("Wrong password or uid");

}
}
}

Output:

Enter username:Bob

Enter password:Cat

Welcome.

iii) Relational Operators

Relational operators in Java are utilised for checking relations such as greater than, less than and equality. These

operators perform comparisons and produce boolean outputs. Relational operators are utilised for conditional ‘if

else’ statements and loop statements.

Format of representation is → variable relation_operator value

* Relational Operator Types and Functions

== (equal to): Output is true if the left hand side value is equal to the right hand side value.

!= (not equal to): Output is true if the left hand side value is not equal to the right hand side value.

< (less than): Output is true if the left hand side value is less compared to the right hand side value.

<= (less than or equal to): Output is true if the left hand side value is less than/equal to the right hand side value.

> (greater than): Output is true if the left hand side value is greater compared to the right hand side value.

>= (greater than or equal to): Output is true if the left hand side value is greater than/equal to the right hand side

value.

6
Trainer
Manual Core Java

* A program demonstrating Java relational operators -

public class operators {


public static void main(String[] args)

{
int a = 10, b = 5;

String x = "Thank", y = "Thank";

int ar[] = { 1, 2, 3 };

int br[] = { 1, 2, 3 };

Boolean condition = true;

Conditional operator variations -

System.out.println("a == b :" + (a == b));

System.out.println("a < b :" + (a < b));

System.out.println("a <= b :" + (a <= b));

System.out.println("a > b :" + (a > b));

System.out.println("a >= b :" + (a >= b));

System.out.println("a != b :" + (a != b));

System.out.println("x == y : " + (ar == br));

System.out.println("condition==true :"

+ (condition == true));

}
}
Output:

a == b :false

a b :true

a >= b :true

7
Trainer
Manual Core Java

a != b :true

x == y : false

condition==true :true

Practical (60 minutes) -

See the example programme for Java arithmetic operators below. Write the same programme with values of int a =
18, and int b = 7 and show the resulting output. Rewrite the programme for int a = 10 and int b = 3.

class OperatorExample{

public static void main(String args[]){

int a=20;

int b=10;

System.out.println(a+b); → 30

System.out.println(a-b); → 10

System.out.println(a*b); → 200

System.out.println(a/b); → 2

}}

8
Trainer
Manual Core Java

Instructions: The progress of students will be assessed with the exercises mentioned below.

MCQ

1. What is an operator in Java?

a) a sign

b) a symbol

c) a callsign

d) None of the mentioned

2. What type of operator is modulus?

a) arithmetic

b) relational

c) logical

d) All of the mentioned

3. ___________ is the value on which an operator is applied.

a) Character

b) Decimal

c) Operand

d) None of the mentioned

4. Two logical operators are logical AND and logical ______.

a) FOR

9
Trainer
Manual Core Java

b) OR

c) IF

d) IF-ELSE

5. Logical operators are also known as __________ operators.

a) Mathematical

b) Transitional

c) Boolean

d) None of the mentioned

6. && is a ___________ operator in Java.

a) relational

b) logical

c) arithmetic

d) None of the mentioned

7. ‘if else’ statements use ___________ operators.

a) arithmetic

b) logical

c) relational

d) None of the mentioned

10
Trainer
Manual Core Java

8. The != rational operator type signifies -

a) less than or equal to

b) not equal to

c) equal to

d) None of the mentioned

9. Conditional statements verify if certain conditions are either true or ______,

a) false

b) sometimes true

c) partially false

d) None of the mentioned

10. A conditional statement is also referred to as a conditional _____________.

a) Enumeration

b) explanation

c) expression

d) None of the mentioned

11

You might also like