0% found this document useful (0 votes)
3 views35 pages

Operators Vsics

The document provides an overview of various operators in Java, including arithmetic, assignment, increment/decrement, relational, conditional, logical, and bitwise operators, along with examples of their usage. It also covers control statements such as selection, iterative, and jump statements, with code examples for if-else, nested if-else, switch-case, and loops. Additionally, it discusses arrays, including one-dimensional and two-dimensional arrays, and introduces sorting algorithms like bubble sort.

Uploaded by

Shikha Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views35 pages

Operators Vsics

The document provides an overview of various operators in Java, including arithmetic, assignment, increment/decrement, relational, conditional, logical, and bitwise operators, along with examples of their usage. It also covers control statements such as selection, iterative, and jump statements, with code examples for if-else, nested if-else, switch-case, and loops. Additionally, it discusses arrays, including one-dimensional and two-dimensional arrays, and introduces sorting algorithms like bubble sort.

Uploaded by

Shikha Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

OPERATORS

 Airthmetic
 Assignment
 Increment/decrement
 Relational
 Conditional/Ternary
 Logical Op
 Bitwise

1. Airthmetic Operator (+,-,*,/)

import java.util.*;
class AirthmeticExample
{
public static void main(String args[])
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter two numbers ”);
a=sc.nextInt();
b=sc.nextInt();
System.out.println(“Addition is ”+(a+b));
System.out.println(“Multi is ”+(a*b));
System.out.println(“Sub is ”+(a-b));
System.out.println(“Division is ”+(a/b));
}
}
2. Assignment Operator (=,+=,-=)

import java.util.*;
class AssignmentExample
{
public static void main(String args[])
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter two numbers ”);
a=sc.nextInt();
b=sc.nextInt();
c=a+b;
System.out.println(“Addition is ”+c);
}
}

3. Increment/Decrement Op

Let, int a=5;

Pre-increment Operator
++var_name
++a; …………//6
Pre-decrement Operator
--var_name
--a;………………//4
Post-increment Operator
Var_name++
a++;…………5
a;……………..6
Post-decrement Operator
var_name—
a-- ;…………5
a;………..4
class IncreDecreExample
{
public static void main(String args[])
{
int a=5;
System.out.println(“value of a is ”+a);
System.out.println(“value of a after preincrement is ”+(++a));
System.out.println(“value of a after post increment is ”+(a++));
System.out.println(“value of a is ”+a);
System.out.println(“value of a after pre decrement is ”+(--a));
System.out.println(“value of a after post decrement is ”+(a--));
System.out.println(“value of a is ”+a);
}
}

4. Relational Operator

>
>=
<
<=
==
!=

import java.util.*;
class RelationalExample
{
public static void main(String args[])
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter two numbers ”);
a=sc.nextInt();
b=sc.nextInt();
System.out.println(a>b);
System.out.println(a>=b);
System.out.println(a<b);
System.out.println(a<=b);
System.out.println(a==b);
System.out.println(a!=b);
}
}

5. Conditional Operator/TernaryOp

Syntax: (condition)?True:false;

import java.util.*;
class ConditionalExample
{
public static void main(String args[])
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter two numbers ”);
a=sc.nextInt(); //6
b=sc.nextInt(); //3
c=(a<b)?a:b;
System.out.println(“Result is ”+c);
}
}

6. Logical Operator

AND op(&&)
0 0 0
0 1 0
1 0 0
1 1 1

OR op( || )
0 0 0
0 1 1
1 0 1
1 1 1

NOT op( ! )
0 1
1 0

import java.util.*;
class LogicalExample
{
public static void main(String args[])
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter three numbers ”);
a=sc.nextInt(); //6
b=sc.nextInt(); //3
c=sc.nextInt(); //9
System.out.println((a>b)&&(a>c));
System.out.println((a>b)||(a>c));
}
}

7. Bitwise Operator

a) Bitwise AND ( & )


b) Bitwise OR ( | )
c) Bitwise XOR ( ^ )
d) Bitwise Compliments ( ~ )
e) Bitwise LeftShift ( << )
f) Bitwise RightShift ( >> )

X Y AND(&) OR( | ) XOR ( ^ )

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0

import java.util.*;
class LogicalExample
{
public static void main(String args[])
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter two numbers ”);
a=sc.nextInt(); //10
b=sc.nextInt(); //7
System.out.println(a&b);
System.out.println(a|b);
System.out.println(a^b);
System.out.println(~a);
}
}

~a=10 -11 1 1 1 1 0 1 0 1
a=10 x 0 0 0 0 1 0 1 0
b=7 y 0 0 0 0 0 1 1 1
a&b 2 0 0 0 0 0 0 1 0
a|b 15 0 0 0 0 1 1 1 1
a^b 13 0 0 0 0 1 1 0 1

Control Statement

1. Selection Statement
a) if-else Statement
b) nested if-else Statement
c) if-else if-else Statement
d) switch-case Statement
2. Iterative/Looping Statement
a) While lopp
b) Do-while loop
c) For loop
3. Jump Statement
a) Break
b) Continue
Selection Statement

1. if else Statement
Syntax:

if(condition)
{
……………
……………
}
else
{
…………..
…………….
}

import java.util.*;
class IfelseExample
{
public static void main(String args[])
{
int a;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a number ”);
a=sc.nextInt();
if(a>=0)
{
System.out.println(“positive”);
}
else
{
System.out.println(“negative”);
}
}
}
Nested if-else Statement
Syntax:

if(condition1)
{
if(condition2)
{
……………
}
else
{
…………….
}
}
else
{
if(condition3)
{
……………
}
else
{
…………….
}
}

import java.util.*;
class NestedIfelseExample
{
public static void main(String args[])
{
int a,b,c,big;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a number ”);
a=sc.nextInt(); //10
b=sc.nextInt(); //5
c=sc.nextInt(); //15
if(a>b)
{
if(a>c)
{
big=a;
}
else
{
big=c;
}
}
else
{
if(b>c)
{
big=b;
}
else
{
big=c;
}

}
System.out.println(“biggest value is ”+big);
}
}

if else if else ladder Statement

Syntax:

if(condition)
{
……..
}
else if(condition)
{
……..
}
else if(condition)
{
……..
}
else if(condition)
{
……..
}
.
.
else
{
…………
}

import java.util.*;
class LadderIfelseif
{
public static void main(String args[])
{
int m1,m2,m3,m4,per;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter Student 4 subject marks ”);
m1=sc.nextInt();
m2=sc.nextInt();
m3=sc.nextInt();
m4=sc.nextInt();
per=(m1+m2+m3+m4)/4;
if(per>=75)
{
System.out.println(“HONOURS”);
}
else if(per>=60)
{
System.out.println(“FIRST”);
}
else if(per>=50)
{
System.out.println(“SECOND”);
}
else if(per>=40)
{
System.out.println(“THIRD”);
}
else
{
System.out.println(“FAILED”);
}
}
}

Switch-Case Statement

Syntax:

switch(choice)
{
case int/char:
……..
break;
case int/char:
……..
break;
case int/char:
……..
break;



default:
……….
………..
}

Example

import java.util.*;
class SwitchCase
{
public static void main(String args[])
{
int a,b;
char op;
Scanner sc=new Scanner(System.in);
System.out.println(“enter number in a ”);
a=sc.nextInt();
System.out.println(“enter number in b ”);
b=sc.nextInt();
System.out.println(“enter operator ”);
op=sc.next().charAt(0);
switch(op)
{
case ‘+’:
System.out.println(“Addition is ”+(a+b));
break;
case ‘-’:
System.out.println(“Substraction is ”+(a-b));
break;
case ‘*’:
System.out.println(“Multiplication is ”+(a*b));
break;
case ‘/’:
System.out.println(“Division is ”+(a/b));
break;
default:
System.out.println(“Invalid Operator ”);
break;
}
}
}

Iterative/looping Statements
while loop do while loop for loop
Syntax: Syntax: Syntax:

initialization initialization for(initi ; condi ; incre/dec)


while(condition) do {
{ { …..
….. ….. …..
….. ….. }
Incre/decre; Incre/decre;
} }
while(condition);
Example code: Example code: Example code:

class Demo class Demo class Demo


{ { {
PSVM PSVM PSVM
{ { {
int i=1; //init int i=1; int i=1;
do for(i=1;i<=10;i++)
while(i<=10) { {
{ SOP(i); SOP(i);
SOP(i); i++; }
i++; } }
} while(i<=10); }
} }
} }

 Table of any number


 Factorial of any number
 Enter a number and check number is prime or not
 Fabonacci Series
 Sum of Digits
 Product of digits
 Enter a number and find number is Armstrong or not
 Enter a number and find number is pallindrome or not

Nested for loop


Syntax:

for( initilization ; condition ; incre/decrement ) //row


{
for( initilization ; condition ; incre/decrement ) //col
{

}
}
Program Example

class Demo
{
public static void main(String args[])
{
int i,j;
for(i=1 ; i<=5 ; i++)
{
for(j=1 ; j<=5 ; j++)
{
System.out.println( j );
}
System.out.println();
}
}
}

Col 1 2 3 4 5
Row
1 1,1 1,2 1,3 1,4 1,5

2 2,1 2,2 2,3 2,4 2,5


3 3,1 3,2 3,3 3,4 3,5
4 4,1 4,2 4,3 4,4 4,5
5 5,1 5,2 5,3 5,4 5,5

PATTERN QUESTIONS

1. 2. 3. 4.

1 1 * *
12 22 ** **
123 333 *** ***
1234 4444 **** ****
12345 55555 ***** *****
5. 6. 7. 8.
1 * ***** *
01 ** **** ***
101 *** *** *****
0101 **** ** *******
10101 ***** * *********
9.

1
232
34543
4567654
567898765

class Patt2
{
public static void main(String args[])
{
int i,j,p;
for(i=1 ; i<=5 ; i++)
{
for(j=1 ; j<=5-i ; j++)
{
System.out.print(" ");
}
p=i;
for(j=1 ; j<=i ; j++)
{
System.out.print(p++);
}
p=p-2;
for(j=1 ; j<=i-1; j++)
{
System.out.print(p--);
}
System.out.println();
}
}
}

Array in Java
User define datatype which is use to store similar type of dataitems .
Types of array
 1D array
 2D array
 Multi or 3D array

1D Array

Syntax:

1.Array Dec:

Datatype arrayname[];

2.Array Defi

Arrayname[]=new Datatype[size];

3.Array value

Arrayname[]={value1,value 2,value 3……….};

Datatype arrayname[]=new Datatype[size];


Eg:

int a[]=new int[5];

import java.util.*;

class OneDArray

public static void main(String args[])


{

int a[]=new int[10];

int i,even=0,odd=0,sum=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter 10 numbers\n");

for(i=0;i<10;i++)

a[i]=sc.nextInt();

System.out.println("********ARRAY ELEMENTS ARE **********\n");

for(i=0;i<10;i++)

System.out.print(a[i]+"\t");

sum=sum+a[i];

if(a[i]%2==0)

even++;

else

odd++;

System.out.println("\n\
n_____________________________________________________________________\n");

System.out.println(even+" elements are even");

System.out.println(odd+" elements are odd");

System.out.println("Sum of all array elements in array is "+sum);

} }

2D Array in Java
Syntax:

Datatype arrayname[][]=new Datatype[r][c];

Eg:

int a[][]=new a[3][3];

a[][]={{22,33,44},{13,54,35},{87,76,54}};

import java.util.*;

class TwoDArray

public static void main(String args[])

int a[][]=new int[3][3];

int i,j;

Scanner sc=new Scanner(System.in);

System.out.println("Enter 9 numbers\n");

for(i=0;i<3;i++)

for(j=0;j<3;j++)

a[i][j]=sc.nextInt();

System.out.println("********ARRAY ELEMENTS ARE **********\n");

for(i=0;i<3;i++)

for(j=0;j<3;j++)

{
System.out.print(a[i][j]+"\t");

System.out.println();

1 2 3 1 1 3 2
4 5 6 5 5 4 6
7 8 9 9 7 9 8

1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9

Searching & sorting in an Array in java

 Bubble sort
 Selection sort
 Insertition sort
 Quik sort
 Merge sort
 Bucket sort
 Heap sort

Bubble sort

import java.util.*;
class Bubble_sort
{
public static void main(String args[])
{
int a[]=new int[5];
int i,temp;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 5 number ");
for(i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Elements are :");
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
for(i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Elements after sorting are :");
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}

Selection Sort

Example:

import java.util.*;
class Selection_sort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[]=new int[5];
int i ,j ,temp;
System.out.println("Enter five elements ");
for(i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Elements are ");
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Elements after selection sort are ");
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}

Insertition Sort
Example:

import java.util.*;
class Insert_sort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[]=new int[5];
int i ,j ,k ,temp;
System.out.println("Enter five elements ");
for(i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Elements are ");
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[j];
for(k=j;k>i;k--)
{
a[k]=a[k-1];
}
a[k]=temp;
}
}
}
System.out.println("Elements after selection sort are ");
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}
Searching in an array :

 Linear Search
 Binary Search

Linear Search

Example

import java.util.*;
class LiSearch
{
public static void main(String args[])
{
int a[]=new int[10];
int i , ele , flag=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter elements ");
for(i=0 ; i<10 ; i++)
{
a[i]=sc.nextInt();
}
System.out.println("Array elements are ");
for(i=0 ; i<10 ; i++)
{
System.out.println(a[i]);
}
System.out.println("Enter a number to be search ");
ele=sc.nextInt();
for(i=0 ;i<10 ;i++)
{
if(ele==a[i])
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("Element found "+ele);
else
System.out.println("Element not found "+ele);
}
}

Binary Search

Example:

import java.util.*;
class Bin_search
{
public static void main(String args[])
{
int a[]=new int[10];
int i , flag=0 ,ele ,u=9 ,l=0,m ;
Scanner sc= new Scanner(System.in);
System.out.println("Enter 10 elements ");
for(i=0 ; i<10 ; i++)
{
a[i]=sc.nextInt();
}
System.out.println("Elements are ");
for(i=0 ; i<10 ; i++)
{
System.out.println(a[i]);
}
System.out.println("Enter element to be search");
ele=sc.nextInt();
while(l<=u)
{
m=(l+u)/2;
if(ele==a[m])
{
flag=1;
break;
}
else if(ele>a[m])
{
l=m+1;
}
else
{
u=m-1;
}
}
if(flag==1)
System.out.println("Element Found ");
else
System.out.println("Element not found");
}
}
OOPS CONCEPTS IN JAVA

class and Object : class is place where we can stores the functions and attributeof an
object whereas object is a blue print of a class.

Animal---------class

 DOG-------object
 CAT-------object
 RAT -------object

Syntax:

Access_specifier class class_name


{
Field decelearation/variables;
Methods
constructors
}

Field decelearation/variables in class


Syntax:
Access specifier datatype field_name=value;
Eg:
int roll=101;
String name=”puneet”;

For access :

Objectname.feildname;
obj.roll;
obj.name

Example code :

class Student
{
String name;
int roll;
double per;
}
class Main_Oops_Ex
{
public static void main(String args[])
{
Student stu=new Student();
stu.name="PUNEET";
stu.roll=101;
stu.per=98.87;
System.out.println("Student name is "+stu.name);
System.out.println("Student roll no is "+stu.roll);
System.out.println("Student percentage is "+stu.per);
}
}

Method in classes
Syntax:

Access_specifier return_type method_name(Arguments)


{
…………..
…………..
…………..
}

import java.util.*;

class Employee
{
String name;
int id;
void emp_detail(String n , int i)
{
name=n;
id=i;
System.out.println("Employe name is "+name);
System.out.println("Employe id is "+id);
}
}
class Main_emp
{
public static void main(String args[])
{
String n;
int i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter employe name ");
n=sc.next();
System.out.println("Enter employe id ");
i=sc.nextInt();
Employee emp=new Employee();
emp.emp_detail(n,i);
}
}

Constructor In Java
Constructor is a special type of method which is used to instansiate the class
member to their default value.
Constructor automatically invoke at the time of object creation of a class.

Note # Constructor name should be same as class name


There are two type of constructor

1. Default Constructor
2. Parameterized Constructor

Default Constructor
Syntax :

Constructor_name()
{
……….
………
……..
}

Example :

class Demo
{
int a,b;
Demo()
{
System.out.println("Default constructor of Demo class invoked ");
}
void add(int x,int y)
{
a=x; b=y;
int c=a+b;
System.out.println("Addition of a and b is "+c);
}
}
class Default_cons
{
public static void main(String args[])
{
Demo obj= new Demo();
obj.add(45,89);
}
}

Parameterized Constructor

Constructor_name(Arguments/Parameters)
{
……….
………
……..
}

Example

import java.util.*;
class Demo
{
int a,b;
Demo(int x ,int y)
{
a=x;
b=y;
}
void add()
{
int c=a+b;
System.out.println("Addition of a and b is "+c);
}
}
class Parameterized_cons
{
public static void main(String args[])
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number in a");
a=sc.nextInt();
System.out.println("Enter number in b");
b=sc.nextInt();
Demo obj= new Demo(a,b);
obj.add();
}
}

this keyword in java

this is a reference keyword which is used to refer class members.


 this can be used to refer class variables.
 this can be used to refer class methods.
 this() can be used to refer constructor.

Example

class Student
{
String name;
int roll;
double per;
Student(String name ,int roll ,double per)
{
this.name=name;
this.roll=roll;
this.per=per;
}
void disp()
{
System.out.println("Student name is "+this.name);
System.out.println("Student roll is "+this.roll);
System.out.println("Student per is "+this.per);
}
}
class This_variable
{
public static void main(String args[])
{
Student stu=new Student("puneet" ,101 ,87.98);
stu.disp();
}
}
Example 2

class Employee
{
String name;
int id;
Double salary;
void showData()
{
System.out.println("Employee name is "+name);
System.out.println("Employee id is "+id);
System.out.println("Employee salary is "+salary);
}
void getData(String n ,int i ,double s)
{
name=n;
id=i;
salary=s;
this.showData();
}
}
class This_method
{
public static void main(String args[])
{
Employee emp=new Employee();
emp.getData("Puneet" , 101 , 87000.67);
}
}

Example 3

class Rectangle
{
int len ,br;
Rectangle()
{
System.out.println("Default Constructor of Rectangle class invoked");
}
Rectangle(int l ,int b)
{
this();
len=l;
br=b;
}
void area()
{
int res=len*br ;
System.out.println("Area of rectangle is "+res);
}
}
class This_constructor
{
public static void main(String args[])
{
Rectangle rect=new Rectangle(60,120);
rect.area();
}
}

Method Overloading in java

Is also know as Compile time polymorphism(poly+ morphs).

Things to remember

1. Number of arguments
2. Type of arguments
3. Sequence of arguments

Number of arguments

void disp(int x)

………..

void disp(int x,int y)

………..
}

Obj.disp(5);

Obj.disp(7,9);

Type of arguments

void disp(int x ,int y)

………..

void disp(float x,float y)

………..

Obj.disp(5.87f ,7.12f);

Obj.disp(7,9);

Sequence of arguments

void disp(int x float y)

………..

void disp(float x,int y)

………..

Obj.disp(5.87f ,7);

Obj.disp(7,9.87f);
Example

class Demo
{
int a,b;
double c;
void calc(int x)
{
a=x;
System.out.println("----------------------------------------------------");
System.out.println("Square of a is "+(a*a));
}
void calc(int x ,int y)
{
a=x;
b=y;
System.out.println("----------------------------------------------------");
System.out.println("Area of rectangle is "+(a*b));
}
void calc(int x ,double y)
{
a=x;
c=y;
System.out.println("----------------------------------------------------");
System.out.println("Addition is "+(a+c));
System.out.println("Substraction is "+(a-c));
System.out.println("Multiplication is "+(a*c));
System.out.println("Division is "+(a/c));
}
}
class Main_methodOverloading
{
public static void main(String args[])
{
Demo obj=new Demo();
obj.calc(5);
obj.calc(20,10);
obj.calc(50,12.76);
}
}
Inheritance in Java

There are 5 types of Inheritance in java .

1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchial Inheritance
5. Hybrid Inheritance

You might also like