Worksheet 04 - Operators
Worksheet 04 - Operators
SESSION- 2021-22
STUDY MATERIAL
SUBJECT- COMPUTER
APPLICATIONS
CLASS- IX
Operators & Expressions
Operators are special symbols (characters) that carry out operations on operands (variables
and values). For example, + is an operator that performs addition.
Assignment Operator
Assignment operators are used in Java to assign values to variables. For example,
int age;
age = 5;
The assignment operator assigns the value on its right to the variable on its left. Here, 5 is
assigned to the variable age using = operator.
There are other assignment operators too. However, to keep things simple, we will learn other
assignment operators later in this article.
class AssignmentOperator {
public static void main(String[] args) {
// Assigning 5 to number1
number1 = 5;
System.out.println(number1);
Output:
5
5
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication, etc.
Operator Meaning
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Remainder Operator
class ArithmeticOperator {
public static void main(String[] args) {
Output:
In the above example, all operands used are variables. However, it's not necessary at all.
Operands used in arithmetic operators can be literals as well. For example,
class ArithmeticOperator {
public static void main(String[] args) {
Output:
Unary Operators
The unary operator performs operations on only one operand.
Operator Meaning
+ Unary plus (not necessary to use since numbers are positive without using it)
class UnaryOperator {
public static void main(String[] args) {
Output:
+number = 5.2
-number = -5.2
number = 6.2
number = 5.2
!flag = true
Increment and Decrement Operator
You can also use ++ and -- operator as both prefix and postfix in Java. The ++ operator
increases value by 1 and -- operator decreases the value by 1.
int myInt = 5;
Simple enough until now. However, there is a crucial difference while using increment and
decrement operators as prefix and postfix. Consider this example,
class UnaryOperator {
public static void main(String[] args) {
System.out.println(number++);
System.out.println(number);
System.out.println(++number);
System.out.println(number);
}
}
Output:
5.2
6.2
7.2
7.2
System.out.println(number++);
When this statement is executed, the original value is evaluated first. Then the number is
increased. This is the reason you are getting 5.2 as an output.
Now, when the line,
System.out.println(number);
System.out.println(++number);
will increase the number by 1 first and then the statement is executed. Hence the output is 7.2.
Equality and relational operators are used in decision making and loops (which will be
discussed later). For now, check this simple example.
Example 6: Equality and Relational Operators
class RelationalOperator {
public static void main(String[] args) {
Output:
Here, we have used > operator to check if number1 is greater than number2 or not.
Since number2 is greater than number1, the expression number1 > number2 is evaluated to false.
Hence, the block of code inside else is executed and the block of code inside if is skipped.
If you didn't understand the above code, don't worry. You will learn it in detail in Java
if...else article.
For now, just remember that the equality and relational operators compare two operands and
is evaluated to either true or false
Logical Operators
The logical operators || (conditional-OR) and && (conditional-AND) operate on boolean
expressions. Here's how they work.
Operator Description Example
class LogicalOperator {
public static void main(String[] args) {
Output:
true
false
Ternary Operator
The conditional operator or ternary operator ?: is shorthand for the if-then-else statement. The
syntax of the conditional operator is:
class ConditionalOperator {
public static void main(String[] args) {
Output:
Leap year
PURWANCHAL VIDYAMANDIR
SESSION : 2021-2022
CLASS : IX
SUBJECT : COMPUTER APPLICATIONS
TOPIC : OPERATORS
2. Define a class circle to find out the area and perimeter of circle with following data
members.
Data members
double radius
double area
double cir
Member methods
void input() : input radius
void area() : calculate the area and display
void cirm() : calculate the perimeter and display .
3. Write the program that defines the class Simple_interest with the following data members-
Data members :
int principle
double rate
int time
double amount
MEMBER METHODS
void input ()-to store the principle, rate &time
void interest_calculate ()-to calculate the simple interest and total amount
void print ()-to print the principle & the total amount.