Write a Java Program to reverse a string without using String inbuilt function.
public class FinalReverseWithoutUsingStringMethods {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "Automation";
StringBuilder str2 = new StringBuilder();
str2.append(str);
str2 = str2.reverse(); // used string builder to reverse
System.out.println(str2);
}
}
Write a Java Program to reverse a string without using String inbuilt function reverse()
public class FinalReverseWithoutUsingInbuiltFunction {
public static void main(String[] args) {
String str = "Saket Saurav";
char chars[] = str.toCharArray(); // converted to character array and printed in reverse order
for(int i= chars.length-1; i>=0; i--) {
System.out.print(chars[i]);
}
}
}
Through another way , we can achieve
import java.util.Scanner;
public class ReverseSplit {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str;
Scanner in = new Scanner(System.in);
System.out.println("Enter your String");
str = in.nextLine();
String[] token = str.split(""); //used split method to print in reverse order
for(int i=token.length-1; i>=0; i--)
{
System.out.print(token[i] + "");
}
}
}
One another way , through which we can do this
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
// TODO Auto-generated method stub
String original, reverse = "";
System.out.println("Enter the string to be reversed");
Scanner in = new Scanner(System.in);
original = in.nextLine();
int length = original.length();
for(int i=length-1; i>=0; i--) {
reverse = reverse + original.charAt(i); //used inbuilt method charAt() to reverse the string
}
System.out.println(reverse);
}
}
Swap two number with and without using third number
1) with
temp = x;
x = y;
y = temp;
2) without
x = x + y;
y = x - y;
x = x - y;
Write a Java Program to count the number of words in a string using HashMap.
import java.util.HashMap;
public class FinalCountWords {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "This this is is done by Nishant Nishant";
String[] split = str.split(" ");
HashMap<String,Integer> map = new HashMap<String,Integer>();
for (int i=0; i<split.length-1; i++) {
if (map.containsKey(split[i])) {
int count = map.get(split[i]);
map.put(split[i], count+1);
}
else {
map.put(split[i], 1);
}
}
System.out.println(map);
}
}
o/p --- {Nishant=2, by=1, this=1, This=1, is=2, done=1}
Write a Java Program to iterate HashMap using While and advance for loop ?
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class HashMapIteration {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer,String> map = new HashMap<Integer,String>();
map.put(2, "Saket");
map.put(25, "Saurav");
map.put(12, "HashMap");
System.out.println(map.size());
System.out.println("While Loop:");
Iterator itr = map.entrySet().iterator();
while(itr.hasNext()) {
Map.Entry me = (Map.Entry) itr.next();
System.out.println("Key is " + me.getKey() + " Value is " + me.getValue());
}
System.out.println("For Loop:");
for(Map.Entry me2: map.entrySet()) {
System.out.println("Key is: " + me2.getKey() + " Value is: " + me2.getValue());
}
}
}
3
While Loop:
Key is 2 Value is Saket
Key is 25 Value is Saurav
Key is 12 Value is HashMap
For Loop:
Key is: 2 Value is: Saket
Key is: 25 Value is: Saurav
Key is: 12 Value is: HashMap
Write a Java Program to find whether a number is prime or not.
import java.util.Scanner;
public class Prime {
public static void main(String[] args) {
// TODO Auto-generated method stub
int temp, num;
boolean isPrime = true;
Scanner in = new Scanner(System.in);
num = in.nextInt();
in.close();
for (int i = 2; i<= num/2; i++) {
temp = num%i;
if (temp == 0) {
isPrime = false;
break;
}
}
if(isPrime)
System.out.println(num + "number is prime");
else
System.out.println(num + "number is not a prime");
}
}
Write a Java Program to find whether a string or number is palindrome or not.
import java.util.Scanner;
public class Palindrome {
public static void main (String[] args) {
String original, reverse = "";
Scanner in = new Scanner(System.in);
int length;
System.out.println("Enter the number or String");
original = in.nextLine();
length = original.length();
for (int i =length -1; i>=0; i--) {
reverse = reverse + original.charAt(i);
}
System.out.println("reverse is:" +reverse);
if(original.equals(reverse))
System.out.println("The number is palindrome");
else
System.out.println("The number is not a palindrome");
}
}
Write a Java Program for Fibonacci series.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int num, a = 0,b=0, c =1;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of times");
num = in.nextInt();
System.out.println("Fibonacci Series of the number is:");
for (int i=0; i<=num; i++) {
a = b;
b = c;
c = a+b;
System.out.println(a + ""); //if you want to print on the same line, use print()
}
}
}
Write a Java Program to iterate ArrayList using for-loop, while-loop, and advance for-loop.
import java.util.*;
public class arrayList {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("20");
list.add("30");
list.add("40");
System.out.println(list.size());
System.out.println("While Loop:");
Iterator itr = list.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("Advanced For Loop:");
for(Object obj : list) {
System.out.println(obj);
}
System.out.println("For Loop:");
for(int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}
}
}
Write a Java Program to demonstrate explicit wait condition check.
There are two main types of wait – implicit and explicit. (We are not considering Fluent wait in this program)
Implicit wait is those waits which are executed irrespective of any condition. In the below program, you can see that it is for Google Chrome and we have used some inbuilt methods to set the property, maximizing window, URL navigation, and web element locating.
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element2 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Software testing - Wikipedia")));
element2.click();
In the above piece of code, you can see that we have created an object wait for WebDriverWait and then we have searched for WebElement called element2.
package Codes;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class explicitWaitConditionCheck {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\webdriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-arguments");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.navigate().to("https://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Testing");
element.submit();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element2 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Software testing - Wikipedia")));
element2.click();
}
}
Write a Java Program to find the duplicate characters in a string.
public class DuplicateCharacters {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = new String("nishant");
int count = 0;
char[] chars = str.toCharArray();
System.out.println("Duplicate characters are:");
for (int i=0; i<str.length();i++) {
for(int j=i+1; j<str.length();j++) {
if (chars[i] == chars[j]) {
System.out.println(chars[j]);
count++;
break;
}
}
}
}
}
Output:
Duplicate characters are:
n
Write a Java Program to find the second highest number in an array.
public class SecondHighestNumberInArray {
public static void main(String[] args)
{
int arr[] = { 14, 46, 47, 94, 94, 52, 86, 36, 94, 89 };
int largest = arr[0];
int secondLargest = arr[0];
System.out.println("The given array is:");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + "\t");
}
for (int i = 0; i < arr.length; i++)
{
if (arr[i] > largest)
{
secondLargest = largest;
largest = arr[i];
}
else if (arr[i] > secondLargest && arr[i] != largest)
{
secondLargest = arr[i];
}
}
System.out.println("\nSecond largest number is:" + secondLargest);
}
}
Output:
The given array is:
14 46 47 45 92 52 48 36 66 85
Second largest number is:85
Write a Java Program to remove all white spaces from a string with using replace().
class RemoveWhiteSpaces
{
public static void main(String[] args)
{
String str1 = "Nishant Sharma is a QualityDev loper";
//1. Using replaceAll() Method
String str2 = str1.replaceAll("\\s", "");
System.out.println(str2);
}
}
}
o/p
NishantSharmaisaQualityDevloper
Write a Java Program to remove all white spaces from a string without using replace()
class RemoveWhiteSpaces
{
public static void main(String[] args)
{
String str1 = "Nishant Sharma is a QualityDev loper";
char[] chars = str1.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < chars.length; i++)
{
if( (chars[i] != ' ') && (chars[i] != '\t') )
{
sb.append(chars[i]);
}
}
System.out.println(sb);
}
}
Output:
NishantSharmaisaQualityDevloper
public class FinalReverseWithoutUsingStringMethods {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "Automation";
StringBuilder str2 = new StringBuilder();
str2.append(str);
str2 = str2.reverse(); // used string builder to reverse
System.out.println(str2);
}
}
Write a Java Program to reverse a string without using String inbuilt function reverse()
public class FinalReverseWithoutUsingInbuiltFunction {
public static void main(String[] args) {
String str = "Saket Saurav";
char chars[] = str.toCharArray(); // converted to character array and printed in reverse order
for(int i= chars.length-1; i>=0; i--) {
System.out.print(chars[i]);
}
}
}
Through another way , we can achieve
import java.util.Scanner;
public class ReverseSplit {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str;
Scanner in = new Scanner(System.in);
System.out.println("Enter your String");
str = in.nextLine();
String[] token = str.split(""); //used split method to print in reverse order
for(int i=token.length-1; i>=0; i--)
{
System.out.print(token[i] + "");
}
}
}
One another way , through which we can do this
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
// TODO Auto-generated method stub
String original, reverse = "";
System.out.println("Enter the string to be reversed");
Scanner in = new Scanner(System.in);
original = in.nextLine();
int length = original.length();
for(int i=length-1; i>=0; i--) {
reverse = reverse + original.charAt(i); //used inbuilt method charAt() to reverse the string
}
System.out.println(reverse);
}
}
Swap two number with and without using third number
1) with
temp = x;
x = y;
y = temp;
2) without
x = x + y;
y = x - y;
x = x - y;
Write a Java Program to count the number of words in a string using HashMap.
import java.util.HashMap;
public class FinalCountWords {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "This this is is done by Nishant Nishant";
String[] split = str.split(" ");
HashMap<String,Integer> map = new HashMap<String,Integer>();
for (int i=0; i<split.length-1; i++) {
if (map.containsKey(split[i])) {
int count = map.get(split[i]);
map.put(split[i], count+1);
}
else {
map.put(split[i], 1);
}
}
System.out.println(map);
}
}
o/p --- {Nishant=2, by=1, this=1, This=1, is=2, done=1}
Write a Java Program to iterate HashMap using While and advance for loop ?
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class HashMapIteration {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer,String> map = new HashMap<Integer,String>();
map.put(2, "Saket");
map.put(25, "Saurav");
map.put(12, "HashMap");
System.out.println(map.size());
System.out.println("While Loop:");
Iterator itr = map.entrySet().iterator();
while(itr.hasNext()) {
Map.Entry me = (Map.Entry) itr.next();
System.out.println("Key is " + me.getKey() + " Value is " + me.getValue());
}
System.out.println("For Loop:");
for(Map.Entry me2: map.entrySet()) {
System.out.println("Key is: " + me2.getKey() + " Value is: " + me2.getValue());
}
}
}
3
While Loop:
Key is 2 Value is Saket
Key is 25 Value is Saurav
Key is 12 Value is HashMap
For Loop:
Key is: 2 Value is: Saket
Key is: 25 Value is: Saurav
Key is: 12 Value is: HashMap
Write a Java Program to find whether a number is prime or not.
import java.util.Scanner;
public class Prime {
public static void main(String[] args) {
// TODO Auto-generated method stub
int temp, num;
boolean isPrime = true;
Scanner in = new Scanner(System.in);
num = in.nextInt();
in.close();
for (int i = 2; i<= num/2; i++) {
temp = num%i;
if (temp == 0) {
isPrime = false;
break;
}
}
if(isPrime)
System.out.println(num + "number is prime");
else
System.out.println(num + "number is not a prime");
}
}
Write a Java Program to find whether a string or number is palindrome or not.
import java.util.Scanner;
public class Palindrome {
public static void main (String[] args) {
String original, reverse = "";
Scanner in = new Scanner(System.in);
int length;
System.out.println("Enter the number or String");
original = in.nextLine();
length = original.length();
for (int i =length -1; i>=0; i--) {
reverse = reverse + original.charAt(i);
}
System.out.println("reverse is:" +reverse);
if(original.equals(reverse))
System.out.println("The number is palindrome");
else
System.out.println("The number is not a palindrome");
}
}
Write a Java Program for Fibonacci series.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int num, a = 0,b=0, c =1;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of times");
num = in.nextInt();
System.out.println("Fibonacci Series of the number is:");
for (int i=0; i<=num; i++) {
a = b;
b = c;
c = a+b;
System.out.println(a + ""); //if you want to print on the same line, use print()
}
}
}
Write a Java Program to iterate ArrayList using for-loop, while-loop, and advance for-loop.
import java.util.*;
public class arrayList {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("20");
list.add("30");
list.add("40");
System.out.println(list.size());
System.out.println("While Loop:");
Iterator itr = list.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("Advanced For Loop:");
for(Object obj : list) {
System.out.println(obj);
}
System.out.println("For Loop:");
for(int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}
}
}
Write a Java Program to demonstrate explicit wait condition check.
There are two main types of wait – implicit and explicit. (We are not considering Fluent wait in this program)
Implicit wait is those waits which are executed irrespective of any condition. In the below program, you can see that it is for Google Chrome and we have used some inbuilt methods to set the property, maximizing window, URL navigation, and web element locating.
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element2 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Software testing - Wikipedia")));
element2.click();
In the above piece of code, you can see that we have created an object wait for WebDriverWait and then we have searched for WebElement called element2.
package Codes;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class explicitWaitConditionCheck {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\webdriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-arguments");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.navigate().to("https://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Testing");
element.submit();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element2 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Software testing - Wikipedia")));
element2.click();
}
}
Write a Java Program to find the duplicate characters in a string.
public class DuplicateCharacters {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = new String("nishant");
int count = 0;
char[] chars = str.toCharArray();
System.out.println("Duplicate characters are:");
for (int i=0; i<str.length();i++) {
for(int j=i+1; j<str.length();j++) {
if (chars[i] == chars[j]) {
System.out.println(chars[j]);
count++;
break;
}
}
}
}
}
Output:
Duplicate characters are:
n
Write a Java Program to find the second highest number in an array.
public class SecondHighestNumberInArray {
public static void main(String[] args)
{
int arr[] = { 14, 46, 47, 94, 94, 52, 86, 36, 94, 89 };
int largest = arr[0];
int secondLargest = arr[0];
System.out.println("The given array is:");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + "\t");
}
for (int i = 0; i < arr.length; i++)
{
if (arr[i] > largest)
{
secondLargest = largest;
largest = arr[i];
}
else if (arr[i] > secondLargest && arr[i] != largest)
{
secondLargest = arr[i];
}
}
System.out.println("\nSecond largest number is:" + secondLargest);
}
}
Output:
The given array is:
14 46 47 45 92 52 48 36 66 85
Second largest number is:85
Write a Java Program to remove all white spaces from a string with using replace().
class RemoveWhiteSpaces
{
public static void main(String[] args)
{
String str1 = "Nishant Sharma is a QualityDev loper";
//1. Using replaceAll() Method
String str2 = str1.replaceAll("\\s", "");
System.out.println(str2);
}
}
}
o/p
NishantSharmaisaQualityDevloper
Write a Java Program to remove all white spaces from a string without using replace()
class RemoveWhiteSpaces
{
public static void main(String[] args)
{
String str1 = "Nishant Sharma is a QualityDev loper";
char[] chars = str1.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < chars.length; i++)
{
if( (chars[i] != ' ') && (chars[i] != '\t') )
{
sb.append(chars[i]);
}
}
System.out.println(sb);
}
}
Output:
NishantSharmaisaQualityDevloper
No comments:
Post a Comment