Basic program in java
-
Upload
sudeep-singh -
Category
Education
-
view
67 -
download
1
description
Transcript of Basic program in java
B.Tech CS/3rd Year
Object Oriented Techniques Practical File
Sudeep Singh
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”) ;
}
}
♦♦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);
}
}
♦♦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); } }
♦♦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); } } }
♦♦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(" ");
}
}
}
}
♦♦ 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--)
{
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(" ");
} } }
♦♦ 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);
}
}
♦♦ 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 );
} }
♦♦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( );
}
}
♦♦ 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( );
} }
♦ 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 );
}
}
♦♦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( ); } }