This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
// WAP to implement bitwise operators. class Bitwise
{
public static void main(String arg[])
{
int a=3;
int b=6;
int c=a|b;
int d=a&b;
int e=a^b;
int f=(~a & b)|(a & ~b);
int g=~a & 0x0f;
System.out.println(" a=" +a);
System.out.println(" b=" +b);
System.out.println(" a|b=" +c);
System.out.println(" a&b=" +d);
System.out.println(" a^b=" +e);
System.out.println("(~a & b)|(a & ~b)=" +f);
System.out.println(" ~a & 0x0f=" +g);
}
}
OUTPUT /*
C:\sinny>javac Bitwise.java
C:\sinny>java Bitwise
a=3
b=6
a|b=7
a&b=2
a^b=5
(~a & b)|(a & ~b)=5
~a & 0x0f=12
*/
2
// WAP to implement arithmetic operator class Arithmetic
{
public static void main(String arg[])
{
//system.out.println("Integer Arithmetic");
int a=1+1;
int b= a*3;
int c= b/4;
int d= c-a;
int e= -d;
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
System.out.println("d=" + d);
System.out.println("e=" + e);
}
}
OUTPUT /*
C:\sinny>javac Arithmetic.java
C:\sinny>java Arithmetic
a=2
b=6
c=1
d=-1
e=1 */
3
// WAP to implement conditional operator class conditional
{
public static void main(String arg[])
{
int i,z;
i=10;
z= i<0 ? -i :i; // get absolute value of i
System.out.print("absolute vale of");
System.out.println( i +"is" + z);
i=-10;
z= i<0 ? -i :i; // get absolute value of i
System.out.print("absolute vale of");
System.out.println( i +"is" + z);
}
}
OUTPUT /*
C:\sinny>javac conditional.java
C:\sinny>java conditional
absolute vale of10is10
absolute vale of-10is10
*/
4
//WAP to implement constructor overload class box
{
double width;
double height;
double depth;
box(double w, double h, double d)
{
w=width;
h=height;
d=depth;
}
box()
{
width=-1;
height=-1;
depth=-1;
}
box(double len)
{
width=height=depth=len;
}
double volume()
{
return width*height*depth;
}
}
class overload
{
public static void main(String arg[])
{
box mybox1 = new box(10,20,30);
box mybox2 = new box();
5
box mycube = new box(7);
double vol;
vol=mybox1.volume();
System.out.println("volume of my box1 is " +vol);
vol=mybox2.volume();
System.out.println("volume of my box2 is " +vol);
vol= mycube.volume();
System.out.println("volume of mycube is " +vol);
}
}
OUTPUT /*
C:\sinny>javac overload.java
C:\sinny>java overload
volume of my box1 is 0.0
volume of my box2 is -1.0
volume of mycube is 343.0
*/
6
// Prefix increment and postfix increment operators.
public class Increment
{
public static void main( String args[] )
{
int c=5; // assign 5 to c
System.out.println( c ); // prints 5
System.out.println( c++ ); // prints 5 then postincrements
System.out.println( c ); // prints 6
System.out.println(); // skip a line
// demonstrate prefix increment operator
c = 5; // assign 5 to c
System.out.println( c ); // prints 5
System.out.println( ++c ); // preincrements then prints 6
System.out.println( c ); // prints 6
} // end main
} // end class Increment
/*
Output : C:\sinny>javac Increment.java
C:\sinny>java Increment
5
5
6
5
6
6*/
7
// Prefix decrement and postfix decrement operators.
public class decrement
{
public static void main( String args[] )
{
int c=7; // assign 7 to c
System.out.println( c ); // prints 5
System.out.println( c-- ); // prints 5 then postdecrements
System.out.println( c ); // prints 6
System.out.println(); // skip a line
// demonstrate prefix decrement operator
c = 7; // assign 7 to c
System.out.println( c ); // prints 5
System.out.println( --c ); // predecrements then prints 6
System.out.println( c ); // prints 6
} // end main
} // end class decrement
OUTPUT /*
C:\sinny>javac decrement.java
C:\sinny>java decrement
7
7
6
7
6
6 */
8
// WAP to implement method overloading. class overloadDemo
{
void test()
{
System.out.println("Function without parameter");
}
void test(int a) //overload test for one integer parameter.
{
System.out.println("a: "+ a);
}
void test(int a, int b) //overload test for two integer parameter.
{
System.out.println("a and b: " +a + " " + b);
}
double test(double a) //overload test for one double parameter.
{
System.out.println("double a: " +a);
return a*a;
}
}
class overloadmethd
{
public static void main(String args[])
{
overloadDemo ob = new overloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10,20);
result = ob.test(123.25);
System.out.println("result of ob.test(123.25):" + result);
}
}
9
// Output….
/*
C:\sinny>javac overloadmethd.java
C:\sinny>java overloadmethd
Function without parameter
a: 10
a and b: 10 20
double a: 123.25
result of ob.test(123.25):15190.5625
*/
10
// WAP to implement relational operator
import java.util.Scanner;
class evenodd
{
public static void main(String args[])
{
int i=1,n;
Scanner s1=new Scanner(System.in);
System.out.println("Enter the limit for Number :");
n=s1.nextInt();
System.out.println("ODD AND EVEN NUMBERS");
while(i<=n) //relational operator
{
if(i%2==0) //relational operator
{
System.out.println("i = " + i);
}
else
{
System.out.print("i = " + i);
System.out.print(" ");
}
i++;
}
}
}
11
/*
OUTPUT C:\sinny>javac evenodd.java
C:\sinny>java evenodd
Enter the limit for
20
ODD AND EVEN NUMBER
i = 1 i = 2
i = 3 i = 4
i = 5 i = 6
i = 7 i = 8
i = 9 i = 10
i = 11 i = 12
i = 13 i = 14
i = 15 i = 16
i = 17 i = 18
i = 19 i = 20
*/
12
// WAP to implement super keyword class Super
{
int x,y;
Super(int a,int b)
{
x=a;
y=b;
}
int area()
{
return x*y;
}
}
class Sub extends Super
{
int z;
Sub(int i,int j,int k)
{
super(i,j);
z=k;
}
int vol()
{
return x*y*z;
}
}
class demosuper
{
public static void main(String args[])
{
Sub s1=new Sub(10,10,20);
System.out.println("Area: " +s1.area());
System.out.println("Volume: " +s1.vol());
}
13
}
OUTPUT
/*
C:\sinny>javac demosuper.java
C:\sinny>java demosuper
Area: 100
Volume: 2000
*/
14
// WAP A PROGRAM TO SHOW PATTERN
class pattern
{
int i ;
int j ;
void display()
{
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
System.out.print( j ) ;
}
System.out.println(" ") ;
}
}
public static void main(String args[])
{
pattern p = new pattern() ;
p.display();
}
}
15
OUTPUT
/*
C:\sinny>javac pattern.java
C:\sinny>java pattern
1
12
123
1234
12345
*/
16
// WAP A PROGRAM TO SHOW PATTERN
class pattern1
{
int i ;
int j ;
void display()
{
for(i=5; i>=0; i--)
{
for(j=i; j<=5; j++)
{
System.out.print( j ) ;
}
System.out.println() ;
}
}
public static void main(String args[])
{
pattern1 p = new pattern1() ;
p.display();
}
}
17
OUTPUT
/*
C:\sinny>javac pattern1.java
C:\sinny>java pattern1
5
45
345
2345
12345
012345
*/
18
Practical file
Of
JAVA
Submitted To : Submitted By :
Mr. Jainendra Name : Pavleen singh BCA – Deptt. Roll No :02021202011