Go to Top

Tuesday 26 April 2011

Operators in Java

                                            The symbols used in a Java expression are known as Operators, whereas the values on which the operations are performed are known as Operands. There are three types of operators :

1. Unary Operator : These are the operators which require only one operand to perform the operation.
2. Binary Operator : These are the operators which require two operands to perform the operation.
3. Tertiary Operator : These are the operators which require three operands to perform the operation.

                                            The operators are also classified on the basis of their types of operation :

1. Arithmetical Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operator
6. Compound Assignment Operator (Short Hand Operator)


1. Arithmetical Operators : These operators are required for arithmetical calculations. They are of two types :


1. Binary Operators : The table given below lists all binary form arithmetical operators in Java :

Operator
Description
Function
Example
+
Plus
Adds the operands
a+b
-
Minus
Subtracts the operands
a-b
*
Multiply
Multiplies the operands
a*b
/
Divide
Divides the operands
a/b
%
Modulus (Remainder)
Finds the remainder when one number is divided by the other
a%b

2. Unary Operators : Operators + and - have a unary form too, in addition to binary forms. They are illustrated along with their functions in the table given below :

Operator
Description
Example
+
Promotes a to int if it’s a byte, short, or char
+a
-
Arithmetically negates a
-a

                                            The operators ++ and -- are the two shortcut arithmetic operators. They are used as follows :
1. x++ stands for x=x+1.
2. x-- stands for x=x-1.
                                            They have two versions :

1. The Prefix version : Increments/decrements the value of the operand by 1 before doing the operation with it.
1. The Postfix version : Increments/decrements the value of the operand by 1 after doing the operation with it.
                                            The table given below shows these operators :

Operator
Description
Example
++
Increments a by 1 after performing the operation with it
a++
++
Increments a by 1 before performing the operation with it
++a
--
Decrements a by 1 after performing the operation with it
a--
--
Decrements a by 1 before performing the operation with it
--a

2. Relational Operators : The relational operators in Java are used to compare the values of two operands a and b and results in the outcome of a boolean value. These are given in the table below :

Operator
Description
Example
> 
Greater than
a>b
< 
Less than
a<b
>=
Greater then equal to
a>=b
<=
Less than equal to
a<=b
= =
Equal to
a= =b
!=
Not equal to
a!=b

3. Logical Operators : These operators in Java are used to return a boolean value from an expression containing two or more comparisons. Java supports six logical operators, out of which five are binary and one is unary. The table belows shows these operators :


Operator
Description
Example
&&
Short-circuit or conditional AND
a&&b
||
Short-circuit or conditional OR
a||b
!
Logical NOT
!a
&
Logical AND
a&b
|
Logical OR
a|b
^
Logical XOR (exclusive OR)
a^b

5. Assignment Operator : The assignment operator = is used to assign value to a variable or to initialize them. For example :

int a=10;
float b=99.99;

6. Compound Assignment Operator (Short Hand Operator) : These operators are just like a shortcut assignment operator. They are given in the table below :

Compound Assignment Operator
Arithmetic Operator
a+=b
a=a+b
a-=b
a=a-b
a*=b
a=a*b
a/=b
a=a/b

Sunday 24 April 2011

What is a message in Java

                                        In the real world we communicate and interact with other objects to make it work. For example, in order to watch the television, we have to switch it on (interaction) either by remote or by using the buttons on it. In the world of computers, the software components communicate and interact with other objects by sending messages. For example, if an object A wants to run one of the methods of the object B, then it sends a message to the object B. Software objects interact and communicate with each other by sending messages. 

                                        Sometimes, the object receiving the message needs some more information than what is provided to it by the message. For example, to change the channel number or the volume of the television, we have to indicate the channel number or the volume to change to. Such information is passed along with the message as Parameters. And after the work has been done according to the message passed, the program, if correct, will show the desired output. 


What is the difference between objects and classes

Objects vs. Classes


  • An class is not the same as the object that is made from it. A class is just like the blueprint of the object, and the object is just like the product that is made by using the blueprint.For example, in a factory making cups, for a same type of cup, first a mould of the cup is made which defines the design, size, and shape of the cup. Here the mould is a blueprint which acts like a class, whereas the products i.e. the cups made by using the mould act like the objects. A class can be used to create any number of objects, such as a blueprint can be used to create any number of products or, a mould can be used to manufacture any number of cups as in the above example. 
  • An object will always occupy some memory space in the system's hard disk drive, but a class will not, because a class is merely a blueprint used to create the object whereas an object is the product made using the blueprint.
  • A class is just like a template from which an object is created.


Differences at at glance :-


1) Constituents :-

Class : Collection of variables and functions.

Object : Has access to the methods of the class, but not its data in the member variables.

2) Format :- 

Class : The format is given as follows : 

class <class name>
{
  functions.....
  variables.....
}

Object : The format is given as follows : 

class-name object-name=new class-name( );


Examples :-

  • Example 1 :-


Class : car_design 
Variables : speed, colour, size
Functions : accelerate, decelerate, stop

Object : car
Functions : accelerate, decelerate, stop

  • Example 2 :-


Class : laptop_design
Variables : processor frequency, hard disk, random access memory
Functions : switch on, switch off, calculate

Object : laptop
Functions : switch on, switch off, calculate

  • Example 3 :- 


Class : mobile_design
Variables : size, shape, processor frequency, features
Functions : call,  receive call, type a message

Object : mobile
Functions : call,  receive call, type a message

Friday 8 April 2011

To reverse a three digit number

Question 8 : Write a program in Java to print the reverse of a three digit number given by the user.


Sample input : 145
Sample output : 541


Java Program : 


class reverse
{
 static void main(int d)
 {
     int a=0;
     int e=0;
     int b=0;
     int c=0;
     int s=0;
     a=d%10;
     e=d/10;
     b=e%10;
     c=d/100;
     s=(a*100)+(b*10)+c;
     System.out.println("The reversed number : "+s);
    }
}


Input : 


145


Output : 


The reversed number : 541

Wednesday 6 April 2011

To print the area and perimeter of a rectangle

Question 8 : Write a program in Java to print the area and perimeter of a rectangle. The magnitude of length and width is to be given by the user.


Java Program :


public class rectangle

{
 public static void main(int l,int b)
 {
     int a=0,p=0;
     a=l*b;
     p=2*(l+b);
     System.out.println("Area of the rectangle = "+a+" units");
     System.out.println("Perimeter of the rectangle = "+p+" units");
    }
}

Input : 


l=5
b=2


Output : 


Area of the rectangle = 10 units
Perimeter of the rectangle = 14 units

To print the sum of two numbers

Question 7 : Write a program in Java to print the sum of two numbers given by the user.


Java Program :


public class sum
{
    public static void main(int a,int b)
    {
        int s;
        s=a+b;
        System.out.print("Sum = "+s);
    }
}

Input : 


a=10
b=20

Output : 


Sum = 30

To classify triangles on basis of their sides

Question 6 : Write a program in Java to classify a triangle whether it is equilateral, isosceles, or scalene on the basis of the values of the three sides given by the user.


Java Program :


public class triangles
{
 public static void main(int a,int b,int c)
 {
    if (a==b && b==c)
    System.out.println("The triangle with sides "+a+","+b+","+c+" is an equilateral one");
    if ((a==b && b!=c && a!=c) || (b==c && b!=a && c!=a) || (c==a & c!=b && a!=b))
    System.out.println("The triangle with sides "+a+","+b+","+c+" is an isosceles one");
    if (a!=b && a!=c && b!=c)
    System.out.println("The triangle with sides "+a+","+b+","+c+" is a scalene one");
}
}

Input : 


(i) a=10
    b=10
    c=10

(ii) a=10
     b=10
     c=20

(iii) a=10
      b=15
      c=20

Output : 


(i) The triangle with sides 10,10,10 is an equilateral one


(ii) The triangle with sides 10,10,20 is an isosceles one


(iii) The triangle with sides 10,15,20 is a scalene one

To find the greatest and smallest of three numbers

Question 5 : Write a program in Java to find the greatest and the smallest number among three numbers given by the user.


Java Program :


public class greatest_of_three
{
 public static void main(int a,int b,int c)
 {
     int d=0;
     int e=0;
     if (a>b && a>c)
     d=a;
     if (b>a && b>c)
     d=b;
     if (c>a && c>b)
     d=c;
     if (a<b && a<c)
     e=a;
     if (b<a && b<c)
     e=b;
     if (c<a && c<b)
     e=c;
     System.out.println("Among the inputs done i.e. "+a+","+b+","+c);
     System.out.println("The greatest number = "+d);
     System.out.println("The smallest number = "+e);
    }
}

Input : 


a=10
b=20
c=30

Output : 


Among the inputs done i.e. 10,20,30
The greatest number = 30
The smallest number = 10

Friday 1 April 2011

To calculate Simple Interest (S.I.)

Question 4 : Write a program in Java to calculate Simple Interest (Principal, Rate, & Time to be given by the user).


Java Program :


class simple_interest
{
 public static void main(int p,int r,int t)
 {
     int si;
     si=(p*r*t)/100;
     System.out.print("The Simple interest on Rs. "+p+" for "+t+" year/s at "+r+"% rate of interest = Rs. "+si);
    }
}

Input :


p=100
r=10
t=1

Output :


The Simple interest on Rs. 100 for 1 years at 10% rate of interest = Rs. 10

ShareThis