Search This Blog

Sunday 27 April 2014

Java program to create a vector and add elements at appropriate locations.

import java.util.*;
import java.io.*;
import java.lang.String;

class Vector1
{
    public static void main(String args[])
    {
        try
        {
       
            int length = args.length;
            int i,ch;
            String str1,str2;

            Vector list = new Vector();

            for( i=0;i<length;i++)
            {
                list.addElement(args[i]);
            }

            DataInputStream in= new DataInputStream(System.in);
           
            for(;;)
            {
                System.out.println("1.Add element at end");
                System.out.println("2.Add element at specific location");
                System.out.println("3.Display");
                System.out.println("4.Exit");       
                System.out.println("Enter your choice:");
   
                str1=in.readLine();
                ch=Integer.parseInt(str1);

                switch(ch)
                {
                   
                    case 1: System.out.print("Enter an element to insert at end:");
                            str2=in.readLine();
                            list.addElement(str2);
                            length++;   
                            for(i=0;i<length;i++)
                            System.out.println(list.elementAt(i));
                            break;
                       
                    case 2: System.out.print("Enter an element & position:");
                            str1=in.readLine();
                            str2=in.readLine();
                            int pos;
                            pos=Integer.parseInt(str2);
                            if(pos<=length && pos>0)
                            {
                                list.insertElementAt(str1,pos-1);
                                length++;
                                for(i=0;i<length;i++)
                                    System.out.println(list.elementAt(i));
                                break;
                            }
                            else
                            {
                                System.out.println("Invalid Position!");
                            }
                   
                    case 3:
                            System.out.println("List of elements:");
                            for(i=0;i<length;i++)
                            System.out.println(list.elementAt(i));
                            break;
                   
                    default:System.exit(0);
   
                }
                              
            }
        }
        catch(Exception e){}
    }
}

Java program to encode a given string.

import java.lang.String;
import java.util.*;
import java.io.*;

class Encode4
{

    public static void main(String args[]) throws IOException
    {
   
        int len,c,i,j,n;
        String str,keystr;

        DataInputStream in= new DataInputStream(System.in);
       
        int[] arr=new int[50];
        System.out.println("Enter the string to encode:");
        str=in.readLine();
        int key;
        System.out.println("Enter the Key:");
        keystr=in.readLine();
        key=Integer.parseInt(keystr);
         len= str.length();
        //System.out.println(key);

        for(i=0;i<len;i++)
        {
            c=str.charAt(i);
            if(c<91)
            {
                n=(c+key)%90;
                if(n<65)
                    n=n+64;
                arr[i]=n;
            }
            else
            {
                n=(c+key)%122;
                if(n<97)
                    n=n+96;
                arr[i]=n;
            }
        }
        System.out.println("The Encoded string is:");
       
        for(j=0;j<len;j++)
        {
            System.out.format("%c",arr[j]);
        }
    }

   
}

Java program to generate odd numbers then pick and display prime numbers among them.

import java.io.*;
public class OddNumber
{

    public static void main(String args[]) throws IOException
    {
               
        int limit;
        int b=0;
  
        DataInputStream in=new DataInputStream(System.in);
        System.out.println("Enter the limit:");
        limit=Integer.parseInt(in.readLine());

        int[] a= new int[50];
        System.out.println("Printing Odd numbers between 1 and " + 

        limit);
        for(int i=1; i <= limit; i++)
        {
            boolean isPrime = true;
            for(int j=2; j < i ; j++)
            {                       
                if(i % j == 0)
                {
                    isPrime = false;
                    break;
                }
            }
            //if the number is not divisible by 2 then it is odd
            if( i % 2 != 0)
            {
                System.out.print(i + " ");
            }
            if(isPrime)
            {
                if(i>2)   //1 is not prime and 2 is even.
                {
                    a[b] =i;
                    b++;
                }
            }
        }
              
        System.out.println("\nPrime numbers are as follows:");
        for(int k=0;k<=b-1;k++)
            System.out.println(a[k]);
      
    }  
}
FREE Hit Counters