Sunday 26 August 2012

java program convert roman to numeric


JAVA PROGRAM TO CONVERT ROMAN TO NUMERIC



public class RomantoDec {

public static void main(String[] args){

int M=1000,D=500,C=100,L=50,X=10,V=5,I=1;
int decimal=0;
String roman = args[0];
String romanNumeral = roman.toUpperCase();
String comRom[] ={"IV","IX","XL","XC","CD","CM"};
String genRom[] = new String[romanNumeral.length()-1];
for(int i=0,j=1;i<romanNumeral.length()&&j<romanNumeral.length();i++,j++){
genRom[i]= Character.toString(romanNumeral.charAt(i)) + Character.toString(romanNumeral.charAt(j));
}
for(int k=0;k<genRom.length;k++){
//System.out.print(genRom[k]+"\t");
String romo=genRom[k];
if(romo.equals("IV")){
decimal += V-I;
decimal = decimal-I;
decimal = decimal-V;
}
if(romo.equals("IX")){
decimal += X-I;
decimal = decimal-I;
decimal = decimal-X;
}
if(romo.equals("XL")){
decimal += L-X;
decimal = decimal-X;
decimal = decimal-L;
}
if(romo.equals("XC")){
decimal += C-X;
decimal = decimal-X;
decimal = decimal-C;
}
if(romo.equals("CD")){
decimal += D-C;
decimal = decimal-C;
decimal = decimal-D;
}
if(romo.equals("CM")){
decimal += M-C;
decimal = decimal-C;
decimal = decimal-M;
}
//}
}
int x = 0;
do {
char convertToDecimal = romanNumeral.charAt(x);
switch (convertToDecimal) {
case 'M':
decimal += M;
break;

case 'D':
decimal += D;
break;

case 'C':
decimal += C;
break;

case 'L':
decimal += L;
break;

case 'X':
decimal += X;
break;

case 'V':
decimal += V;
break;

case 'I':
decimal += I;
break;
}
x++;
} while (x < romanNumeral.length());
System.out.println("Decimal Number is: " + decimal);
}

}

Java program Numeric to Roman

WRITE A JAVA PROGRAM CONVERT NUMERIC TO ROMAN

convert numeric value to roman value,numeric to roman this is good program and good logic to convert a numeric to roman


class Roman
{
int n;
void set(int x)
{
n=x;
}
void convert()
{
if (n<=0)
{
System.out.println("no roman equivalant");
}
else
{
while(n>=1000)
{
System.out.print("M");
n=n-1000;
}
if(n>=900)
{
System.out.print("CM");
n=n-900;
}
if(n>=500)
{
System.out.print("D");
n=n-500;
}
if(n>=400)
{
System.out.print("CD");
n=n-400;
}
while(n>=100)
{
System.out.print("C");
n=n-100;
}
if(n>=90)
{
System.out.print("XC");
n=n-90;
}
if(n>=50)
{
System.out.print("L");
n=n-50;
}
if(n>=40)
{
System.out.print("XL");
n=n-40;
}
while(n>=10)
{
System.out.print("X");
n=n-10;
}
if(n>=9)
{
System.out.print("IX");
n=n-9;
}
if(n>=5)
{
System.out.print("V");
n=n-5;
}
if(n>=4)
{
System.out.print("IV");
n=n-4;
}
while(n>=1)
{
System.out.print("I");
n=n-1;
}


}
}
}

class RomanDemo
{
public static void main(String[] args)
{
if (args.length!=1)
{
System.out.println("plz enter only one value");
}
else
{
int x=Integer.parseInt(args[0]);
Roman r=new Roman();
r.set(x);
r.convert();
}
}
}

Tuesday 24 January 2012

Print Stars Diamond Program in java

Print Stars Diamond Program in java

The following program shows how to write a program to print stars Diamond in java
print stars Diamond program in java,write a java program to print stars,write a java program to print Diamond,write a program to print stars Diamond ,printing stars program.

class Dymond
{
    public static void main(String[] args)
    {
         int x=11;
         int y=x/2; // spaces
         int z=1; // *`s

         boolean b1=true;
         boolean b2= true;
         for(int i=0;i<x;i++)
        {
             for(int j=0;j<y;j++)
            {
                 System.out.print(" ");
            }
            for(int k=0;k<z;k++)
            {
                System.out.print("*");
            }
            if(y==0) b1=false;
            if(z==x) b2=false;

            y=b1?y-1:y+1;
            z=b2?z+2:z-2;
            System.out.println();
        }
       
    }
}

Print Stars Pyramid Program in java

Print Stars Pyramid Program in java


The following program shows how to write a program to print stars pyramid in java
print stars pyramid program in java,write a java program to print stars,write a java program to print pyramid,write a program to print stars pyramid,printing stars program.

class Pyramid
{
    public static void main(String[] args)
    {
         int x=11;
         int y=x/2; // spaces
         int z=1; // *`s

         for(int i=0;i<5;i++)
        {
             for(int j=0;j<y;j++)
            {
                 System.out.print(" ");
            }
            for(int k=0;k<z;k++)
            {
                System.out.print("*");
            }

            y=y-1;
            z=z+2;
            System.out.println();  //new line
        }
       
    }
}



Monday 23 January 2012

JVM Architecture

Internal stuff in JVM

Internal stuff in jvm is consists of various kind of things to tell about jvm how it will work and how it will perform operations on a java file once it has been compile
internal stuff in jvm,jvm architecture,jvm internal detailes,what jvm does,how jvm executes java byte code
observe the following diagram shows entire jvm Architecture and its internal detailes

                                                  JVM ARCHITECTURE
JAVA PROGRAM EXECUTION AND JVM ARCHITECTURE



Sunday 8 January 2012

variables in java

                java variables beginers
Based  on the purpose and position of  Declaration all the variables are divided into three types
java variables beginers,java variables in java,java variables,java variables for beginers
  • Instance Variables
  • static variables
  • local variables
Instance Variables:
  • If the value of the variable varying from object to object such type of variables are called instance variables.what is variable in java?
  • For the every object separate copy  of instance variables will be created .
  • instance variables will be created at the time of creation of object and destroy at time of object destruction.hence the scope of instance variables is exactly same as scope of object.
  • instance variables will be stored in heap memory. as part of object.
  • instance variables should be declare with in the class directly but outside of any method/block/constructor. not allowed
  • we can't access instance variables directly from static area.we should access by using object reference only.
  • but from instance area we can access instance variables directly. 


Example:
class Test{
int x=10;
public void m1(){
System.out.println(x);
}


public static void main(String ar[]){
System.out.println(x);//compile-time error
Test t=new Test();
System.out.println(t.x);


}


}


D:\javaprog>javac Test.java
Test.java:5: non-static variable x cannot be referenced from a static context
System.out.println(x);
                   ^
1 error


for the instance variables it is not required to perform initialization explicitly.Jvm will always provide default values.java variables beginers


class Test{
int x;
public static void main(String ar[]){
Test t=new Test();
System.out.println("the default value:"+t.x);

}
}

  
D:\javaprog>javac Test.java

D:\javaprog>java Test
the default value:0


static varibles:


If the value of the variable  not varying from object to object such type of variables are never recommended to declare instance variables.We have to declare those variables at class level with static modifier,java variables beginers
In  the case of instance variables every object separate copy   will be created .where in the case of static varibles for entire class a single copy will be created and shared by all objects.what is a varible in java?

static  variables should be declare with in the class directly but outside of any method/block/constructor.  with static modifier not allowed.


static variables will be created at the time of class loading  and destroy at time of class unloading.hence the scope of static variables is exactly same as scope of .class file.what is a variable in java?

static variables will be stored in method area.

We can access static variables from anywhere like from instance area or static area 
 
class Test{
static int x=10;
public void m1(){
System.out.println(x);//instance area
}

public static void main(String ar[]){
System.out.println(x);//static area
}


}
static variable we can access either by using object reference or class name.But usage of class name is recommended.with in the same class we can access directly.java variables beginers


class Test{
static int x=10;
public void m1(){
System.out.println(x);
}


public static void main(String ar[]){
System.out.println(x);//directly we can access
Test t=new Test();
System.out.println(t.x);//by using object reference
System.out.println(Test.x);//by using class name


}


}


for the static variables it is not required to perform initialization explicitly.Jvm will always provide default values.this is about what is variable in java
class Test{
static int x;
public static void main(String ar[]){
System.out.println("the default value:"+x);

}
}

  
D:\javaprog>javac Test.java

D:\javaprog>java Test
the default value:0

























Polindrome Program

program to check whether number is palindrome or not
program to check whether number is palindrome or not,polindrom program in java with example in java,java program to know whether a number is polindrome or not,polindrom program,polindrome in java,java polindrom with program.

import java.io.*;

public class poli  {
      public static void main(String [] args){
          try{
            BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter number");
            int num= Integer.parseInt(object.readLine());
              int n = num;
              int rev=0;
              System.out.println("Number: ");
              System.out.println(" "+ num);
              for (int i=0; i<=num; i++){
                  int r = num%10;
                  num = num/10;
                  rev = rev*10+r;
                  i=0;
              }
              System.out.println("After reversing the number: "+ " ");
              System.out.println(" "+ rev);         
              if(n == rev){
                System.out.print("Number is palindrome!");
              }
              else{
                System.out.println("Number is not palindrome!");
              }
          }
          catch(Exception e){
              System.out.println("Out of range!");
          }
    }
}    


program to check whether number is  polindrom or not,polindrom program in java,polindrome program, the below is the output for that    


D:\javaprog>javac polindrome.java

D:\javaprog>java polindrome
Enter number
121
Number:
 121
After reversing the number
 121
Number is palindrome!
D:\javaprog>javac polindrome.jav

D:\javaprog>java polindrome
Enter number
567
Number:
 567
After reversing the number
 765
Number is not palindrome!

Factorial value Program

java factorial program code
The below  is java factorial program code,how to find out a factorial of a number,factorial of a number in java,factorial program in java,factorial program
The below is java factorial program code


import java.io.*;
class Fact{
  public static void main(String[] args) {
      try{
        BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the number");
        int a= Integer.parseInt(object.readLine());
        double fact= 1;
        System.out.println("Factorial of " +a+ ":");
        for (int i= 1; i<=a; i++){
              fact=fact*i;
        }
        System.out.println(fact);
    }
    catch (Exception e){
        System.out.println("Array out of bounds exaception");
    }
  }
}

OUTPUT:

C:\>d:

D:\>cd javaprog

D:\javaprog>javac Fact.java

D:\javaprog>java Fact
enter the number:5
the factorial value
120.0















Fibanancci series program

Fibonacci series program in java 
The below code is arrnging fibonacci series progrm in java,fibonacci series program in java with example,program for fibonacci series,


import java.io.*;


class Fibanancci{
public static void main(String a[])throws IOException{
BufferedReader  br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("how many fibonacci?");
int n=Integer.parseInt(br.readLine());
long f1=0,f2=1;
System.out.println(f1);
System.out.println(f2);
long f=f1+f2;
System.out.println(f);
//already 3 fibonacci are displayed so count will start at 3
int count=3;
while(count<n){
f1=f2;
f2=f;
f=f1+f2;
System.out.println(f);
count++;


}


}
}
out put of fibonacci series program in java
C:\>d:

D:\>cd javaprog

D:\javaprog>javac Fibanancci.java

D:\javaprog>java Fibanancci
how many fibonacci? 10

1
1
2
3
5
8
13
21
34





Hashtable

  • Hashtable is collection that stores elements in the form of key-value pairs.
  • If key provided later its corresponding value can be easily retrieved from the Hashtable .
  • Duplicate keys are not allowed but values can be duplicated
  • Insertion order is not preserved and it is based on hashcode of the keys.
  • Heterogeneous objects are  allowed  for key and value
  • Null key or null value is not allowed .otherwise we will get NullPointerException.  
  • It is not Synchronized.but we can synchronized by using method of collection class
                   public staic Map synchronizedMap(Map m);

Constructors:
  • Hashtable m=new Hashtable();//creates an empty Hashtable object with default  initial capacity 11 and default fill ratio 0.75 .fill ratio is also known as Load Faction
  • Hashtable m=new Hashtable(Map m); 
  • Hashtable m=new Hashtable(int initialcapacity); 
  • Hashtable m=new Hashtable(int initialcapacity float fillratio); 

Saturday 7 January 2012

HashMap

HashMap:
  • HashMap is collection that stores elements in the form of key-value pairs.
  • If key provided later its corresponding value can be easily retrieved from the HashMap .
  • Duplicate keys are not allowed but values can be duplicated
  • Insertion order is not preserved and it is based on hashcode of the keys.
  • Heterogeneous objects are  allowed  for key and value
  • Null allowed for the key(only once) and for values(any number of)  
  • It is not Synchronized.but we can synchronized by using method of collection class
                   public staic Map synchronizedMap(Map m);

Constructors:
  • HashMap m=new HashMap();//creates an empty HashMap object with default  initial capacity 16 and default fill ratio 0.75 .fill ratio is also known as Load Faction
  • HashMap m=new HashMap(Map m); 
  • HashMap m=new HashMap(int initialcapacity); 
  • HashMap m=new HashMap(int initialcapacity float fillratio); 

 HashMapExample

import java.util.Collection;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Set;

public class HashMapExample {

  public static void main(String[] args) {
  
    //create HashMap object
    HashMap hMap = new HashMap();
  
    //add key value pairs to HashMap
    hMap.put(1,"One");
    hMap.put(2,"Two");
    hMap.put(3,"Three");
    hMap.put(4,"four");
    hMap.put(5,"five");
    Set st = hMap.keySet();//get the keys
    System.out.println("get the keys");

   Iterator itr = st.iterator();
   while(itr.hasNext())
   System.out.println(itr.next());

   System.out.println("get the values");
   Collection c = hMap.values();
  
    //obtain an Iterator for Collection
    Iterator itr1 = c.iterator();
  
    //iterate through HashMap values iterator
    while(itr1.hasNext())
      System.out.println(itr1.next());
}

output:
get the keys:
1          
2          
3          
4          
5           
get the values
One         
Two         
Three      
four        
five                   







HashSet

HashSet:
  •  HashSet represents a set of elements (objects)
  •  The underlying data structure is HashTable.
  • Insertion order is not preserved and it is based on hashcode of that objects
  • If we are insert duplicate objects we won't get any compile-time or run-time errors."add()" method simply returns false.
  •  Heterogeneous objects are allowed.
  • Null insertion is possible. 
  • It implements  serializable, clonable interfaces but not RandomAccess.
 Constructors:
  • HashSet h=new HashSet();//creates an empty HashSet object with default  initial capacity 16 and default fill ratio 0.75 .fill ratio is also known as Load Faction
  • HashSet h=new HashSet(int initialcapacity);
  • HashSet h=new HashSet(int initialcapacity,float Fillratio);
HashSetExample
import java.util.HashSet;
import java.util.Iterator;

public class HashSetExample {

  public static void main(String[] args) {

    //create object of HashSet
    HashSet hSet = new HashSet();
  
    //add elements to HashSet object
    hSet.add("a");
    hSet.add("b");
    hSet.add("c");
    hSet.add(null);
    hSet.add("e");
    hSet.add("f");
    hSet.add("a");//duplicate value it return false
    System.out.println(hSet);

    Iterator itr = hSet.iterator();
 System.out.println("displaying elements by using listIterator");
   
    while(itr.hasNext()){
     String s=(String)itr.next();
    System.out.println(s);
}
}
}
    
output:
[f, null, e, b, c, a]
displaying elements by using ListIterator
f
null
e
b
c

a



Friday 6 January 2012

Retriving elements from Collections

The Three cursors in java:

                               If we want get the objects from one by one  from collection object we should go for cursors.
There are three type of cursor available in java
  • Enumeration
  • Iterator
  • ListIterator
Enumeration: 
                              We can use Enumeration to get objects one by one from legacy Collection objects. We can create Enumeration objects by using elements() method

Ex:
Vector v=new Vector();
Enumeration e=v.elements();

Enumeration interface defines the fallowing method:
  •  public boolean hasMoreElements();
  •   public objects nextElement();
Limitations of Enumeration:
  • we can  apply  Enumeration concept only for Legacy  classes and it is not a universal cursor.
  • By using  Enumeration we can get only read access  and we can't perform remove option
  • To overcome these limitations sun people introduced  "Iterator "  concept.

Enumeration Example:

import java.util.Vector;
import java.util.Enumeration;

public class EnumerationDemo {
 public static void main(String[] args) {
    //create a Vector object
    Vector v = new Vector();
 
    //populate the Vector
    v.add("One");
    v.add("Two");
    v.add("Three");
    v.add("Four");
   /Get Enumeration of Vector's elements using elements() method
    Enumeration e = v.elements();
  

    /*
      Enumeration provides two methods to enumerate through the elements.
      It's "hasMoreElements()" method returns true if there are more elements to
      enumerate through otherwise it returns false. Its" nextElement() "method returns
      the next element in enumeration.
    */
  
    System.out.println("Elements of the Vector are : ");
  
    while(e.hasMoreElements()){
      System.out.println(e.nextElement());

}
 

}
}

Output would be
Elements of the Vector are :
One
Two
Three
Four

Iterator:

  • We can use Iterator to get objects one by one from any collection object.
  • we can apply  Iterator concept for any Collection object and it is a universal collection
  • While iterating objects by iterator we can perform both read and write operations.

We can get Iterator objects by using iterator() method of Collection interface.

Iterator i=c.iterator(); //"C" any collection object

Iterator interface defines the fallowing methods
  • public boolean hasNext();
  • public object next();
  • public void remove();
Limitations of Iterator:
  • Both Enumeration and Iterator are single direction cursors only.i.e we can always move only forward direction only.we can't move backward direction.
  • While iterating by iterator we can perform only read and remove operation but we can't perform replace and addition operation on that object.
  • To overcome these drawbacks we go for ListIterator.
Iterator Example:

import java.util.Iterator;
import java.util.ArrayList;

public class IteratorDemo {

  public static void main(String[] args) {

    //create an ArrayList object
    ArrayList aList = new ArrayList();
  
    //adding elements to  ArrayList object
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");

    aList.remove(2);//we are remove the element from specific position

    Iterator itr = aList.iterator();
 //iterate through the ArrayList values using Iterator's hasNext and next methods
while(itr.hasNext()){
      System.out.println(itr.next());
   }

}
}
Output would be
1
2
3
5


ListIterator: 

       ListIterator  is the child interface  of  Iterator.
                                    By using  ListIterator  we can move both  in forward direction  and backward direction.i.e it is a bidirectional cursor.
While iterating by ListIterator we can perform replacement and addition of new objects in addtion to read and remove operations.
By using listIterator() method we can create ListIterator object.

ListIterator itr = l.listIterator();
ListIterator defines the fallowing methods.

  • public boolean hasNext();
  • public object next();
  • public int  nextIndex();
  • public boolean  hasPrecious();
  • public object  previous();
  • public void previousIndex();
  • public void remove();
  • public void set(Object o);
  • public void add(Object new);




























Wednesday 4 January 2012

First program in java how to compile and run

              First program in java how to compile and run
 
First program in java how to compile and run for new in java,java first program,compile and run java program,command to compile and run a java program,first program in java how to compile and run
Beginners can easily understand how to compile and run a java program through command promt
first program in java how to compile and run 
First Program
------------------------------------------------------------------------------------------------------------
  class  FirstProgram
{
    public static void main(String[] args)
    {
        System.out.println("This is My Frist Program in Java!");
    }
}






------------------------------------------------------------------------------------------------------------


Steps  to compile and run the above code :

Step1: save the file with the class name 
           ex: FirstProgram.java 

Step2: go to commnad promt  
           
          and go to the path  that is where you have saved FirstProgram.java

          ex:  e:/java/practice/      is your path

          cmd:> cd E:/java/practice/                 press enter
          E:\>java/practice> javac FirstProgram.java       ---------> compile the program
          E:\>java/practice>java FristProgram                  ----------> run the program

           Note:  javac is  java compiler to compile the java program
                      java is commnad to run our java program

    >>> with this we have done our frist program in java












 

How to Set path

path setting in java with screen shot
Setting  java path  in environment varible is very easy just go through following steps
path setting in java with screen shot any one can easily understand how to set path for java in windows 7 operating system,path setting in java with diagram,set path in java environment variables,
set path in windows 7 for java,java path setting in windows 7,path setting in java

Step1: go to your installed java directory

         ex:  computer --> programfiles -->  java --> jdk1.6 --> bin

             (  C:\Program Files (x86)\Java\jdk1.6.0_11\bin  )

Step2: copy the path what you have opened directory window

            ex: C:\Program Files (x86)\Java\jdk1.6.0_11\bin 

Step3: open control panel in that  go through Advance Sysem Settings --> Environment Varibles
            --> System variables
            if there is no path click on new and write variable name as path and variable value as                           path name(what you have copied )
            if path variable already is there go through  and select  that
            click on edit button and paste the path
           view the follwing diagram for better understanding
           observe the 5th step in diagram paste the path at initial(first) position and put semicolon(;)
           if you paste path at last position first put semicolon and then paste the path
           
 

   with this we have done setting path  environment variables to work with java in our computer


Step4 : After setting path just open your commnd prompt and type the following commnads to      
             check whether the java enivironment is enabled or not

             cmd>  javac                                                                                                                                                           'javac' is not recognized as internal ro external command
             cmd>  java
                         'java' is not recognized as internal ro external command

         ---> if you found the above error messages you did not set the path properly check it once          
                 in the environment varibles and modify that and solve your problem
         ---> again come to commnd prompt and type the above commnad this time if u did not get the
                previous error well and good you have done path setting in EV

                                                                                                                                                                 
                                                                                                                                                               
                                                                                                                                                               



    





Java Introduction


   Introduction


  • Java is a Technology used for developing applications that make the web more fun and useful and it is a progaming lanauge it has an api documentation(help file)having classes and methods to execute a java program.
  • Java is an  Object Oriented Language(oops), in java every thing will be maintain interms of objects
  • Java is platform independent(java is platform independent because java code is compile on javac compiler after create one class file this file is run any of paltform run this code becauase jvm is understand this code properly...)
  • Java is like Write once run any where (WORA)

       


Monday 2 January 2012

Vector

Vector

                An Vector is like an array ,which can grow in memory dynamically.it means that we can store elements into the Vector,depending on the number of elements,the memory is dynamically allotted and re-allotted accommodate all the elements. 
  • Duplicate objects are allowed
  • Insertion order preserved
  • Heterogeneous objects are allowed 
  • Null insertion is possible
  •  Vector is  synchronized .this means that when more than one thread acts simultaneously on the Vector object,the results  will be reliable
    Vector constructor:

   Vector v=new Vector();   creates an empty Vector object with default initial capacity 10.

if Vector reaches max capacity then a new Vector object will be  with

                        new   capacity=(current capacity*2)

Vector v=new Vector(int initialcapacity);creates an empty Vector object with initial capacity

Vector v=new Vector(Collection c);creates an equivalent Vector object with the given collection.

Vector Example:
 
import java.util.Vector;
import java.util.Enumeration;

public class Edemo {
 public static void main(String[] args) {
    //create a Vector object
    Vector v = new Vector();
 
    //populate the Vector
    v.add("One");
    v.add("Two");
    v.add("Three");
    v.add("Four");
   /Get Enumeration of Vector's elements using elements() method
    Enumeration e = v.elements();
  

    /*
      Enumeration provides two methods to enumerate through the elements.
      It's "hasMoreElements()" method returns true if there are more elements to
      enumerate through otherwise it returns false. Its" nextElement() "method returns
      the next element in enumeration.
    */
  
    System.out.println("Elements of the Vector are : ");
  
    while(e.hasMoreElements()){
      System.out.println(e.nextElement());


}
 

}
}



Output would be
Elements of the Vector are :
One
Two
Three
Four