Thursday, 30 August 2012

overview of class in java


overview of class in java
//LINEAR SEARCH IN JAVA
package com.swain.cell;
public class LinearSearch {
public static void main(String[] args)
{
System.out.println("Linear Search List");

int[] arr = {0,2879, 15, 29, 78, 26, 24, 44, 167, 13,24,47};

int i;
int num = 44;
boolean flag=false;

for(i=0; i<arr.length; i++){
if(arr[i]==num){
flag=true;
break;
}

}//for

if(flag){
System.out.println(num + " index  position "+i);
}
else{
System.out.println(num + " no not found");
}

}

}
//MATCH TWO STRING IN JAVA
package com.swain.cell;

import java.util.HashMap;
import java.util.StringTokenizer;

public class MatchString {
public static void main(String arg[]) {
       String str = "hima hima sekhar swain hima";
       char ch1[]=str.toCharArray();
       HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
       for(int i=0; i<ch1.length; i++)
       {
           char ch=ch1[i];
           hm.put(ch,(hm.get(ch)==null?1:hm.get(ch)+1));
       }
       System.out.println(hm.get('a'));
       
       StringTokenizer t = new StringTokenizer(str);
       HashMap<String,Integer> hms=new HashMap<String,Integer>();
       
while(t.hasMoreTokens())
{
String word = t.nextToken();
hms.put(word,(hms.get(word)==null?1:hms.get(word)+1));
System.out.println("Found the word " + word);
}
System.out.println("no of word " + hms.toString());
       
   }
}
//PALINDROME IN JAVA
package com.swain.cell;

public class Palindrome {

public static void main(String [] args){
 try{
 
 int num= 161;
 int n = num;
 int rev=0;
 for (int i=0; i<=num; i++){
 int r=num%10;
 num=num/10;
 rev=rev*10+r;
 i=0;
 }
  if(n == rev){
 System.out.print("Number is palindrome!");
 }
 else{
 System.out.println("Number is not palindrome!");
 }
 }
 catch(Exception e){
 System.out.println("Error");
 }
 }
}
//PIRAMID IN JAVA
package com.swain.cell;

public class Piramid {

public static void main(String args[]){
int p=1;
for(int i=1;i<=5;i++){
for(int j=i;j<5;j++){
System.out.print(" ");
}
for(int k=1;k<=p;k++){
if(k%2==0){
System.out.print(" ");

}else
System.out.print("*");

}
System.out.println();
p+=2;
}
}
}

No comments:

Post a Comment