basic program in java

14
B.Tech CS/3 rd Year Object Oriented Techniques Practical File Sudeep Singh

Upload: sudeep-singh

Post on 02-Jul-2015

66 views

Category:

Education


1 download

DESCRIPTION

Basic program in java

TRANSCRIPT

Page 1: Basic program in java

B.Tech CS/3rd Year

Object Oriented Techniques Practical File

Sudeep Singh

Page 2: Basic program in java

Syntax Of Classes

♦♦ Simple class ♦♦

<class- keyword> <class- name>

{

<method- access specifier> <type- return/non_return> <method- name>()

{ operations }

}

♦♦ static top most class ♦♦

<static- keyword><class- keyword> <class- name>

{

<method- access specifier> <type- return/non_return> <method- name>()

{ operations }

}

♦♦ main class ♦♦

<class- keyword> <class- name>

{

public static void main (String [ ]s )

{

System.out.println(“Welcome In BHABHA GROUP OF INSTITUTION”) ;

}

}

Page 3: Basic program in java

♦♦1 program to add two number ♦♦

class sum

{

public static void main(String[]s)

{

int a,b,c;

a=Integer.parseInt(s[0]);

b=Integer.parseInt(s[1]);

c=a+b;

System.out.println("sum = "+c);

} }

♦♦2 program to multiply two number ♦♦ class multi

{

public static void main(String[]s)

{

int a,b,c;

a=Integer.parseInt(s[0]);

b=Integer.parseInt(s[1]);

c=a*b;

System.out.print("sum = "+c);

}

}

Page 4: Basic program in java

♦♦3 program to print factorial of any number ♦♦ class factorial

{

public static void main(String[]s)

{

int a,f=1;

a=Integer.parseInt(s[0]);

for(int i=a;i>=1;i--)

{ f=f*i; }

System.out.println("Factorial of n = "+f);

} }

♦♦4 program to find number is palindrom or not♦♦ class palindrom

{

public static void main(String[]s)

{

int a,b,c=0,d;

a=Integer.parseInt(s[0]);

d=a;

do

{ b=a%10;

c=c*10+b;

a=a/10; }

while(a>0);

if(d==c)

System.out.println("number is palindrom "+d);

else System.out.println("number is not palindrom "+d); } }

Page 5: Basic program in java

♦♦5 program to find out no.is Armstrong or not ♦♦ class armstrong

{

public static void main(String[]s)

{

int a,b,c=0,d;

a=Integer.parseInt(s[0]);

{

d=a;

do

{ b=a%10;

c=c+b*b*b;

a=a/10; }

while(a>0);

if(d==c)

System.out.println("number is armstrong "+d);

else System.out.println("number is not armstrong "+d); } } }

♦♦6 program to find out even & odd number ♦♦ class number

{

public static void main(String[]s)

{

int a,b;

a=Integer.parseInt(s[0]);

b=Integer.parseInt(s[1]);

for(int i=a;i<=b;i++)

{ if(i%2==0)

System.out.println("Number is even = "+i);

else System.out.println("Number is odd = "+i); } } }

Page 6: Basic program in java

♦♦7 program to print the table from “x” to “y”♦♦ class table

{

public static void main(String []s)

{

int a,b;

a=Integer.parseInt(s[0]);

b=Integer.parseInt(s[1]);

if(a<=b)

{

for(int i=a;i<=b;i++)

{

for(int j=1;j<=10;j++)

{

System.out.print(j*i+" ");

}

System.out.println(" ");

}}

else

{

for(int i=b;i<=a;i++)

{

for(int j=1;j<=10;j++)

{

System.out.print(j*i+" ");

}

System.out.println(" ");

}

}

}

}

Page 7: Basic program in java

♦♦ 8 program to print no. in various format ♦♦ class print

{

public static void main(String[]s)

{

int a,b;

a=Integer.parseInt(s[0]);

b=Integer.parseInt(s[1]);

for(int i=a;i<=b;i++)

{

for(int j=a;j<=i;j++)

{

System.out.print(" "+j);

}

System.out.println(" ");

} System.out.println(" ");

for(int i=b;i>=a;i--)

{

for(int j=a;j<=i;j++)

{

System.out.print(" "+j);

}

System.out.println(" ");

}

System.out.println(" ");

for(int i=b;i>=a;i--)

{

for(int j=b;j>=i;j--)

{

System.out.print(" "+j);

}

System.out.println(" ");

}

System.out.println(" ");

for(int i=a;i<=b;i++)

{

for(int j=b;j>=i;j--)

{

System.out.print(" "+j);

}

System.out.println(" ");

}

System.out.println(" ");

for(int i=a;i<=b;i++)

{

for(int j=i;j>=a;j--)

{

Page 8: Basic program in java

System.out.print(" "+j);

}

System.out.println(" ");

} System.out.println(" ");

for(int i=b;i>=a;i--)

{

for(int j=i;j>=a;j--)

{

System.out.print(" "+j);

} System.out.println(" ");

} } }

Page 9: Basic program in java

♦♦ 9 program Of Overriding ♦♦

class x

{

public void test(int a,int b )

{

int c= a + b ;

System.out.println("from class x sum ="+c);

}

}

class y extends x

{

public void test(int a,int b )

{

int c= a * b ;

System.out.println("from class y multiplication ="+c);

}

}

class overriding

{

public static void main(String [ ]s)

{

int a,b;

a=Integer.parseInt(s[0]);

b=Integer.parseInt(s[1]);

y obj=new y( );

obj.test(a,b);

}

}

Page 10: Basic program in java

♦♦ 10 program Of Overloading ♦♦ class x

{

public void test(int a, int b )

{

int c = a * b ;

System.out.println("multiplication of number = "+c);

} }

class y

{

public void test(int a, int b, int d )

{

int c= a + b + d ;

System.out.println("sum of given number = "+c);

} }

class overloading

{

public static void main (String [ ] s)

{

int a=Integer.parseInt(s[0]);

int b=Integer.parseInt(s[1]);

int d=Integer.parseInt(s[2]);

x obj = new x( );

obj.test(a ,b );

y obj1 = new y( );

obj1.test(a ,b ,d );

} }

Page 11: Basic program in java

♦♦11 Communication of class by creating object♦♦

class A

{

public void show( )

{

System.out.println("welcome from A");

}

}

class B

{

public void show1( )

{

A obj=new A( );

obj.show( );

}

}

class C

{

public static void main (String [ ] s)

{

B obj=new B( );

obj.show1( );

}

}

Page 12: Basic program in java

♦♦ 12 Communication of class by inheritance ♦♦ class A

{

public void show( )

{

System.out.println("welcome from A");

} }

class B extends A

{

public void show1( )

{

System.out.println("welcome from B");

} }

class D

{

public static void main (String [ ] s)

{

B obj = new B( );

obj.show( );

} }

Page 13: Basic program in java

♦ 13 Area of circle & rectangle using object ♦

class circle

{

public void area_c(int r )

{

int ar;

ar=r*r*22/7;

System.out.println("Area of circle= "+ar);

}

}

class rect

{

public void area_rec(int le, int br )

{

int ar;

ar=le*br;

System.out.println("Area of rectangle= "+ar);

}

}

class area

{

public static void main (String [ ] s)

{

int r=Integer.parseInt(s[0]);

int le=Integer.parseInt(s[1]);

int br=Integer.parseInt(s[2]);

circle obj=new circle( );

obj.area_c(r ); rect obj1=new rect( );

obj1.area_rec(le, br );

}

}

Page 14: Basic program in java

♦♦14 use of abstract class ♦♦

abstract class test

{

abstract public void show( );

{ }

}

class help extends test

{

public void show( )

{

System.out.println(" from class help ");

} }

class take extends test

{

public void show( )

{

System.out.println(" from class take ");

} }

class give extends test

{

public void show( )

{

System.out.println(" from class give ");

} }

class abstractdemo

{

public static void main (String [ ] s)

{

help obj=new help( );

obj.show( );

take obj1=new take( );

obj1.show( );

give obj2=new give( );

obj2.show( ); } }