20 Java Program
20 Java Program
public class
FinalReverseWithoutUsingString
Methods {
Output :- noitamotuA
Q #2) Write a Java Program
to reverse a string without
using String inbuilt function
reverse().
public class
FinalReverseWithoutUsingIn
builtFunction { public static
void main(String[] args)
{
String str = "Saket Saurav";
charchars[]=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]);
}
}
}
import java.util.Scanner;
public class
SwapTwoNumbers {
import java.util.Scanner;
class
SwapTwoNumberWithoutThi
rdVariable {
public static void main(String
args[])
{ int x, y;
System.out.println("Enter x
and y"); Scanner in = new
Scanner(System.in); x =
in.nextInt();
y = in.nextInt();
System.out.println("Before
Swapping\nx = "+x+"\ny =
"+y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After
Swapping without third
variable\nx = "+x+"\ny = "+y);
}
Q #5) Write a Java Program
to count the number of
words in a string using
HashMap.
Answer: Here, we are
initializing a string variable
str and making use of the
string builder class.
public class
FinalReverseWithoutUsingSt
ringMethods { 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); } }
Output:
noitamotuA
Output:
varuaS tekaS
Method 2:
Output:
Method 3:
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); }
}
Output:
import java.util.Scanner;
public class
SwapTwoNumbers { public
static void main(String[] args)
{ // TODO Auto-generated
method stub int x, y, temp;
System.out.println("Enter x
and y"); Scanner in = new
Scanner(System.in); x =
in.nextInt(); y = in.nextInt();
System.out.println("Before
Swapping" + x + y); temp = x;
x = y; y = temp;
System.out.println("After
Swapping" + x + y); } }
Output:
Enter x and y
45
98
Before Swapping4598
After Swapping9845
import java.util.Scanner;
class
SwapTwoNumberWithoutThi
rdVariable { public static void
main(String args[]) { int x, y;
System.out.println("Enter x
and y"); Scanner in = new
Scanner(System.in); x =
in.nextInt(); y = in.nextInt();
System.out.println("Before
Swapping\nx = "+x+"\ny =
"+y); x = x + y; y = x - y; x = x -
y; System.out.println("After
Swapping without third
variable\nx = "+x+"\ny = "+y);
}}
Output:
Enter x and y
45
98
Before Swapping
x = 45
y = 98
After Swapping without a
third variable
x = 98
y = 45
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 Saket
Saket"; String[] split =
str.split(" ");
HashMap<String,Integer>
map = new
HashMap<String,Integer>();
for (int i=0; i<split.length; 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);
}
}
Output:-{Saket=2, by=1,
this=1, This=1, is=2, done=1}
Q #6) 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&g
t;();
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());
}
}
}
Output :-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
Q #7) 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");
}
}
Output :-445
445number is not a prime
Q #8) 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");
}
}
Output:-
For String-
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("Fibonacc
i 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()
}
}
}
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("Advance
d 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));
}}}
Output:- 3
While Loop:
20
30
40
Advanced For Loop:
20
30
40
For Loop:
20
30
40
Q #11) Write a Java
Program to demonstrate an
explicit wait condition
check.
public class
explicitWaitConditionCheck
{
public static void
main(String[] args) { // TODO
Auto-generated method
stub
System.setProperty("webdriv
er.chrome.driver",
"C:\\webdriver\\chromedrive
r.exe"); ChromeOptions
options = new
ChromeOptions();
options.addArguments("--dis
able-arguments"); WebDriver
driver = new ChromeDriver();
driver.manage().window().ma
ximize();
driver.manage().timeouts().im
plicitlyWait(20,
TimeUnit.SECONDS);
driver.navigate().to("https://w
ww.google.com");
WebElement element =
driver.findElement(By.name("
q"));
element.sendKeys("Testing");
element.submit();
WebDriverWait wait = new
WebDriverWait(driver, 20);
WebElement element2 =
wait.until(ExpectedCondition
s.visibilityOfElementLocated(
By.partialLinkText("Software
testing - Wikipedia")));
element2.click();
}
}
Q #12) Write a Java
Program to demonstrate
Scroll up/ Scroll down.
Output:
Starting ChromeDriver
2.38.551601
(edb21f07fc70e9027c746e
dd3201443e011a61ed) on
port 16163
Only local connections are
allowed.
4
https://support.google.com/
chrome/answer/6130773?hl
=en-GB
Learn more
https://support.google.com/
accounts?hl=en-GB
Help
https://accounts.google.co
m/TOS?loc=IN&hl=en-GB&p
rivacy=true
Privacy
https://accounts.google.co
m/TOS?loc=IN&hl=en-GB
Terms
Q #14) Write a Selenium
code to switch to the
previous tab.
import
java.awt.AWTException;
import java.awt.Robot;
import
java.awt.event.KeyEvent;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.By;
import
org.openqa.selenium.Keys;
import
org.openqa.selenium.WebDr
iver; import
org.openqa.selenium.WebEl
ement; import
org.openqa.selenium.chrom
e.ChromeDriver; public class
PreviousTab { public static
void main(String[] args)
throws AWTException { //
TODO Auto-generated
method stub
System.setProperty("webdriv
er.chrome.driver",
"C:\\webdriver\\chromedrive
r.exe"); WebDriver driver =
new ChromeDriver();
driver.manage().window().ma
ximize();
driver.manage().timeouts().im
plicitlyWait(20,
TimeUnit.SECONDS);
driver.get("https://www.googl
e.com"); WebElement
element1 =
driver.findElement(By.name("
q"));
element1.sendKeys("softwar
e testing help");
element1.sendKeys(Keys.EN
TER); String a =
Keys.chord(Keys.CONTROL,
Keys.RETURN);
driver.findElement(By.partialL
inkText("Software Testing
Help - A Must Visit Software
Testing Portal")).sendKeys(a);
Robot robot = new Robot(); //
instantiated robot class
robot.keyPress(KeyEvent.VK
_CONTROL); // with robot
class you can easily achieve
anything if you know the
shortcut keys
robot.keyPress(KeyEvent.VK
_2); // here, we have just
pressed ctrl+2
robot.keyRelease(KeyEvent.
VK_CONTROL); // once we
press and release ctrl+2, it
will go to the second tab.
robot.keyRelease(KeyEvent.
VK_2); //if you again want to
go back to first tab press
and release vk_1
}
}
Q #15) 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("Sakkett");
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:
public class
SecondHighestNumberInArr
ay {
public static void
main(String[] args)
{ int arr[] = { 100,14, 46, 47,
94, 94, 52, 86, 36, 94, 89 };
int largest = 0;
int secondLargest = 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)
{ secondLargest = arr[i];
}
}
System.out.println("\nSecond
largest number is:" +
secondLargest);
System.out.println("Largest
Number is: " +largest);
}
}
Output:
class Armstrong{
public static void
main(String[] args) {
int c=0,a,temp;
int n=153;//It is the number
to check Armstrong
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("armstron
g number");
else
System.out.println("Not
armstrong number");
}
}
Output :-armstrong number
Q #18) 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 = "Saket Saurav is
a QualityAna list"; //1. Using
replaceAll() Method String
str2 = str1.replaceAll("\\s", "");
System.out.println(str2);
}
}
}
Output:-SaketSauravisaQua
lityAnalist
Q #19) 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 = "Saket Saurav is
an Autom ation Engi ne er";
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:-SaketSauravisanAu
tomationEngineer
Q #20) Write a Java
Program to read an excel.