Showing posts with label Selenium Exercises. Show all posts
Showing posts with label Selenium Exercises. Show all posts

Wednesday, April 24, 2013

Selenium : Reverse each word of a sentence

Reverse each word of a sentence

import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class Reverse_words_sentence {
 public static void main(String[] args)
 {
  String sStr=JOptionPane.showInputDialog("Enter a string");
  String[] sArr=StringUtils.split(sStr, " ");
  String sRes;

  for (int i=0 ;i<=sArr.length-1;i++)
  {
   sArr[i]=StringUtils.reverse(sArr[i]);
  }
  sRes=StringUtils.join(sArr, " ");
  System.out.print(sRes);
 }
}

Selenium :Reverse a String without using builtin Functions

Reverse a String without using builtin Functions

import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class Reverse_str
{
 public static void main(String[] args)
 {
  String sStr=JOptionPane.showInputDialog("Enetr a str");
  String sRes="";
  for (int i=sStr.length() ;i>=0;i--)
  {
   sRes=sRes+StringUtils.mid(sStr, i, 1);
  }
  System.out.print(sRes);
 }


}


Tuesday, April 23, 2013

Selenium : To find the length of the string withouth using "Length " function

 To find the length of the string withouth using "Length " function



import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class Len_of_string
{

 public static void main(String[] args)
 {
  String sStr= JOptionPane.showInputDialog("enter string");
  char[] sArr=sStr.toCharArray();
  System.out.print(sArr.length);
 }

}

Selenium : Extract only digits from a string ( try ....catch )

                                                Extract only digits from a string


import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class extract_digits
{

 public static void main(String[] args)
 {
 String sStr=JOptionPane.showInputDialog("Enter any string with Digit");
 String sRes="";
 int sTemp;

 for (int i=1 ; i<=sStr.length();i++)
 {
   try
   {
     sTemp=Integer.parseInt(StringUtils.mid(sStr, i, 1));//substring(sStr, 1, end)
     sRes=sRes+"\n"+sTemp;
   
   }catch (Exception  e)
   { 
   }
 }

 System.out.println(sRes);

}
}



Friday, April 19, 2013

Selenium : Program To display each Character seperatly from a string


 Program To display each Character seperatly from a string


import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class Display_each_char
{

 public static void main(String[] args)
 {
  String sStr=JOptionPane.showInputDialog("Enetr some data");

  for (int i=0 ;i<= sStr.length();i++)
  {
   System.out.println(StringUtils.mid(sStr, i, 1));
  }
  

 }
}
 

Selenium : To find substring inside a string

To find substring inside a string



import javax.swing.JOptionPane;
public class Substring_in_string
{
 public static void main(String[] args)
 {

  String sData1=JOptionPane.showInputDialog("enter a string data");
  String sData2=JOptionPane.showInputDialog("enter a sub string data");
  if (sData1.contains(sData2) == true)
  {
  System.out.print("The substring exits ");
  }
  else
  {
   System.out.print("The substringdoes not exits ");
  }
 }
}

Thursday, April 18, 2013

Selenium : Find occurence of last "/" in an URL without using string reverse function(built in function)

 

 Find occurence of last "/" in an URL without using string reverse function(built in function)



import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;
public class url_bkslah_nrev
{
 public static void main(String[] args)
 {
  String sUrl=JOptionPane.showInputDialog("enter Url");
  Boolean bFlag=false;
  int i;

  for( i=sUrl.length()-1; i>=0 ; i--)  // Do if true until false
  {
   if (StringUtils.mid(sUrl, i, 1).charAt(0) == "/".charAt(0)) //converting to character
   {
    bFlag=true;
    break;
   }
  }

  if (bFlag=true)
  {
   System.out.print("/ exists at "+(i+1));
  }
  else
  {
   System.out.print("/ not exists");
  }

 }
//1/3/56/'
}

Selenium : Find the last occurence of "/" in the URL

                                    Find the last occurence of "/" in the URL



import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;

public class url_backslash
{

 public static void main(String[] args)
 {
  String sUrl=JOptionPane.showInputDialog("enter url");
  Boolean bFlag=false;
  int i;
  String sReverse=StringUtils.reverse(sUrl);
  for ( i=0 ;i<=sUrl.length()-1;i++)
  {
   //if (Character.toString(sReverse.charAt(i)) ==  "/")  //Convert character to string
   if (sReverse.charAt(i)== "/".charAt(0))
   {
    bFlag=true;
    break;
   }
  }

  if ( bFlag = true )
  {
   System.out.print("the last '/' exists @ "+(sUrl.length()- i));
  }
  else
  {
   System.out.print("/ does not exists");
  }
 }

}
 

Wednesday, April 17, 2013

Selenium : Display data in right angle triangle with side towards right

Display data in right angle triangle with side towards right


I have used "StringUtils.leftPad"  which is present inside " org.apache.commons.lang3.StringUtils; "

1. Download common/common-lang3.jar.zip( 613 k) from http://www.java2s.com/Code/Jar/c/Downloadcommonlang3jar.htm

2.Extract Jar files and the files to your Package in Eclipse.


 import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;
public class Triangle_right
{
 public static void main(String[] args)
 {
  String sRes="";
  String sData=JOptionPane.showInputDialog("enter data");
  for (int i=1 ; i<=sData.length();i++)
  {
   sRes=sRes+StringUtils.leftPad( sData.substring(0, i), sData.length())+"\n";    
  }
  System.out.print(sRes);
 }
}

Selenium :Display data in form of triangle in reverse

 Display data in form of triangle in reverse



import javax.swing.JOptionPane;

public class display_data_tri2
{

 public static void main(String[] args)
 {
    String sRes="";
    String sData=JOptionPane.showInputDialog("enter data");
    for (int i=sData.length() ;i>=1;i--)
    {
     sRes=sRes+sData.substring(0, i)+"\n";    
    }
    System.out.print(sRes);
  } 
}
 

Selenium : To display data in the form of right angled triangle


 To display data in the form of right angled triangle


import javax.swing.JOptionPane;
public class Triangle_left
{
 public static void main(String[] args)
 {
  String sRes="";
  String sData=JOptionPane.showInputDialog("enter data");
  for (int i=1 ; i<=sData.length();i++)
  {
   sRes=sRes+sData.substring(0, i)+"\n";    
  }
  System.out.print(sRes);
 }
}
 

Selenium :Search a String in a 2D array


Search a String in a 2D array



import javax.swing.JOptionPane;

public class Search_str_arr
{

 public static void main(String[] args)
 {

  String arr_String[][]=new String[3][3];

  for (int i=0 ;i<=2 ;i++)
  {
   for (int j=0;j<=2;j++ )
   {
    arr_String[i][j]=JOptionPane.showInputDialog("Enter string for arr_String["+i+"]["+j+"]");
   }
  }

  String sData=JOptionPane.showInputDialog("Enter Data to be searched");
  Boolean bFlag=false;

  for (int i=0 ;i<=2 ;i++)
  {
    for (int j=0;j<=2;j++ )
    {
     if (arr_String[i][j]==sData)
     {
      bFlag=true;
     }
    }
  
   if (bFlag=true)
   {
    break ;
   }   
  }

  if (bFlag=true)
  {
  System.out.println("the data exists");
  }
  else
  {
   System.out.println("the data does not exist");
  }
   
 }

}

Tuesday, April 16, 2013

Selenium :Transpose of Matrix

                                                    Transpose of Matrix



public class Transpose_Matrix
{
 /**
  * @param args
  */
 public static void main(String[] args)
 {
  String sRes="";
  int aArray[][]=new int [3][3];
  aArray[0][0]=0;
  aArray[0][1]=0;
  aArray[0][2]=0;

  aArray[1][0]=1;
  aArray[1][1]=1;
  aArray[1][2]=1;

  aArray[2][0]=2;
  aArray[2][1]=2;
  aArray[2][2]=2;


  for (int i=0 ; i<=aArray.length-1 ;i++)
  {
   for (int j=0;j<=aArray[0].length-1;j++)
   {
    sRes=sRes+" "+aArray[i][j];
   }
   sRes=sRes+"\n";
  }
  System.out.println(sRes);
  sRes="";
  //-----------------------------------------Method 1 
  for (int j=0;j<=aArray[0].length-1;j++)

  {
   for (int i=0 ; i<=aArray.length-1 ;i++)
   {
    sRes=sRes+" "+aArray[i][j];
   }
   sRes=sRes+"\n";
  }
  System.out.println(sRes);
  sRes="";
  //-----------------------------------------Method 2
  for (int i=0 ; i<=aArray.length-1 ;i++)
  {
   for (int j=0;j<=aArray[0].length-1;j++)
   {
    sRes=sRes+" "+aArray[j][i];
   }
   sRes=sRes+"\n";
  }
  System.out.println(sRes);
 }
}

Selenium :Print Array 3X3 in the form of Matrix

Print Array 3X3  in the form of Matrix



public class Print_array_in_matrix
{
 public static void main(String[] args)
 {
  String sRes="";
  int aArray[][]=new int [3][3];
  aArray[0][0]=0;
  aArray[0][1]=0;
  aArray[0][2]=0;

  aArray[1][0]=0;
  aArray[1][1]=0;
  aArray[1][2]=0;

  aArray[2][0]=0;
  aArray[2][1]=0;
  aArray[2][2]=0;


  for (int i=0 ; i<=aArray.length-1 ;i++)
  {
   for (int j=0;j<=aArray[0].length-1;j++)
   {
    sRes=sRes+" "+aArray[i][j];
   }
   sRes=sRes+"\n";
  }
  System.out.print(sRes);
  
 }
}


Selenium :To find Factorial of a number



To find Factorial of a number



import javax.swing.JOptionPane;
public class Factorial
{
 public static void main(String[] args)
 {
  int iRes=1;
  String sPict="1";
  int ifact=Integer.parseInt(JOptionPane.showInputDialog("enter an integer"));

  for (int i=2 ; i<=ifact ; i++)
  {
   iRes=iRes*i;
   sPict=i+"x"+sPict;
  
  }
  System.out.println(sPict+"="+iRes); 
 }
}

Monday, April 15, 2013

Selenium : Accept Array and print in Reverse order

Array in Reverse order



public class Array_Reverse
{
 public static void main(String[] args)
 {
  int aArray[]= new int[2];//  2 means not 2+1 --> aArray[0] , aArray[1]
  for (int iSize=0 ; iSize<=aArray.length-1 ; iSize++)
  {
   aArray[iSize]=Integer.parseInt(JOptionPane.showInputDialog("Enter value for aArray["+iSize+"]" ));
  }
  //System.out.println(aArray[0]);

  int iSize=aArray.length-1;
  for ( iSize=aArray.length-1 ; iSize>=0 ; iSize--) //"iSize>=0 "Execute if true till false
  {
   System.out.println("aArray["+iSize+"]="+aArray[iSize]);
  }
 
  for ( iSize=0 ; iSize<=aArray.length-1 ;iSize++ )// Execute if true till false
  {
   System.out.println(aArray[iSize]);
  }
 
  System.out.println("Done"); 

 }
}

Selenium : To check whether the given number is prime

To check whether the given number is prime


import javax.swing.JOptionPane;

public class Prime {
 public static void main(String[] args)
 {
  boolean bFlag=true;
  int iVar=Integer.parseInt(JOptionPane.showInputDialog("Enter an integer value"));
  for (int i=2 ; i<=iVar/2 ; i++)
  {
   if (iVar%i == 0)
   {
    System.out.print("The number is not prime");
    bFlag=false;
    break ;
   } 
 
  }
  if( bFlag == true )
  {
   System.out.print("The number is prime");
  }   
 }
}

Selenium : Size of Matrix or 2D array

 Calculate Size of Matrix or Size of 2D Array


public class Size_Matrix {
 public static void main(String[] args)
 {
  int[][] matrix = new int[20][30];
  System.out.println("Number of rows = " + matrix.length);
  System.out.println("Number of columns = " + matrix[0].length);
 }

Selenium : Units of Electricity bill

Calculate Units of Electricity bill

0-50      -> $.5/Unit
50-100  ->$1.5/Unit
100-200->$2 /unit
150-200->$3/Unit
>200    ->  $4/unit


import javax.swing.JOptionPane;

public class Electricity
{
 public static void main(String[] args)
 {
  int iUnit=Integer.parseInt(JOptionPane.showInputDialog("Enter units consumed"));

  if (iUnit<= 50) // ex 30 unit ==$0.5
  {
   System.out.print("Pay ="+"$.5");
 
  }
  else if (50  < iUnit && iUnit <=100 )  //Eg 70 Unit = 30.5
  {
   System.out.print("Pay =$"+(.5+(iUnit-50)*1.5));
  }
  else if (100  < iUnit && iUnit <=150 ) //Eg 120 Unit = 115.5
  {
   System.out.print("Pay =$"+( ((iUnit-100)*2) + ((50*1.5)) +(.5)));
  }
  else if (150  < iUnit && iUnit <=200 ) //Eg 170 Unit =$235.5
  {
   System.out.print("Pay =$"+((iUnit-150)*3+(50*2)+(50*1.5)+.5));
  }
  else if (iUnit >200 )  //Eg 210 units
  {
   System.out.print("Pay =$"+((iUnit-200)*4+(50*3)+(50*2)+(50*1.5)+.5));
  }
 }
}