Tuesday 26 August 2014

Core Java Programming



This post is for student who study java and they need many programs in their exam and test so I am trying to make some java programs which help them to make their work easy 

Question : Write a program which accept input of full path of a file and produce following output?
Input : C:/Users/Public/Pictures/SamplePictures/Desert.jpg
Output :
Extension : jpg
File Name : Desert
Path : C:/Users/Public/Pictures/SamplePictures

Solution :

import java.util.*;
public class Main
{

       public static void main(String[] args)
    {
        String str;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter your name>> ");
        str=sc.nextLine();

        int ep,sp;
        ep=str.indexOf(".");
        sp=str.lastIndexOf("\\");
        System.out.print("\nExtention  is "+str.substring(ep+1,str.length()));
        System.out.println("\nFile Name is "+str.substring(sp+1,ep));
        System.out.println("\npath is "+str.substring(0,sp));


    }
}

Question : Write a program which accept input from user and print the word which has consecutive repeated Character?
Input  :  An apple is banana and banana is apple.

Output : apple,banana,banana,apple
import java.util.*;
public class Main
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        String str1,str2;
        char ch;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter your string ");
        str1=sc.nextLine();
        int i,start=0,j,flag;
        for(i=1;i<str1.length();i++)
        {
            ch=str1.charAt(i);
            flag=0;
            if(ch==' ')
            {
                str2=str1.substring(start,i);
                
                for(j=0;j<str2.length()-1;j++)
                {
                    if(str2.charAt(j)==str2.charAt(j+1))
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==1)
                       System.out.println(str2);
                start=i;
            }
                    }
        //for last word
        str2=str1.substring(start,i);
        flag=0;
         for(j=0;j<str2.length()-1;j++)
        {
            if(str2.charAt(j)==str2.charAt(j+1))
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
               System.out.println(str2);

    }
}
Question :                                                                                                                    
Write a program that accept two string one for main string and second string will store some word and remove this word form main string
Input : ram is a good boy
Search string : ram good
Output : is a boy

import java.util.*;
public class Main
{
  
    public static void main(String[] args)
    {
        int i,start=0,j,flag;
        String str1,str2,str3,str4="";
        char ch;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter your string>> ");
        str1=sc.nextLine();
        str4=str1;
        System.out.print("Enter your Search String>> ");
        str2=sc.nextLine();
        for(i=0;i<str2.length();i++)
        {
            if(str2.charAt(i)==' ')
            {
                str3=str2.substring(start,i);
                str1=str1.replaceAll(str3,"");
                start=i;
            }
        }
        //Getting last word from search string
        str3=str2.substring(start,str2.length());
        str1=str1.replaceAll(str3,"");
        System.out.println("Original string is >> "+str4);
        System.out.println("After remove words string is >> "+str1);
    }
}
Question : Write a program to find the following pattern
Input : Ashok Kumar Yadav
Output: A.K. Yadav
Program :
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
      String str1,str2;
      int start=0,i;
      Scanner sc=new Scanner(System.in);
      System.out.print("Enter Your Full Name>> ");
      str1=sc.nextLine();
      for(i=0;i<str1.length();i++)
      {
          if(str1.charAt(i)==' ')
          {
              str2=str1.substring(start,i).trim();
              System.out.print(str2.charAt(0)+".");
              start=i;
          }
      }
      //Printing Last Word
      System.out.print(" "+str1.substring(start,i));
    }
}

Magic Number

A Magic number said to be a magic when we add each digit until its reached less than 10 after that  if value is 1 then number is Magic.
Example :
    28 = 2+8
         =10
     10= 1+0
         =1 
Program :
 import java.util.*;
public class Main
{
  
    public static void main(String[] args)
    {
       int m,n,j;
      Scanner sc=new Scanner(System.in);
      System.out.print("Enter Number");
      m=sc.nextInt();
      while(m>0)
      {
          if(m<10)
          {
              if(m==1)
                System.out.println("Magic Number");
              else
                  System.out.println("Not A Magic Number");
              break;
          }         
          n=m;
          j=0;
          while(n>0)
          {
              j=j+(n%10);
              n=n/10;
          }
          m=j;
      }
    }
}
Question : Write a program to check whether a number is perfect or not ?
(A number said to be  perfect if sum of all factor  less than given number then equal to given number)
Input : 6
Output : Perfect Number
import java.util.*;
public class Main {
   
    public static void main(String[] args)
    {
        int n,m,sum=0;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter number>> ");
        n=sc.nextInt();
        for(m=1;m<n;m++)
        {
            if(n%m==0)
                   sum=sum+m;
        }
        if(sum==n)
            System.out.println("Number is perfect");
        else
            System.out.println("Number is Not perfect");
     }
   
}

No comments:

Post a Comment