Saturday, December 12, 2015

Program to implement multiple inheritance using interfaces.

interface Printable{
void print();
}

interface Showable{
void show();
}

class A implements Printable,Showable{

public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
}
class Multiple_inheritance{
public static void main(String args[]){
 A obj = new A();
obj.print();
obj.show();
 }
}
Output:

Friday, November 20, 2015

Program to demostrate final keyword.

class Base
{
final void show()
{
System.out.println("Base class Method");
}
}
class Derived extends Base
{
void show()
{
System.out.println("Derived class Method");
}
}

class FinalMethod
{
public static void main(String args[])
{
Derived obj=new Derived();
obj.show();
}
}
Output:
 error: show() in Derived cannot override show() in Base

Thursday, November 19, 2015

Program to demostrate the use of super keyword.

class Reclangle
{
double length, width,area;
Reclangle(double l, double w)
{
length=l;
width=w;
area=length*width;
System.out.println("Area of Rectangle is : "+area);
}
}
class Cuboid extends Reclangle
{
double height, volume;
Cuboid(double l,double w,double h)
{
super(l,w);
height=h;
volume=length*width*height;
System.out.println("VoIume of cuboid is : "+volume);
}
}
class Demosuper
{
public static void main(String args[])
{
Cuboid obj=new Cuboid(10,15,20);
}
}
Output:


Wednesday, November 18, 2015

Program to implement multilevel inheritance.

class Base
{
void display()
{
System.out.println("Base class Method");
}
}
class Derived1 extends Base
{
void show()
{
System.out.println("Intermediate class method");
}
}
class Derived2 extends Derived1
{
void dis()
{
System.out.println("DeIived class Method");
}
}
class Inherit_Multilevel
{
public static void main(String args[])
{
Derived2 obj=new Derived2();
obj.display();
obj.show();
obj.dis();
}
}
Output:

Program to implement single inheritance.

class Base
{
void display()
{
System.out.println("Base class Method");
}
}
class Derived extends Base
{
void show()
{
System.out.println("DeIived class Method");
}
}
class Inherit_Single
{
public static void main(String args[])
{
Derived obj=new Derived();
obj.display();
obj.show();
}
}
Output:

Program to implement Constructor Overloading.

class Volume
{
double cuboid, sphere;
Volume(double l,double w, double h)
{
cuboid=l*w*h;
System.out.println("Volume of Cuboid = "+cuboid);
}
Volume(double r)
{
sphere=(4/3.0)*3.14*r*r*r;
System.out.println("Volume of Sphere = "+sphere);
}
}
class Multiple_const
{
public static void main(String ar[])
{
Volume cuboid=new Volume(10,20,30);
Volume sphere=new Volume(2.0);
}
}
Output:

Saturday, November 7, 2015

Program of Parameterized Constructor

class Add
{
    int a,b,c;
    public Add(int x,int y)
    {   
        a=x;
        b=y;
        c=a+b;
    }
    void display()
    {
        System.out.println("Sum is = "+c);
    }
}
class ParaConst
{
    public static void main(String args[])
    {
        Add obj = new Add(10,12);
        obj.display();
    }
}

Copy Constructor

class Demo_Copy
{
    int a,b;
    public Demo_Copy(int x,int y)
    {
        a=x;
        b=y;
    }
    public Demo_Copy(Demo_Copy obj)
    {
        a=obj.a;
        b=obj.b;
    }
    void display()
    {
        System.out.println("a = "+a);
        System.out.println("b = "+b);
    }
}
class Copy_Constr
{
    public static void main(String args[])
    {
        Demo_Copy obj1 = new Demo_Copy(10,20);
        Demo_Copy obj2 = new Demo_Copy(obj1);
        obj2.display();
    }
}

Tuesday, October 27, 2015

Program to toggle string's letter.

import java.io.*;
class StrToggle
{
public static void main(String arg[])
{
int i;
System.out.println("Enter the String:");
DataInputStream d=new DataInputStream(System.in);
try
{
String str=d.readLine();
char a[]=str.toCharArray();
for(i=0;i<a.length;i++)
{
if((a[i]>='a')&&(a[i]<='z'))
{
a[i]=(char)((int)a[i]-32);
}
else if(((a[i]>='A')&&(a[i]<='Z')))
{
a[i]=(char)((int)a[i]+32);
}
}
System.out.println("After convert:");
for(i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:

Friday, October 23, 2015

Program to convert first letter of each word in string to uppercase and other in lower case.

import java.io.*;
class StrTog
{
public static void main(String arg[])
{
int i;
System.out.println("Enter the String:");
DataInputStream d=new DataInputStream(System.in);
try
{
String str=d.readLine();
char a[]=str.toCharArray();
for(i=0;i<a.length;i++)
{
if((a[i]>='a')&&(a[i]<='z'))
{
a[i]=(char)((int)a[i]-32);
}
}
for(i=1;i<a.length;i++)
{
if((a[i-1]==' ')&&((a[i]>='a')&&(a[i]<='z')))
{
a[i]=(char)((int)a[i]-32);
}

if((a[i-1]!=' ')&&((a[i]>='A')&&(a[i]<='Z')))
{
a[i]=(char)((int)a[i]+32);
}
}
System.out.println("After convert:");
for(i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:

Tuesday, October 20, 2015

Program to convert String from lowercase to uppercase.

import java.io.*;
class StrUpper
{
public static void main(String args[])
{
int i;
System.out.println("Enter the Sring:");
DataInputStream d=new DataInputStream(System.in);
   try
{
String str=d.readLine();
String uper=str.toUpperCase();
System.out.println(uper);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:

Monday, October 19, 2015

Program to convert String from lower case to upper case without using predefined method.

import java.io.*;
class Strup
{
public static void main(String args[])
{
int i;
System.out.println("Enter the Sring:");
DataInputStream d=new DataInputStream(System.in);
   try
{
String str=d.readLine();
char a[]=str.toCharArray();
for(i=0;i<a.length;i++)
{
if((a[i]>='a')&&(a[i]<='z'))
{
a[i]=(char)((int)a[i]-32);
}
}
for(i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:


Saturday, October 17, 2015

Program to find length of string using built-in string fuctiion.

import java.io.*;
class Len
{
    public static void main(String args[])
    {
        System.out.println("Enter a string:");
        DataInputStream s=new DataInputStream(System.in);
        try
        {
            String str=s.readLine();
            int le=str.length();
            System.out.println("String length="+le);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Output:

Monday, October 5, 2015

Program to find the length of String without using buit-in function.

import java.util.*;
class SLength
{
public static void main(String args[])
{
int len=0;
String str="Welcome to Java Programing";
char a[]=str.toCharArray();
for(int i=0;i<a.length;i++)
{
len++;
}
System.out.println("String Length="+len);
}
}

Monday, September 28, 2015

Transpose of Matrix

import java.io.*;
class TransMatrix
{
    public static void main(String args[])
    {
        int a[][] = new int[2][3];
        int b[][]= new int[3][2];
        int i,j;
        DataInputStream r = new DataInputStream(System.in);
        try
        {
            System.out.println("Enter the Matrix values");
            for(i=0;i<2;i++)
            {
                for(j=0;j<3;j++)
                {
                    a[i][j]=Integer.parseInt(r.readLine());
                }
            }
            System.out.println("Matrix is :");
            for(i=0;i<2;i++)
            {
                for(j=0;j<3;j++)
                {
                    System.out.print("  "+a[i][j]);
                }
                System.out.println();
            }
            System.out.println("After the Transport of Matrix");
            for(i=0;i<3;i++)
            {
                    for(j=0;j<2;j++)
                    {
                            b[i][j]=a[j][i];
                            System.out.print("  "+b[i][j]);
                    }
                    System.out.println();
            }
        }
        catch(Exception e)
        {
        }
    }
}

Thursday, September 24, 2015

Program to find a numbuer using Binary Search.

import java.io.*;
class BinarySearch
{
public static void main(String args[])
{
int item,n,first,last,i,mid;
int a[] = new int[15];
DataInputStream r = new DataInputStream(System.in);
try
{
System.out.println("Enter the size of list:");
n = Integer.parseInt(r.readLine());
first=0;
last=n-1;
System.out.println("Enter the numbers in the sorted form:");
for(i=0;i<n;i++)
{
a[i] = Integer.parseInt(r.readLine());
}
mid=(first+last)/2;
System.out.println("Enter the search value:");
item = Integer.parseInt(r.readLine());
while(first<=last)
{
if(item == a[mid])
{
System.out.println("Number is at Location "+(mid));
break;
}
else if(item>=a[mid])
{
first=mid+1;
}
else
{
last=last-1;

}
mid=(first+last)/2;
}
if(first>last)
{
System.out.println("Number is not found");
}
else
{
System.out.println("Number found successfully");
}
}
catch(Exception e)
{
System.out.println("error is occur at"+e);
}
}
}

Output:


Wednesday, September 23, 2015

write a program for BubbleSort

import java.io.*;
class BubbleSort
{
public static void main(String arg[])
{
int n,i,j,temp;
int a[] = new int[10];
DataInputStream r = new DataInputStream(System.in);
try
{
System.out.println("Enter the numbers of values");
n = Integer.parseInt(r.readLine());
System.out.println("Enter the unsorted list");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(r.readLine());
}
System.out.println("Sorted List");
for(i=n-2;i>=0;i--)
{
for(j=0 ;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j]=a[j+1];
a[j+1
]=temp;
}
}
}
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
catch(Exception e)
{
System.out.println("Error is occur at"+e);
}
}
}
S

write a program for SelectionSort

import java.io.*;
class SelectionSort
{
public static void main(String arg[])
{
int n,i,j,temp;
int a[] = new int[10];
DataInputStream r = new DataInputStream(System.in);
try
{
System.out.println("Enter the numbers of values");
n = Integer.parseInt(r.readLine());
System.out.println("Enter the unsorted list");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(r.readLine());
}
System.out.println("Sorted List");
for(i=0;i<n;i++)
{
for(j=i+1 ;j<n;j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
catch(Exception e)
{
System.out.println("Error is occur at"+e);
}
}
}

Monday, September 21, 2015

Program to add two matrix.

import java.util.*;
class Add
{
public static void main(String args[])
{
int i,j;
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
Scanner f=new Scanner(System.in);
System.out.println("Enter numbers in first matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=f.nextInt();
}
}
System.out.println("Enter numbers in Second matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=f.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("Addition of Two matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println();
}
}
}

Program to multiply two matrix.

 import java.util.*;
class Mult
{
public static void main(String args[])
{
int i,j,k;
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
Scanner f=new Scanner(System.in);
System.out.println("Enter numbers in first matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=f.nextInt();
}
}
System.out.println("Enter numbers in Second matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=f.nextInt();
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
System.out.println("Multiplication of Two matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println();
}
}
}

Thursday, September 17, 2015

Program to Search a number in the list.

import java.util.*;
class Search
{
public static void main(String args[])
{
int i,sv,flag=0;
int nums[]=new int[10];
System.out.println("Enter numbers of the List");
Scanner f=new Scanner(System.in);
for(i=0;i<10;i++)
{
nums[i]=f.nextInt();
}
System.out.println("Enter the Search Value:");
sv=f.nextInt();
for(i=0;i<10;i++)
{
if(sv==nums[i])
{
flag=1;
System.out.println("Number is at nums["+i+"]="+nums[i]);
}
}
if(flag==1)
System.out.println("Number is found");
else
System.out.println("Number is  not found");
}
}

Program to find maximum in the list

import java.util.*;
class Max
{
public static void main(String args[])
{
int i,max;
int nums[]=new int[10];
System.out.println("Enter numbers of the List");
Scanner f=new Scanner(System.in);
for(i=0;i<10;i++)
{
nums[i]=f.nextInt();
}
max=nums[0];
for(i=1;i<10;i++)
{
if(max<nums[i])
{
max=nums[i];
}
}
System.out.println("Maximum number in the List:"+max);
}
}

Program to find minmum number in list.

import java.util.*;
class Min
{
public static void main(String args[])
{
int i,min;
int nums[]=new int[10];
System.out.println("Enter numbers of the List");
Scanner f=new Scanner(System.in);
for(i=0;i<10;i++)
{
nums[i]=f.nextInt();
}
min=nums[0];
for(i=1;i<10;i++)
{
if(min>nums[i])
{
min=nums[i];
}
}
System.out.println("Minimum number in the List:"+min);
}
}

Wednesday, September 16, 2015

Program to Check Number is Palindrom or not

 import java.util.*;
 class Palindrom
 {
      public static void main(String args[])
      {
    int r,sum=0,temp;
    System.out.println("Enter a Number:");
    Scanner f=new Scanner(System.in);
    int n=f.nextInt();
    temp=n;
    while(n>0)
    {
r=n%10;
sum=(sum*10)+r;
n=n/10;
    }
    if(temp==sum)
    System.out.println("palindrome number ");
    else
    System.out.println("not palindrome");
    }
 }

Tuesday, September 15, 2015

Program to find number is Even or Odd

import java.io.*;
class EvenOrOdd
{
    public static void main(String args[])
    {
        int num;
        DataInputStream r = new DataInputStream(System.in);
        try
        {
            System.out.println("Enter the number");
            num = Integer.parseInt(r.readLine());
            if(num%2==0)
            {
                System.out.println("Number is Even");
            }
            else
            {
                System.out.println("Number is Odd");
            }
        }
        catch(Exception e)
        {
            System.out.println("Error is occur in = "+e);
        }
    }
}

Sunday, September 13, 2015

Program to check number is prime or not.

import java.io.*;
class PrimeNum
{
public static void main(String args[])
{
int num,i,flag=0;
DataInputStream r = new DataInputStream(System.in);
try
{
System.out.println("Enter the number:");
num = Integer.parseInt(r.readLine());
for(i=2;i<num;i++)
{
if(num%i==0)
{
flag = flag+1;
break;
}
}
if(flag==0)
{
System.out.println("Number is Prime");
}
else
{
System.out.println("Number is not Prime");
}
}
catch(Exception e)
{
System.out.println("error is occur in = "+e);
}
}
}

Program to find largest among three integer.


import java.io.*;
class LargestOfThreeNum
{
public static void main(String args[])
{
int num1,num2,num3;
DataInputStream r = new DataInputStream(System.in);
try
{
System.out.println("Enter the value of Num1");
num1 = Integer.parseInt(r.readLine());
System.out.println("Enter the value of Num2");
num2 = Integer.parseInt(r.readLine());
System.out.println("Enter the value of Num3");
num3 = Integer.parseInt(r.readLine());
if(num1>num2)
{
if(num1>num3)
{
System.out.println("Greater Number is = "+num1);
}
else
{
System.out.println("Greater Number is = "+num3);
}
}
else
{
if(num2>num3)
{
System.out.println("Greater Number is = "+num2);
}
else
{
System.out.println("Greater Number is = "+num3);
}
}
}
catch(Exception e)
{
System.out.println("error is occur in = "+e);
}
}
}

Program to print Fibonacci Series.

import java.util.*;
class Fibo
{
public static void main(String args[])
{
int sum=0,b=1;
System.out.println("Enter the Size of Fibonacci Series:");
Scanner f=new Scanner(System.in);
int n=f.nextInt();
System.out.println("Fibonacci Series:");
for(int i=1;i<=n;i++)
{
System.out.print("\t"+sum);
sum=sum+b;
b=sum-b;

}
}
}

Thursday, September 10, 2015

Program to find Sum of Digits

import java.io.*;
class SumOfDigits
{
    public static void main(String args[])
    {
        int num,i=0,temp,sum=0;
        DataInputStream r = new DataInputStream(System.in);
        try
        {
            System.out.println("Enter the Number");
            num = Integer.parseInt(r.readLine());
            while(i<num)
            {
                temp = num%10;
                num = num/10;
                sum = sum+temp;
            }
            System.out.println("Sum of Digits = "+sum);
        }
        catch(Exception e)
        {
            System.out.println("error is occur in"+e);
        }
    }
}

Program to print pattern decreasing and increasing order


class Pattern4
{
    public static void main(String arg[])
    {
        int i,j,r;
        for(i=1,r=5;i<=5;i++,r--)
        {            for(j=1;j<=r;j++)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        for(i=2;i<=5;i++)
        {
            for(j=1;j<=i;j++)
            {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Program to print pattern increasing and decreasing order

public class Pattern3
{
    public static void main(String args[])
    {
        int i,j,r;
        for(i=1;i<=5;i++)
        {
            for(j=1;j<=i;j++)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        for(i=1,r=5-1;i<=5-1;i++,r--)
        {
            for(j=1;j<=r;j++)
            {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Program to print Pattern decreasing order

class Pattern2
{
    public static void main(String arg[])
    {
        int i,j;
        for(i=1;i<=5;i++)
        {
            for(j=5;j>=i;j--)
            {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Program to print Pattern

public class Pattern1
{
    public static void main(String args[])
    {
        int i,j;
        for(i=1;i<=5;i++)
        {
            for(j=1;j<=i;j++)
            {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}


Wednesday, September 9, 2015

Program to find the calculations with Switch.

import java.io.*;
class ArithematicsWithSwitch
{
    public static void main(String args[])
    {
        int num1,num2,res,i;
        DataInputStream r = new DataInputStream(System.in);
        try
        {
            System.out.println("Enter the Numbers");
            System.out.println("Enter the Num 1 = ");
            num1 = Integer.parseInt(r.readLine());
            System.out.println("Enter the Num 2 = ");
            num2 = Integer.parseInt(r.readLine());
            System.out.println("Select a option:\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Modolus");
            i = Integer.parseInt(r.readLine());
            switch(i)
            {
                case 1:
                    res = num1+num2;
                    System.out.println("Sum of Num1 and Num2 is = "+res);
                    break;
                case 2:
                    res = num1-num2;
                    System.out.println("Subtraction of Num1 and Num2 is = "+res);
                    break;
                case 3:
                    res = num1*num2;
                    System.out.println("Multiplication of Num1 and Num2 is = "+res);
                    break;
                case 4:
                    res = num1/num2;
                    System.out.println("Division of Num1 and Num2 is = "+res);
                    break;
                case 5:
                    res = num1%num2;
                    System.out.println("Modolus of Num1 and Num2 is = "+res);
                    break;
                default:
                    System.out.println("Invalide Number enter");
                    break;
            }
        }
        catch(Exception e)
        {
            System.out.println("error is occur in "+e);
        }
    }
}

Tuesday, September 8, 2015

Program to check no. is Armstrong or not.

import java.util.*;
class Armstrong
{
    public static void main(String args[])
    {
        int r,s,cube,sum=0;
        System.out.println("Enter a Number:");
        Scanner a=new Scanner(System.in);
        int num =a.nextInt();
        s=num;
        while(num>0)
        {
            r=num%10;
            num=num/10;
            cube=r*r*r;
            sum=sum+cube;
        }
        if(sum==s)
        {
            System.out.println("Number is Armstrong");
        }
        else
        {
            System.out.println("Number is not Armstrong");
        }
    }
}

Program to find Factorial of a number.

import java.util.*;
class Factorial
{
public static void main(String args[])
{
int fact=1;
System.out.println("Enter a Number:");
Scanner f=new Scanner(System.in);
int num=f.nextInt();
for(int i=num;i>0;i--)
{
fact=fact*i;
}
System.out.println("Factorial="+fact);
}
}

Sunday, September 6, 2015

Program to print a series:0,6,24,60,120.

class Series
{

    public static void main(String args[])
    {
        System.out.print("Series->");
        for(int i=1;i<=5;i++)
        {
            int num=i*i*i-i;
            System.out.print(num+"\t");
        }
    }
}