Professional Documents
Culture Documents
File
File
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:
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
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);
class construc
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: