Wednesday, 4 September 2013

java program used for string sorting

import java.lang.*;

class City
{
public static void main(String args[])
{

String name[] = {"visakhapatnam","hyderabad","kolkata","mumbai","chennai","delhi","ahamdabad","calcutta","bombay"};

int size=name.length;
String temp;
    System.out.println("The total number of the strings are:" + name.length);

    for(int i=0;i<size;i++)
    {
        for(int j=i+1;j<size;j++)
        {
        if(name[j].compareTo(name[i])<0)
        {

        temp=name[i];
        name[i]=name[j];
        name[j]=temp;
        }
    }
}
    for(int i=0;i<size;i++)
    {
        System.out.println(name[i]);
    }
    }
}



Interface concept java program


Harmonic Series java program

class Harmonic
{
public static void main (String args[])
{
int k,n=3;
double i=1,s,f=0;
while(i<=n)
{
s=1/i;
f=f+s;
i++;
}
for(k=1;k<=n;k++)
{
System.out.print("1/i"+k);
System.out.print("   ");
System.out.print(" + ");
System.out.print("  ");
}
System.out.print("-----------");
System.out.print("="+f);
}
}


constructor in java

class BoxDemo {
  double width;
  double height;
  double depth;
 
  // this is a constructor for Box
    BoxDemo(){
    System.out.println("Constructing BoxDemo");
    width =10;
    height =10;
    depth =10;
    }
    //compute and return value
    double volume(){
    return width*height*depth;
    }
  }

class  Box{
    public static void main(String[] args) {
        BoxDemo mybox = new BoxDemo();
       
        double vol;
        // get the volume of the box
       
        vol = mybox.volume();
       
        System.out.println("VOLUME IS "+vol);
      }
}


// output : Constructing BoxDemo
//             VOLUME IS 1000.0

class / objec and method in java

class Student{

    int no=10;    // data members

    void display(){  // method
        System.out.println("Hai!");
    }
}

class Main_Student{
public static void main(String args[]){
   
    Student s = new Student(); //object s for Student class
   
    System.out.println("Student number is:" +s.no);
    s.display();
    System.out.println("This is main class!");   
    }
}