Java Programs for Selenium Interview (100% Working)


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
packageΒ package_name;
publicΒ classΒ AccessModifierInJavaΒ {
Β Β Β Β Β Β publicΒ intΒ i=1;
Β Β Β Β Β Β protectedΒ intΒ k=2;
Β Β Β Β Β Β privateΒ intΒ p=3;
Β Β Β Β Β Β intΒ d=4;
Β Β Β Β Β Β publicΒ voidΒ test1()Β {
Β Β Β Β Β Β Β Β Β Β Β Β System.out.println(β€œpublic void test1()”);
Β Β Β Β Β Β }
Β Β Β Β Β Β protectedΒ voidΒ test2()Β {
Β Β Β Β Β Β Β Β Β Β Β Β System.out.println(β€œprotected void test2()”);
Β Β Β Β Β Β }
Β Β Β Β Β Β voidΒ test3()Β {
Β Β Β Β Β Β Β Β Β Β Β Β System.out.println(β€œvoid test3()”);
Β Β Β Β Β Β }
Β Β Β Β Β Β privateΒ voidΒ test4()Β {
Β Β Β Β Β Β Β Β Β Β Β Β System.out.println(β€œprivate void test4()”);
Β Β Β Β Β Β }
Β Β Β Β Β Β publicΒ staticΒ voidΒ main(String[]Β args)Β {
AccessModifierInJavaΒ objΒ =Β newΒ AccessModifierInJava();
Β Β Β Β Β Β obj.test1();
Β Β Β Β Β Β obj.test2();
Β Β Β Β Β Β obj.test3();
Β Β Β Β Β Β obj.test4();
Β Β Β Β Β Β System.out.println(obj.i);
Β Β Β Β Β Β System.out.println(obj.k);
Β Β Β Β Β Β System.out.println(obj.p);
Β Β Β Β Β Β System.out.println(obj.d);
Β Β Β Β Β Β }
}

package arrays;

public class AddTwoMatrix {
public static void main(String args[])
{
int m, n, c, d;

int first[][] = { {1,2}, {5,10}, {2,6} };
int second[][] = { {2,6}, {1,2}, {5,3} };

m=first.length;
n=first[0].length;

int sum[][] = new int[m][n];

System.out.println(β€œCalculating Sum of 2 matrices….”);

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];Β  //replace β€˜+’ with β€˜-β€˜ to subtract matrices

System.out.println(β€œSum of 2 matrices….”);

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+”\t”);

System.out.println();
}
}

}
——————————————————————————–
package collections;

import java.util.ArrayList;

public class ArrayListExample1 {

public static void main(String[] args) {

//Declaration
ArrayListΒ list=new ArrayList();

//Add values to arraylist
list.add(β€œJohn”);
list.add(β€œDavid”);
list.add(β€œScott”);
list.add(β€œSmith”);

System.out.println(list.size()); // returns size of arraylist

//reading values from arraylist
for(String s:list)
{
System.out.println(s);
}

}

}

——————————————————————————–
package conditions;

public class EvenOrOddNumber {

public static void main(String[] args) {

int num=10;

if(num%2 == 0)
{
System.out.println(β€œNumber is even number”);
}

else
{
System.out.println(β€œNumber is odd number”);
}
}

}

——————————————————————————–
package arrays;

class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search_element;

int array[] = {100,200,300,400,500};

search_element=200;

n=array.length;

firstΒ  = 0;
lastΒ  Β = n – 1;
middle = (first + last)/2;

while( first <= last )
{
if ( array[middle] < search_element )
first = middle + 1;
else if ( array[middle] == search_element )
{
System.out.println(search_element + ” found at location ” + (middle + 1) + β€œ.”);
break;
}
else
last = middle – 1;

middle = (first + last)/2;
}
if ( first > last )
System.out.println(search_element + ” isn’t present in the list.\n”);
}
}
——————————————————————————–

package arraysandstrings;

public class EvenAndOddNumbersinArray {

public static void main(String[] args) {

int a[]={10,20,15,3,6,7,8,2,5,7};

int n=a.length;

System.out.print(β€œOdd numbers:”);
for(int i = 0 ; i < n ; i++)
{
if(a[i] % 2 != 0)
{
System.out.print(a[i]+” β€œ);
}
}
System.out.println();

System.out.print(β€œEven numbers:”);
for(int i = 0 ; i < n ; i++)
{
if(a[i] % 2 == 0)
{
System.out.print(a[i]+” β€œ);
}
}

}

}

——————————————————————————–
package collections;

import java.util.ArrayList;

public class ArrayListExample2 {

public static void main(String[] args) {

//Declaration
ArrayList list=new ArrayList();

//Adding values to array list
list.add(β€œwelcome”);
list.add(100);
list.add(10.5);
list.add(β€˜C’);
list.add(true);

System.out.println(list.size()); //size of arraylist

System.out.println(list.get(2)); //returns specific value from array list, index starts from 0

System.out.println(β€œBefore inserting:” +list); // print all the values from arraylist

//Insert values into araylist
list.add(1,”selenium”);
System.out.println(β€œAfter insertion:” +list);

// remove values from arraylist
list.remove(3);
System.out.println(β€œAfter remove:” +list);

//reading values from array list usign for loop

for(Object i:list)
{
System.out.println(i);
}

}

}

——————————————————————————–
package collections;

import java.util.Random;

//Generating Random Numbers In The Given Range

public class GenerateRandomeNumbersInGivenRange {

public static void main(String[] args)
{
//Generating random integers between 0 and 50 using Random class

System.out.println(β€œRandom integers between 0 and 50 using Random class :”);

Random random = new Random();

for (int i = 0; i < 5; i++)
{
System.out.println(random.nextInt(50));
}

//Generating random integers between 0 and 50 range using Math.random()

System.out.println(β€œRandom integers between 0 and 50 using Math.random() :”);

for (int i = 0; i < 5; i++)
{
System.out.println((int)(Math.random() * 50));
}

}
}

——————————————————————————–
package arrays;

import java.util.Arrays;

public class BinarySearchUsingMethod {

public static void main(String args[])
{
int array[] = {10, 20, 30, 40, 50 }; //Should be in order

System.out.println(Arrays.binarySearch(array, 30));
}
}

——————————————————————————–
package strings;

public class ConvertSentenceCase {

public static void main(String[] args) {

String s = β€œmy name is pavan”;
String rev = β€œβ€;

String s1 [] = s.split(” β€œ);
for(int i =0; i
String f = s1[i].substring(0, 1).toUpperCase();
String l = s1[i].substring(1, s1[i].length());

rev = rev +” β€œ+Β  (f+l);

}
System.out.println(rev.trim());

}
}
——————————————————————————–
package arrays;

public class BubbleSort {

public static void main(String []args) {
int n, c, d, temp;

int array[] = {500,300,200,400,100};
n = array.length;

System.out.println(β€œArray Before Bubble Sort”);

for(int i = 0; i < array.length; i++) {
System.out.print(array[i] + ” β€œ);
}

//Sorting
temp = 0;

for(int i = 0; i < n; i++) {
for(int j=1; j < (n-i); j++)
{
if(array[j-1] > array[j])
{
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
}

System.out.println();

System.out.println(β€œArray After Bubble Sort”);

for(int i = 0; i < array.length; i++) {
System.out.print(array[i] + ” β€œ);
}
}
}
——————————————————————————–
package strings;

import java.util.Scanner;

public class CountTheWords {

public static void main(String[] args) {
{
System.out.println(β€œEnter the string:”);

Scanner sc = new Scanner(System.in);

String s=sc.nextLine();

int count = 1;

for (int i = 0; i < s.length()-1; i++)
{
if((s.charAt(i) == β€˜ β€˜) && (s.charAt(i+1) != β€˜ β€˜))
{
count++;
}
}

System.out.println(β€œNumber of words in a string = β€œ+count);
}

}

}
——————————————————————————–
package collections;

import java.util.Random;

public class GeneratingRandomNumbers {

public static void main(String[] args)
{

//Appraoch1: Generating Random Numbers Using java.util.Random Class

Random random = new Random();

//Generating random integers using Random class

for(int i = 0; i < 5; i++)
{
System.out.println(β€œRandom Integers : β€œ+random.nextInt());
}

System.out.println(β€œβ€”β€”β€”β€”β€”β€”β€”β€”β€”β€“β€œ);

//Generating random doubles using Random class

for(int i = 0; i < 5; i++)
{
System.out.println(β€œRandom Doubles : β€œ+random.nextDouble());
}

System.out.println(β€œβ€”β€”β€”β€”β€”β€”β€”β€”β€”β€“β€œ);

//Generating random booleans using Random class

for(int i = 0; i < 5; i++)
{
System.out.println(β€œRandom booleans : β€œ+random.nextBoolean());
}

// Generating Random Numbers Using Math.random()
//Generating random doubles using Math.random()

for(int i = 0; i < 5; i++)
{
System.out.println(β€œRandom Doubles : β€œ+Math.random());
}

}

}

package strings;
——————————————————————————–

/*Write a java program to count the total number of occurrences of a given character
in a string without using any loop?
*/

public class CountCharacterOccurence {
public static void main(String[] args)
{
String s = β€œJava is java again java again”;

char c = β€˜a’;

int count = s.length() – s.replace(β€œa”, β€œβ€).length();

System.out.println(β€œNumber of occurances of β€˜a’ is: β€œ+count);
}
}

package collections;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class DuplicateElements {

public static void main(String[] args) {

String names[] = {β€œJava”, β€œJavaScript”, β€œRuby”, β€œC”, β€œPython”, β€œJava”};

//1. compare each element: O(nxn) β€” worst solution
for(int i = 0; i
for(int j = i+1; j

if(names[i].equals(names[j])){
System.out.println(β€œduplicate element is::” + names[i]);
}
}
}

System.out.println(β€œ*********”);

//2. using HashSet: Java Collection: it stores unique values: O(n)
SetΒ store = new HashSet();
for(String name : names){
if(store.add(name) == false){
System.out.println(β€œduplicate element is::” + name);
}
}

System.out.println(β€œ*********”);

//3. using HashMap: O(2n)
MapΒ storeMap = new HashMap();

for(String name : names){
Integer count = storeMap.get(name);
if(count == null){
storeMap.put(name, 1);
}
else{
storeMap.put(name, ++count);
}
}

//get the values from this HashMap:

Set> entrySet = storeMap.entrySet();
for(EntryΒ entry : entrySet){
if(entry.getValue()>1){
System.out.println(β€œduplicate element is::”+ entry.getKey());
}
}

}

}
——————————————————————————–
package conditions;

public class GreatestOfThreeNumbers {

public static void main(String[] args) {

int a=50;
int b=100;
int c=20;

if(a>b && a>c)
{
System.out.println(” a is greatest”);
}
else if(b>a && b>c)
{
System.out.println(β€œb is largest”);
}
else
{
System.out.println(β€œc is greatest”);
}

}

}

——————————————————————————–
package collections;

import java.util.HashMap;
import java.util.Map;

public class HashMapExample {

public static void main(String[] args) {

HashMapΒ hm=new HashMapΒ ();

//adding values to HashMap
hm.put(101,”John”);
hm.put(102,”Scott”);
hm.put(103,”David”);
hm.put(104,”Smith”);
hm.put(105,”Kim”);

System.out.println(hm);

//Remove a pair from hashmap
hm.remove(103);
System.out.println(β€œAfter remove pair from hashmap:”+hm);

hm.put(106,”XYZ”);
System.out.println(hm);

//Get values
System.out.println(β€œThe Value is: ” + hm.get(104)); //Smith

//How to read pairs from HashMap

for(Map.Entry m:hm.entrySet())
{
System.out.println(m.getKey()+”  Β β€œ+m.getValue());

}

}

}

——————————————————————————–
package conditions;

public class IfElseCondition {

public static void main(String[] args) {

int age=20;

if(age>=18)
{
System.out.println(β€œEligible for vote”);
}
else
{
System.out.println(β€œNOT Eligible for vote”);
}
}

}

——————————————————————————–
package strings;

public class IntegerToStringConvesion {

public static void main(String[] args) {
// Convert Integer To String Using Integer.toString() Method
int i = 2015;

String s = Integer.toString(i);

System.out.println(s);

//Convert Integer To String Using String.valueOf() method
s = String.valueOf(i);

System.out.println(s);

}

}

——————————————————————————–
package arrays;

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

int array[] = {100,200,300,400,500};

int search_element=400;

int c;

for (c = 0; c < array.length; c++)
{
if (array[c] == search_element)Β  Β  Β /* Searching element is present */
{
System.out.println(search_element + ” is present at location ” + (c + 1) + β€œ.”);
break;
}
}
if (c == array.length)Β  /* Element to search isn’t present */
System.out.println(search_element + ” isn’t present in array.”);
}
}
package conditions;

public class LargestofTwoNumbers {

public static void main(String[] args) {

int a=50;

int b=20;

if(a>b)
{
System.out.println(β€œa is largest”);
}

else
{
System.out.println(β€œb is largest”);
}
}

}

——————————————————————————–
package arrays;

public class MaxAndMinElementInArray {

public static void main(String[] args) {

int array[]={10,100,20,50,5,60};

//Max value in array
int max = array[0];

for (int i = 1; i < array.length; i++) {
if (array[i] > max)
{
max = array[i];
}
}

System.out.println(β€œMax Element in array:” +max);

// Min value in array
int min = array[0];

for (int i = 1; i < array.length; i++) {
if (array[i] < min)
{
min = array[i];
}
}

System.out.println(β€œMin Element in array:” +min);

}

}

——————————————————————————–
package arrays;

public class MissingNumber {

public static void main(String[] args) {

//int a[] = {1,2,3,4,5,7,8,9,10,……..100};
//1+2+4+5 = 12
//1+2+3+4+5 = 15
//15-12 = 3

int a[] = {-1,0,1,2,4,5,6,7,8,9,10};
int sum = 0;
for(int i = 0; i
sum = sum + a[i]; //49
}
System.out.println(sum);

int sum1 = 0;
for(int j=-1; j<=10; j++){
sum1 = sum1 + j;//55
}

System.out.println(sum1);

System.out.println(β€œmissing number is:”+ (sum1-sum));

}

}
——————————————————————————–
package loops;

public class MultiplicationTable {

public static void main(String[] args) {

int n=5;

for(int i=1;i<=10;i++)
{
System.out.println(n +”X”+i+”=”+ (n*i));
}

}

}

——————————————————————————–
package conditions;

public class NestedIfElse {

public static void main(String[] args) {

int day=10;

if(day==1)
{
System.out.println(β€œSunday”);
}
else if(day==2)
{
System.out.println(β€œMonday”);
}
else if(day==3)
{
System.out.println(β€œTuesday”);
}
else if(day==4)
{
System.out.println(β€œWednesday”);
}
else if(day==5)
{
System.out.println(β€œThursday”);
}
else if(day==6)
{
System.out.println(β€œFriday”);
}
else if(day==7)
{
System.out.println(β€œSaturday”);
}
else
{
System.out.println(β€œInvalid week number”);
}

}

}

——————————————————————————–
package loops;
public class NumberOfDigits {

public static void main(String[] args) {

int count = 0;
int num = 3452;

while(num != 0)
{
num /= 10;Β  // 345Β  34Β  3
++count;
}

System.out.println(β€œNumber of digits: ” + count);

}

}

——————————————————————————–
package loops;

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

int lastDigit,sum=0,a;
int inputNumber=171; //It is the numberΒ  to be checked for palindrome

a=inputNumber;

// Code to reverse a number
while(a>0)
{Β  Β System.out.println(β€œInput Number β€œ+a);
lastDigit=a%10; //getting remainder
System.out.println(β€œLast Digit β€œ+lastDigit);
System.out.println(β€œDigit β€œ+lastDigit+ ” was added to sum β€œ+(sum*10));
sum=(sum*10)+lastDigit;
a=a/10;

}

// if given number equal to sum than number is palindrome otherwise not palindrome
if(sum==inputNumber)
System.out.println(β€œNumber is palindrome β€œ);
else
System.out.println(β€œNumber is not palindrome”);
}

}

——————————————————————————–
package arraysandstrings;

public class PalindromeString {

public static void main(String[] args) {

String s = β€œDAD”;

//1. using for loop
int len = s.length(); //7
String rev = β€œβ€;

for(int i =len-1; i>=0; i–){
rev = rev + s.charAt(i); //muineleS
}

System.out.println(rev);

if(s.equals(rev))
{
System.out.println(β€œPalindrome string”);
}
else
{
System.out.println(β€œNot Palindrome string”);
}

//2. using StringBuffer class:
//StringBuffer sf = new StringBuffer(s);
//System.out.println(sf.reverse());
}

}
——————————————————————————–
//Write a Java program print a number is positive or negative.
package conditions;

public class PositiveOrNagitiveNumber {

public static void main(String[] args) {

int num = 10; // positive
// int num=-10; //Negitive

if (num > 0)
{
System.out.println(” Number is Positive”);
} else
{
System.out.println(β€œNumber is Negitive”);
}

}

}

——————————————————————————–
package collections;

import java.util.ArrayList;
import java.util.HashSet;

//Removing Duplicate Elements From ArrayList Using HashSet

public class RemoveDuplicatesFromArrayList {
public static void main(String[] args)
{
//Constructing An ArrayList

ArrayListΒ listWithDuplicateElements = new ArrayList();

listWithDuplicateElements.add(β€œJAVA”);

listWithDuplicateElements.add(β€œJ2EE”);

listWithDuplicateElements.add(β€œJSP”);

listWithDuplicateElements.add(β€œSERVLETS”);

listWithDuplicateElements.add(β€œJAVA”);

listWithDuplicateElements.add(β€œSTRUTS”);

listWithDuplicateElements.add(β€œJSP”);

//Printing listWithDuplicateElements

System.out.print(β€œArrayList With Duplicate Elements :”);

System.out.println(listWithDuplicateElements);

//Constructing HashSet using listWithDuplicateElements

HashSetΒ set = new HashSet(listWithDuplicateElements);

//Constructing listWithoutDuplicateElements using set

ArrayListΒ listWithoutDuplicateElements = new ArrayList(set);

//Printing listWithoutDuplicateElements

System.out.print(β€œArrayList After Removing Duplicate Elements :”);

System.out.println(listWithoutDuplicateElements);
}

}
——————————————————————————–

package arrays;

public class RemoveJunk {

public static void main(String[] args) {

String s = β€œΓ₯Β°?Γ§Β±Β³Γ€Β½β€œΓ©ΒͺŒç‰ˆ latin string 01234567890”;
String s1 = β€œ@#$@#$@ testing #@$@#$@#$ Selenium !@#$@#$@# &&&& Java”;

//Regular Expression: [^a-zA-Z0-9]

s = s.replaceAll(β€œ[^a-zA-Z0-9]”, β€œβ€);
System.out.println(s);

s1 = s1.replaceAll(β€œ[^a-zA-Z0-9]”, β€œβ€);
System.out.println(s1);

}

}
——————————————————————————–
package strings;
/*
* Write a java program to remove all white spaces from a string?
*
*/

public class RemoveWhiteSpacesInaString {

public static void main(String[] args) {
{
String str = ”  Core Java selenium automationΒ  Β  Β  Β oops programmingΒ  β€œ;

String strWithoutSpace = str.replaceAll(β€œ\\s”, β€œβ€);

System.out.println(strWithoutSpace);Β  Β  Β  Β  Β //Output : CoreJavajspservletsjdbcstrutshibernatespring

}

}

}
——————————————————————————–

package strings;

import java.util.Scanner;

// Please change the extension to .java before using the file
// I changed the extension to .txt because udemy does not allow to upload .java files
public class ReverseChars {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(β€œPlease enter a string: β€œ);
String original = scan.nextLine();

while (original.isEmpty() || original == null) {
System.out.println(β€œPlease enter a valid string, empty and null strings are not accepted:”);
original = scan.nextLine();
}
scan.close();

ReverseChars output = new ReverseChars();
String reverseCharacters = output.reverseCharacters(original);
System.out.println(reverseCharacters);
}

private String reverseCharacters(String originalString) {
String reverse = β€œβ€;

for (int i = originalString.length() – 1; i >= 0; i–) {
reverse = reverse + originalString.charAt(i);
}
return reverse;
}
}

——————————————————————————–
package strings;

/*
* Java Program To Reverse Each Word Of A String
*/
public class ReverseEachWord {
public static void main(String[] args)
{
reverseEachWordOfString(β€œJava Concept Of The Day”);
reverseEachWordOfString(β€œJava J2EE JSP Servlets Hibernate Struts”);
reverseEachWordOfString(β€œI am string not reversed”);
reverseEachWordOfString(β€œReverse Me”);
}

static void reverseEachWordOfString(String inputString)
{
String[] words = inputString.split(” β€œ);

String reverseString = β€œβ€;

for (int i = 0; i < words.length; i++)
{
String word = words[i];

String reverseWord = β€œβ€;

for (int j = word.length()-1; j >= 0; j–)
{
reverseWord = reverseWord + word.charAt(j);
}

reverseString = reverseString + reverseWord + ” β€œ;
}

System.out.println(inputString);
System.out.println(reverseString);
System.out.println(β€œβ€”β€”β€”β€”β€”β€”β€”β€”-β€œ);
}
}
——————————————————————————–

package loops;

public class ReverseNumber {
public static void main(String args[])
{
// 1. using algorithm
long num = 12345; // 54321
long rev = 0;

while (num != 0)
{
rev = rev * 10 + num % 10; // 5432
num = num / 10; // 12
}

System.out.println(β€œReverse num is:” + rev);

// 2. using StringBuffer method
long num1 = 12345;
System.out.println(new StringBuffer(String.valueOf(num1)).reverse());
}
}

——————————————————————————–
package strings;

import java.util.Scanner;

public class ReverseString {

public static void main(String[] args) {
//Reverse a String:
//diff bw String and StringBuffer
//do we have reverse function in String?

System.out.println(β€œEnter the string:”);

Scanner sc = new Scanner(System.in);

String s=sc.nextLine();

//1. using for loop
int len = s.length(); //8
String rev = β€œβ€;

for(int i =len-1; i>=0; i–){
rev = rev + s.charAt(i); //muineleS
}

System.out.println(rev);

//2. using StringBuffer class:
StringBuffer sf = new StringBuffer(s);
System.out.println(sf.reverse());
}

}
——————————————————————————–
package arraysandstrings;

public class SearchNumberinArray {

public static void main(String[] args) {

int a[]={10,20,30,40,50};

int num=30;
boolean flag=false;

for(int i:a)
{
if(num==i)
{
System.out.println(β€œElement found”);
flag=true;
break;
}
}

if(flag==false)
{
System.out.println(β€œElement NOT found”);
}

}

}
——————————————————————————–
package arrays;

public class SearchNumericValueInArray {

public static void main(String[] args) {

int a[]={10,20,30,40,50};

int num=30;
boolean flag=false;

for(int i:a)
{
if(num==i)
{
System.out.println(β€œElement found”);
flag=true;
break;
}
}

if(flag==false)
{
System.out.println(β€œElement NOT found”);
}

}

}

——————————————————————————–
package arraysandstrings;

public class SearchStringinArray {

public static void main(String[] args) {

String a[]={β€œabc”, β€œxyz”, β€œpqr”,” mno”};

String search_String=”xyz”;

boolean flag=false;

for(String s:a)
{
if(search_String==s)
{
System.out.println(β€œElement found”);
flag=true;
break;
}
}

if(flag==false)
{
System.out.println(β€œElement NOT found”);
}

}

}
——————————————————————————–
package arrays;

public class SearchStringValueInArray {

public static void main(String[] args) {

String a[]={β€œabc”, β€œxyz”, β€œpqr”,” mno”};

String search_String=”xyz”;

boolean flag=false;

for(String s:a)
{
if(search_String==s)
{
System.out.println(β€œElement found”);
flag=true;
break;
}
}

if(flag==false)
{
System.out.println(β€œElement NOT found”);
}

}

}
——————————————————————————–
package arraysandstrings;

import java.util.Arrays;

public class SearchUsingMethod {

public static void main(String args[])
{
int array[] = {10, 20, 30, 40, 50 }; //Should be in order

System.out.println(Arrays.binarySearch(array, 10));
}
}
——————————————————————————–
package arrays;

public class SingleDimArray {

public static void main(String[] args) {

/*int a[]=new int[5]; // Declared an array with size 5, starting 0, end index 4

//storing/inserting values into array
a[0]=100;
a[1]=200;
a[2]=300;
a[3]=400;
a[4]=500;*/

int a[]={100,200,300,400,500}; // Declare an array without size and store values

System.out.println(a.length); // Prints length of an array

//System.out.println(a[2]); //300

/* for(int i=0;i<=a.length-1;i++)
{
System.out.println(a[i]); //100 200 300 400 500
}*/

//Enhanced for loop/for..each loop
for(int i:a)
{

System.out.println(i);
}

//How to break for loop in the middle
for(int i:a)
{
if(i==400)
{
break;
}

System.out.println(i);
}

}

}
——————————————————————————–

package arraysandstrings;

import java.util.Arrays;

public class SortArray {

public static void main(String[] args) {

//Number Array sorting
int data[] = { 4, 10, 2, 6, 1 };

Arrays.sort(data);

for (int c: data)
{
System.out.println(c);
}

//String array sorting

String data2[] = { β€œz”,”a”,”x”};
Arrays.sort(data2);

for (String c: data2)
{
System.out.println(c);
}

}

}
——————————————————————————–
package arrays;

import java.util.Arrays;

public class SortingUsingSortMethod {

public static void main(String args[])
{
int data[] = { 4, 10, 2, 6, 1 };

Arrays.sort(data);

for (int c: data)
{
System.out.println(c);
}
}
}

——————————————————————————–
package strings;

public class StringMethods {

public static void main(String[] args) {

String s=”welcome”;

//length()
System.out.println(s.length());

//concat()
String s1=”welcome”;
String s2=” to java”;

System.out.println(s1.concat(s2));
System.out.println(β€œwelcome”.concat(” to java”));

//trim()
s=”  Β  welcomeΒ  Β  β€œ;
System.out.println(s);
System.out.println(s.trim());

//charAt()
s=”Welcome”;

System.out.println(s.charAt(4)); //o

//contains() –> return true/false
s=”Welcome to java”;
System.out.println(s.contains(β€œjava”)); //true
System.out.println(s.contains(β€œJava”)); //false

//equals()Β  & equalsIgnoreCase()
s=”Selenium”;
System.out.println(s.equals(β€œSELENIUM”));
System.out.println(s.equalsIgnoreCase(β€œSELENIUM”));

//Replace()
s=”welcome to java”;
System.out.println(s.replace(β€˜e’, β€˜a’)); // replacing single character
System.out.println(s.replace(β€œjava”, β€œselenium”)); // replacing multiple chars

//substring()
s=”Welcome”;
System.out.println(s.substring(2,4)); //lc
System.out.println(s.substring(4,7));Β  //ome

//toLowerCase() && toUpperCase()

s=”WelCome”;

System.out.println(s.toLowerCase()); //welcome
System.out.println(s.toUpperCase()); //WELCOME
}

}
——————————————————————————–

package strings;

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

//WAP to swap strings without using temp/third variable:

String a = β€œHello”;
String b = β€œWorld”;

System.out.println(β€œbefore swapping: β€œ);
System.out.println(β€œthe value of a is:”+ a);
System.out.println(β€œthe value of b is:”+ b);

//1. append a and b:
a = a+b; //HelloWorld

//2. Store initial string a in String b:
b = a.substring(0, a.length()-b.length());

//3. Store initial string b in String a:
a = a.substring(b.length());

System.out.println(β€œthe value of a and b after swapping”);

System.out.println(β€œthe value of a is:”+ a);
System.out.println(β€œthe value of b is:”+ b);

}
}
——————————————————————————–
package strings;

public class StringToIntegerConversion {

public static void main(String[] args) {

// Convert String To Integer Using Integer.parseInt() method
String s = β€œ2015”;

int i = Integer.parseInt(s);

System.out.println(i);

//Convert String To Integer Using Integer.valueOf() method

i = Integer.valueOf(s);

System.out.println(i);
}

}
——————————————————————————–
package arrays;

public class SumOfArray {

public static void main(String args[]){
int[] array = {10, 20, 30, 40, 50, 10};
int sum = 0;

//Advanced for loop
for( int num : array) {
sum = sum+num;
}
System.out.println(β€œSum of array elements is:”+sum);
}

}

package javaBasics;

public class SwappingWithoutThirdVariable {

public static void main(String[] args) {

int x = 5;
int y = 10;

//x = 10, y = 5

//1. with using third var : t
// int t;
// t = x; //5
// x = y; //10
// y = t; //5

//2. without using third var: using + operator
// x = x + y; //15
// y = x – y; //5
// x = x – y; // 10

//3. without using third var: using * operator
// Β x = x * y; //50
// Β y = x / y;Β  //5
// Β x = x / y; //10

System.out.println(x);
System.out.println(y);
}

}

package conditions;
public class SwitchCaseStatement {

public static void main(String[] args) {

int day=10;

switch(day)
{
case 1: System.out.println(β€œSunday”); break;
case 2: System.out.println(β€œMonday”); break;
case 3: System.out.println(β€œTuesday”); break;
case 4: System.out.println(β€œWednesday”); break;
case 5: System.out.println(β€œThursday”); break;
case 6: System.out.println(β€œFriday”); break;
case 7: System.out.println(β€œSaturday”); break;
default: System.out.println(β€œInvalid week number”);
}

}

}
——————————————————————————–
package arrays;

public class TwoDimArray {

public static void main(String[] args) {

int a[][]=new int[3][2];

a[0][0]=100;
a[0][1]=200;

a[1][0]=300;
a[1][1]=400;

a[2][0]=500;
a[2][1]=600;

//int a[][]={ {100,200},{300,400},{500,600}};

System.out.println(a.length); //return number of rows

System.out.println(a[0].length); //returns number of columns

/*for(int r=0; r<=a.length-1;r++) // increment rowsΒ  r=0 1 2 3
{
for(int c=0;c<=a[0].length-1;c++) //c=0 1 2
{
System.out.print(a[r][c]+”  β€œ); //00 =100Β  01=200 10=300Β  11=400 20=500 21=600
}
System.out.println();
}*/

for(int r[]:a)
{
for(int c:r)
{
System.out.println(c);
}
}

}

}
——————————————————————————–
package strings;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

//Program To Find Number Of Characters, Words And Lines In A File

public class WordCountInFile
{
public static void main(String[] args)
{
BufferedReader reader = null;

//Initializing charCount, wordCount and lineCount to 0

int charCount = 0;

int wordCount = 0;

int lineCount = 0;

try
{
//Creating BufferedReader object

reader = new BufferedReader(new FileReader(β€œC:\\SeleniumPractice\\Test.txt”));

//Reading the first line into currentLine

String currentLine = reader.readLine();

while (currentLine != null)
{
//Updating the lineCount

lineCount++;

//Getting number of words in currentLine

String[] words = currentLine.split(” β€œ);

//Updating the wordCount

wordCount = wordCount + words.length;

//Iterating each word

for (String word : words)
{
//Updating the charCount

charCount = charCount + word.length();
}

//Reading next line into currentLine

currentLine = reader.readLine();
}

//Printing charCount, wordCount and lineCount

System.out.println(β€œNumber Of Chars In A File : β€œ+charCount);

System.out.println(β€œNumber Of Words In A File : β€œ+wordCount);

System.out.println(β€œNumber Of Lines In A File : β€œ+lineCount);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
reader.close();Β  Β  Β  Β  Β  Β //Closing the reader
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

Schools 360

Content Writer