Sunteți pe pagina 1din 50

Akash Kumar Mishra 1112010010

Page 1



Program 1

import java.lang.Math
class Squareroot
{
public static void main(String args[])
{
double x=5,y;
y=Math.sqrt(x);
System.out.print("y = " +y);
}
}

Akash Kumar Mishra 1112010010


Page 2


Program 2

class Room
{
float length,breadth;
void getdata(float a,float b)
{
length = a;
breadth = b;
}
}
class RoomArea
{
public static void main(String args[])
{
float area;
Room r1=new Room();
r1.getdata(14,10);
area = r1.length*r1.breadth;
System.out.print("Area = " +area);
}
}

Akash Kumar Mishra 1112010010


Page 3


Program 3

class ComLinetest
{
public static void main(String args[])
{
int c,i=0;
String string;
c=args.length;
System.out.print("No.of arguments = " +c);
while(i<c)
{
string = args[i];
i=i+1;
System.out.print(i + " : " + " Java is " + string + "!");
}
}
}
Akash Kumar Mishra 1112010010


Page 4


Program 4

class greatest
{
public static void main(String args[])
{
int a=325,b=712,c=478;
System.out.print("Largest value is : ");
if(a>b)
{
if(a>c)
{
System.out.print(a);
}
else
{
System.out.print(c);
}
}
else
{
if(c>b)
{
System.out.print(c);
}
Akash Kumar Mishra 1112010010


Page 5


else
{
System.out.print(b);
}
}
}
}

Akash Kumar Mishra 1112010010


Page 6


Program 5

class Room
{
int length,breadth;
Room(int x,int y)
{
length = x;
breadth = y;
}
int area()
{
return (length*breadth);
}
}
class BedRoom extends Room
{
int height;
BedRoom(int x,int y,int z)
{
super(x,y)
height = z;
}
int volume()
{
Akash Kumar Mishra 1112010010


Page 7


return(length*breadth*height)
}
}
class exe
{
public static void main(String args[])
{
BedRoom r1= new BedRoom(14,12,10);
int ar=r1.area();
int vol=r1.volume();
System.out.print("Area = " +ar);
System.out.print("Volume = " +vol);
}
}

Akash Kumar Mishra 1112010010


Page 8


Program 6

class Numsort
{
public static void main(String args[])
{
int num[] = {55,40,80,65,71};
int i,j;
int n=num.length;
System.out.print("Given list : ");
for(i=0;i<n;i++)
{
System.out.print(" " + num[i]);
}
System.out.print("\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i] > num[j])
{
temp= num[i];
num[i]= num[j];
num[j]=temp;
}
Akash Kumar Mishra 1112010010


Page 9


}
}
System.out.print("Sorted list :");
for(i=0;i<n;i++)
{
System.out.print(" " +num[i]);
}
System.out.print(" ");
}
}


Akash Kumar Mishra 1112010010

Page
10


Program 7

class stack
{
private int a[] = new int[10];
private int tos=-1;
void push(int x)
{
if(tos==9)
System.out.print("Stack is Full");
else
a[++tos]=x;
}
int pop()
{
if(tos==-1)
{System.out.print("Stack is Empty");return 0;}
else
return a[tos--];
}
}
class stackdemo
{
public static void main(String args[])
{
Akash Kumar Mishra 1112010010

Page
11


int i;
stack s1=new stack();
for(i=0;i<=9;i++)
s1.push(i);
for(i=0;i<=9;i++)
System.out.print(s1.pop());
}
}

Akash Kumar Mishra 1112010010

Page
12


Program 8

class box
{
int l,b,h;
int vol()
{
int vol;
vol=l*b*h;
return vol;
}
}
class access
{
public static void main(String args[])
{
int c;
box b1 = new box();
b1.l=10;b1.b=20;b1.h=30;
c=b1.vol();
System.out.println("vol is : "+c);
}
}

Akash Kumar Mishra 1112010010

Page
13


Program 9

class factorial
{
int fact(int n)
{
int result;
if(n==1)
return 1;
result=fact(n-1)*n;
return result;
}
}
class recursion
{
public static void main(String args[])
{
factorial f=new factorial();
System.out.println("Factorial of 3 is " +f.fact(3));
System.out.println("Factorial of 4 is " +f.fact(4));
System.out.println("Factorial of 5 is " +f.fact(5));
}
}

Akash Kumar Mishra 1112010010

Page
14


Program 10

class staticdemo
{
static int i=5;
int x;
static void callme()
{
System.out.println(i);
}
static
{
System.out.println("Static block1");
}
static
{
System.out.println("Static block2");
}
}
class staticdemo1
{
public static void main(String args[])
{
staticdemo.callme();
System.out.println(staticdemo.i);
Akash Kumar Mishra 1112010010

Page
15


}
}

Akash Kumar Mishra 1112010010

Page
16


Program 11

class stringdemo
{
public static void main(String args[])
{
String strob1="first string";
String strob2="Second string";
String strob3=strob1;
System.out.println("length of strob1:"+strob1.length());
System.out.println("Char at index 3 in strob1:"+strob1.charAt(3));
if(strob1.equals(strob2))
System.out.println("strob1==strob2");
else
System.out.println("strob1!=strob2");
if(strob1.equals(strob3))
System.out.println("strob1==strob3");
else
System.out.println("strob1!=strob3");
}
}

Akash Kumar Mishra 1112010010

Page
17


Program 12

class newpat
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<6;j++)
{
if(i==j)
continue;
else
System.out.print(j);
}
System.out.println();
}
}
}

Akash Kumar Mishra 1112010010

Page
18


Program 13
class outer
{
int out=100;
void test()
{
for(int i=0;i<10;i++)
{
class inner
{
void display()
{
System.out.println("display:out="+out);
}
}
inner in=new inner();
in.display();
}
}
}
class innerclassdemo
{
public static void main(String args[])
{
outer ot= new outer();
Akash Kumar Mishra 1112010010

Page
19


ot.test();
}
}
Program 14

class fun
{
void display()
{
System.out.println("Display1");
}
void display(int x)
{
System.out.println("Display2"+x);
}
void display(float x,float y)
{
System.out.println("Display3"+x+" "+y);
}
}
class demo1
{
public static void main(String args[])
{
fun f1=new fun();
Akash Kumar Mishra 1112010010

Page
20


f1.display();
f1.display(5,5);
}
}

Akash Kumar Mishra 1112010010

Page
21


Program 15

import java.util.Scanner;

class Add
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter two integers to calculate their sum ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = x + y;
System.out.println("Sum of entered integers = "+z);
}
}

Akash Kumar Mishra 1112010010

Page
22


Program 16
class addarry
{
public static void main(String args[])
{
int a[]=new int[10];
int s=0,i;
for(i=0;i<10;i++)
{
a[i]=i;
}
for(i=0;i<10;i++)
{
s=s+a[i];
}
System.out.print("sum of elements of array="+s);
}
}


Akash Kumar Mishra 1112010010

Page
23


Program 17
abstract class operation
{
int a;
int b;
operation(int p,int q)
{
a=p;
b=q;
System.out.println(a+" "+b);
}
abstract int result();
}
class add extends operation
{
add(int p,int q)
{
super(p , q);
}
int result()
{
System.out.println("inside add-Addition");
return(a+b);
}
}
Akash Kumar Mishra 1112010010

Page
24


class subtract extends operation
{
subtract(int p,int q)
{
super(p , q);
}
int result()
{
System.out.println("Inside subtract-Subtraction");
return(a-b);
}
}
class abstracttest
{
public static void main(String args[])
{
add a1=new add(3,2);
subtract s1=new subtract(3,2);
operation ref;
ref=a1;
System.out.println("result is"+ref.result());
ref=s1;
System.out.println("result is"+ref.result());
}
}
Akash Kumar Mishra 1112010010

Page
25




Akash Kumar Mishra 1112010010

Page
26


Program 18
class base
{
void override()
{
System.out.println("I am base class");
}
}
class derived extends base
{
void override()
{
System.out.println("I am overridden in derived class");
}
}
class test1
{
public static void main(String args[])
{
base b=new base();
b.override();
derived d=new derived();
d.override();
}
}
Akash Kumar Mishra 1112010010

Page
27




Akash Kumar Mishra 1112010010

Page
28


Program 19
class x
{
void show()
{
System.out.println("Inside x");
}
}
class y extends x
{
void show()
{
System.out.println("Inside y");
}
}
class test2
{
public static void main(String args[])
{
x x1=new x();
y y1=new y();
x ref;
ref=x1;
ref.show();
ref=y1;
Akash Kumar Mishra 1112010010

Page
29


ref.show();
}
}


Akash Kumar Mishra 1112010010

Page
30


Program 20
class conversion
{
public static void main(String args[])
{
byte b;
int i=257;
double d=323.142;
System.out.print("\nconversion of int to byte:\n");
b=(byte)i;
System.out.print("\ni and b "+i+" "+b);
System.out.print("\nconversion of double to int:\n");
i=(int)d;
System.out.print("\ni and d "+i+" "+d);
System.out.print("\nconversion of double to byte:\n");
b=(byte)d;
System.out.print("\ni and d "+i+" "+d);

}
}


Akash Kumar Mishra 1112010010

Page
31


Program 21
class shift
{
public static void main(String args[])
{
int a=5;
System.out.print(a>>2);
System.out.print("\n");
System.out.print(a<<2);
System.out.print("\n");
System.out.print(a>>>3);
}
}


Akash Kumar Mishra 1112010010

Page
32


Program 22
class multiply
{
public static void main(String args[])
{
int i,j,k=0;
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=k++;
b[i][j]=k++;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][i];
}
Akash Kumar Mishra 1112010010

Page
33


System.out.print(c[i][j]);
}
System.out.print("\n");
}

}
}


Akash Kumar Mishra 1112010010

Page
34


Program 23
class fore
{
public static void main(String args[])
{
float a[]={1.1f,1.2f,1.3f,1.4f,1.5f};
for(float i:a)
{
i=i+1;
System.out.print("\n"+i+"\n");
}
System.out.print("increment is not reflected:\n");
for(float i:a)
{
System.out.print("\n"+i+"\n");
}
}
}


Akash Kumar Mishra 1112010010

Page
35


Program 24
class evenodd
{
public static void main(String args[])
{
int x=20;
if(x%2==0)
{
System.out.print("even number");
}
else
{
System.out.print("odd number");
}
}
}


Akash Kumar Mishra 1112010010

Page
36


Program 25
class fir
{
public static void main(String args[])
{
int y=2014,c=0,i,d,a;
for(i=1;i<y;i++)
{
if(i%4==0)
{
if(i%100==0)
{
if(i%400==0)
{
c=c+1;
}
}
else
{
c=c+1;
}
}
}
d=((2013-c)*365)+(c*366)+1;
a=d%7;
Akash Kumar Mishra 1112010010

Page
37


switch(a)
{
case 0:
System.out.print("sunday");
break;
case 1:
System.out.print("monday");
break;
case 2:
System.out.print("tuesday");
break;
case 3:
System.out.print("wednesday");
break;
case 4:
System.out.print("thursday");
break;
case 5:
System.out.print("friday");
break;
case 6:
System.out.print("satruday");
break;
default:
System.out.print("invalid");
Akash Kumar Mishra 1112010010

Page
38


}}
}


Akash Kumar Mishra 1112010010

Page
39


Program 26
class abc1
{
public static void main(String args[])
{
try
{
int c=0;
int d=42/c;
}
catch(ArithmeticException e)
{
System.out.print(e);
}
}
}

Akash Kumar Mishra 1112010010

Page
40


Program 27
class conbreak
{
public static void main(String args[])
{
loop1:for(int i=0;i<100;i++)
{
System.out.println(" ");
if(i>=10)
break;
for(int j=0;j<100;j++)
{
System.out.print("\n*");
if(j==i)
continue loop1;
}
}
System.out.println("Terminated by BREAK");
}
}

Akash Kumar Mishra 1112010010

Page
41


Program 28
class FindPrime
{
public static void main(String args[])
{
int num;
boolean isPrime;
num=14;
if(num<2) isPrime=false;
else isPrime=true;
for(int i=2;i<=num/i;i++)
{
if(num%i==0)
{
isPrime=false;
break;
}
}
if(isPrime) System.out.println("prime");
else System.out.println("not prime");
}
}

Akash Kumar Mishra 1112010010

Page
42


Program 29
class varargs
{
static void fun(String...person)
{
for(String name:person)
{
System.out.print("\nthis is "+name);
}
}
public static void main(String args[])
{
fun("John","Mary","David");
}
}

Akash Kumar Mishra 1112010010

Page
43


Program 30
interface area
{
final static float pi=3.14f;
float compute(float x,float y);
}
class rect implements area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class circle implements area
{
public float compute(float x,float y)
{
return(pi*x*x);
}
}
class interfacetest
{
public static void main(String args[])
{
rect r=new rect();
circle c=new circle();
Akash Kumar Mishra 1112010010

Page
44


area a;
a=r;
System.out.print("area of rectangle="+a.compute(10,20));
a=c;
System.out.print("\narea of circle="+a.compute(10,10));
}
}

Akash Kumar Mishra 1112010010

Page
45


Program 31
class check
{
public static void main(String args[])
{
int x=5,y=6,z=7;
int a=(x>=y?(x>=z?x:z):(y>=z?y:z));
System.out.print(a);
}
}

Akash Kumar Mishra 1112010010

Page
46


Program 32
class test
{
void display(float x,int y)
{
System.out.print("method 1");
}
void display(float x,float y)
{
System.out.print("method 2");
}
}
class testpro
{
public static void main(String args[])
{
test t=new test();
t.display(5.5f,10);
}
}

Akash Kumar Mishra 1112010010

Page
47


Program 33
class pyramid
{
public static void main(String args[])
{

int i,j,k;
for(k=4;k>=1;k--)
{
for(i=4;i>=1;i--)
{
for(j=1;j<i;j++)
System.out.print(" ");
for(j=4;j>=i;j--)
if(j==k)
System.out.print(j);
else
System.out.print(" ");
for(j=i+1;j<=4;j++)
if(j==k)
System.out.print(j);
else
System.out.print(" ");
System.out.print("\n");
}
for(i=1;i<4;i++)
Akash Kumar Mishra 1112010010

Page
48


{
for(j=i;j>=1;j--)
System.out.print(" ");
for(j=4;j>i;j--)
if(j==k)
System.out.print(j);
else
System.out.print(" ");
for(j=i+2;j<=4;j++)
if(j==k)
System.out.print(j);
else
System.out.print(" ");
System.out.print("\n");
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
//no operation
}
}
}
}
Akash Kumar Mishra 1112010010

Page
49


Program 34
class twod
{
public static void main(String args[])
{
int t[][]=new int[4][];
t[0]=new int[1];
t[1]=new int[2];
t[2]=new int[3];
t[3]=new int[4];
int j,k=1;
for(int i[]:t)
{
for(int y:i)
{
for(j=0;j<y+1;j++)
{
t[y][j]=k;
System.out.print(t[y][j]+" ");
k++;
}
}
System.out.println();
}
}
}
Akash Kumar Mishra 1112010010

Page
50


Program 35
class Table
{
public static void main(String argr[])
{
int b;
for(int i=1;i<=10;i++)
{
b=5*i;
System.out.print("5x"+i);
System.out.print("="+b);
System.out.print("\n");
}
}
}