Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Arithmetic operators

class op1

{
public static void main(String args[])
{
int a=10;
int b=5;
int c=a+b;
int d=a-b;
int e=a*b;
int f=a/b;
System.out.println("the addition of 10 and 5 is "+ c);
System.out.println("the addition of 10 and 5 is "+ d);
System.out.println("the addition of 10 and 5 is "+ e);
System.out.println("the addition of 10 and 5 is "+ f);
}
}
Output:

Relational operator

class op2
{
public static void main(String args[])
{
int a=10;
int b=20;
System.out.println("a is less than b " +(a<b));
System.out.println("a is greater than b " +(a>b));
System.out.println("a is less than or equal to b " +(a<=b));
System.out.println("a is greater than or equal to b " +(a>=b));
System.out.println("a is equal to b " +(a==b));
System.out.println("a is not equal to b " +(a!=b));
}
}
Output

Logical
class logical
{
public static void main(String args[])
{
int a=50;
boolean b= true;
boolean c= (a>70 && a<100);
boolean d= (a==70 || a==100);
boolean e= (!b);
System.out.println("The marks are between 70 and 100: " +c );
System.out.println("The marks are 70 or 100: " +d );
System.out.println("The reverse of boolean value : " +e );
}
}
Output

Increment decrement
class inc
{
public static void main(String args[])
{
int x=5;
int z=2;
int y=x;
int w=z;
System.out.println("The expression x++ evaluates to "+(x++));
x=y;
System.out.println("The expression x-- evaluates to "+(x--));
x=y;
System.out.println("The expression z+=(x++) evaluates to "+(z+=(x++)));
x=y;
z=w;
System.out.println("The expression z+=(x--) evaluates to "+(z+=(x--)));
x=y;
z=w;
System.out.println("The expression z+=(++x) evaluates to "+(z+=(++x)));
x=y;
z=w;
System.out.println("The expression z+=(--x) evaluates to "+(z+=(--x)));
}
}
Output;

Bitwise
class bitwise
{
public static void main(String args[])
{
int x=50;
int y=40;
int a=x;
int b=y;
System.out.println("The expression x>>1 evaluates to "+(x>>1));
x=a;
System.out.println("The expression x<<1 evaluates to "+(x<<1));
x=a;
System.out.println("The expression x>>>1 evaluates to "+(x>>>1));
x=a;
System.out.println("The expression x=~x evaluates to "+(x=~x));
x=a;
System.out.println("The expression x&y evaluates to "+(x&y));
x=a;
y=b;
System.out.println("The expression x|y evaluates to "+(x|y));
x=a;
y=b;
System.out.println("The expression x^y evaluates to "+(x^y));
}
}
Output

Ternary
class ternary
{
public static void main(String args[])
{
int x=20;
int y=25;
String result= (x>y)?x+" is greater":y+" is greater";
System.out.println(result);
}
}
Output:

Implicit type conversion


class type_imp
{
public static void main(String args[])
{
byte bval=20;
short sval=bval;
int ival=sval;
long lval=ival;
float fval=lval;
double dval=fval;
System.out.println("Byte value = "+bval);
System.out.println("Short value = "+sval);
System.out.println("Int value = "+ival);
System.out.println("Float value = "+fval);
System.out.println("Double value = "+dval);
}
}
Output

Explicit type conversion


class type_exp
{
public static void main(String args[])
{
double dval=1234567.23;
float fval=(float)dval;
long lval=(long)fval;
int ival=(int)lval;
short sval=(short)ival;
byte bval=(byte)sval;
System.out.println("Double Value = "+dval);
System.out.println("Float Value = "+fval);
System.out.println("Long Value = "+lval);
System.out.println("Int Value = "+ival);
System.out.println("Short Value = "+sval);
System.out.println("Byte Value = "+bval);
}
}
Output:
If statement
class dec_if
{
public static void main(String args[])
{
int a=20;
int b=10;
if(a>b)
{
System.out.println("a is greater than b");
}
}
}
Output

If else
class dec_else
{
public static void main(String args[])
{
int a=21;
if(a%2==0)
{
System.out.println("A is even");
}
else
{
System.out.println("A is odd");
}
}
}
Output

Else if ladder
class ladder
{
public static void main(String args[])
{
int a=60;
if(a>=90)
{
System.out.println("A grade");
}
else if(a>=80)
{
System.out.println("B grade");
}
else if(a>=70)
{
System.out.println("C grade");
}
else if(a>=60)
{
System.out.println("D grade");
}
else if(a>=50)
{
System.out.println("E grade");
}
else
{
System.out.println("F grade");
}
}
}
Output
While loop
class loopw
{
public static void main(String args[])
{
int a=1;
while(a<=10)
{
System.out.println(a);
a++;
}
}
}

Ouput

Do while loop
class loopd
{
public static void main(String args[])
{
int a=0;
do
{
a++;
System.out.println(a);
}
while(a<10);
}
}
Output:

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

Using class, objrct and command line input, print your age on screen
class Age
{
int age;
void data(int x)
{
age=x;
}
void display()
{
System.out.println("My age is "+age);
}
}
class cls
{
public static void main(String args[])
{
Age obj=new Age();
obj.data(Integer.parseInt(args[0]));
obj.display();
}
}
Output

Accessinf=g methods and variables


class var
{
int age;
int salary;
void data()
{
age=25;
salary=25000;
}
void display()
{
System.out.println("My age is "+age);
System.out.println("My salary is "+salary);
}
}
class var1
{
public static void main(String args[])
{
var obj= new var();
var obj1= new var();
obj.data();
obj.display();
obj1.age=24;
obj1.salary=30000;
obj1.display();
}
}
Output

Method overloading
class rect
{
double length;
double width;
void data(double a, double b)
{
length=a;
width=b;
}
void data(double a)
{
length=a;
width=20;
}
void show()
{
System.out.println("Length of rectangle is "+length);
System.out.println("Width of rectangle is "+width);
System.out.println("Area of rectangle is "+(length*width));
}
}
class overload
{
public static void main(String args[])
{
rect obj=new rect();
rect obj1=new rect();
obj.data(30,20);
obj.show();
obj1.data(30);
obj.show();
}
}

Constructor

class college

int year;

int phone;

int pincode;

college()

year=2018;

phone=73476;

pincode=160036;

void show()

System.out.println("Year= "+year);

System.out.println("phone number= "+phone);


System.out.println("pincode= "+pincode);

class construc

public static void main(String args[])

college obj=new college();

obj.show();

Output

Parametrized constructor
class college
{
int year;
int phone;
int pincode;
college(int a, int b, int c)
{
year=a;
phone=b;
pincode=c;
}
void show()
{
System.out.println("Year= "+year);
System.out.println("phone number= "+phone);
System.out.println("pincode= "+pincode);
}
}
class paramet
{
public static void main(String args[])
{
college obj=new college(2018,73476,160036);
obj.show();
}
}
Output:

You might also like