Java Basics -2
Java Basics -2 ~30 mins

Week1 - Day 2

package week1.day2;

public class CountCharacters {

public static void main(String[] args) {
// Find the count of given character in the String
// Input String -> "testleaf"
// Input character -> 'e'
// Output -> how many 'e' available in testleaf -> 2

String input = "cognizant";
char ch = 'e';
int count = 0;

char[] charArray = input.toCharArray();
for (int i = 0; i < charArray.length; i++) {
if(charArray[i] == ch) {
count = count+1;
}
}
System.out.println(count);


}

}
package week1.day2;

public class Even {

public static void main(String[] args) {

Even even = new Even();
even.printEvenOrOdd(100, false);

}

public void printEvenOrOdd(int max, boolean even) {
for (int i = 1; i <= max; i++) {
if(i%2 == 0 && even) {
System.out.println("The even number is "+i);
} else {
System.out.println("The odd number is "+i);
}
}
}


}
package week1.day2;

import java.util.Arrays;

public class FindDuplicateNumber {

// Monday to Thursday -> 1 hour -> 7 to 8 PM IST
public static void main(String[] args) {

int[] numbers = {2,5,3,7,4,7,4,1};
Arrays.sort(numbers);
for (int i = 0; i < numbers.length; i++) {
if(numbers[i] == numbers[i+1]) {
System.out.println("Duplicate number "+numbers[i]);
break;
}
}

}

}
package week1.day2;

public class GetAllNumbers {

public static void main(String[] args) {

String sentence = "Amazon has 11412 employees at chennai";
// I wanted to get only the numbers here. => 11412
System.out.println(sentence.replaceAll("[^0-9]", ""));
// What need to be deleted? ->
// anything which is not a number to be deleted
// How to find which ones are not number -> regular expression
// -> \\D or [^0-9]


}

}
package week1.day2;

import java.util.Arrays;

public class LearnArrays {

public static void main(String[] args) {

String mentor1 = "Sheriba";
String mentor2 = "Divya";
String mentor3 = "Bowya";
String mentor4 = "Naveen";
String mentor5 = "Kruthi";

// Array -> Can store similar data under one variable
String[] mentors = {"Sheriba", "Divya","Bowya", "Naveen", "Kruthi"};
System.out.println(mentors.length);

/*for (int i = 0; i < mentors.length; i++) {
System.out.println(mentors[i]);
}*/

Arrays.sort(mentors);
for (String eachMentor : mentors) {
System.out.println(eachMentor);
}


// Array work by the index -> starts at 0
//System.out.println(mentors[2]);

// Limitation: You should declare the size upfront!!
// You cannot increase or decrease the size of array later
String[] courses = new String[14];
courses[0] = "Selenium"; // Assigned value for the first index
courses[1] = "Rest API Automation";
courses[2] = "Microservices Automation";
// Other index -> default value as null
//System.out.println(courses[4]);


}

}






package week1.day2;

public class LearnString {

public static void main(String[] args) {



/*
* String can be created by 2 ways
* 1) Using object
* 2) Using literal (like variables)
*
* Difference: to do with memory allocation
*
*/


String title = "Leaftaps";
String username = "demosalesmanager";
String dropdownOption = "Marketing";


// What a class can have? (Members of the class)
/*
* 1) Methods
* 2) Variables
* 3) Constructor
*
*/


// 11 Methods -> Essential automation
// String -> class -> concatenation of characters -> '1' , '2', 'a'
/*
* 1) How many characters are available in a String?
* Method: length() -> int
* 2) To convert the String to character array
* Method: toCharArray() -> char[]
* 3) To find whether given character/string exist or not?
* Method: contains('e') -> boolean
* 4) Convert to upper or lowercase
* Methods: toLowerCase() -> String with all lowercase
* toUpperCase() -> String to uppercase
* 5) first index of the given character
* indexOf('e') -> index starting from 0
* lastIndexOf()
*
* 6) find the character that is present in the index
* charAt(0) -> the character is living in the given index
*
* 7) Compare 2 string -> equals, starts-with, ends-with
* String is immutable !! Hence == does not work for comparison
* equals -> return boolean
* equalsIgnoreCase -> compare without case sensitivity\
* startsWith -> comparing starting text
* endsWith -> comparing ending text
* contains -> compare any text within full String
*
* 8) Replace given character or String with another
* replace -> replace the given character or String with another
* replaceAll -> replace the pattern
*
* 9) Split the given string by specific delimiter
* split("delimiter") -> return String[]
*
* 10) Convert String to Integer
* Integer.parseInt("") -> number -> int
*
* 11) Find the value of other data type from String
* String.valueOf()
*/


// String name = new String("TestLeaf $");
String name = "TestLeaf $#";
/*System.out.println(name.length());
System.out.println(name.contains("k"));
System.out.println(name.toLowerCase());
System.out.println(name.toUpperCase());
System.out.println(name.indexOf("ea")); // if it does not -> -1
System.out.println(name.lastIndexOf("es")); // if it does not -> -1
*/

//System.out.println(name.charAt(1));
// I want to print the last character
// What you need to find first? find the length -> length()
// character at length()-1

int lastIndex = name.length()-1;
//System.out.println(name.charAt(lastIndex));

String myPreviousCompany = "TestLeaff"; // TestLeaves // f -> ves
String presentCompany = "TestLeaf Software";
if(presentCompany.startsWith(myPreviousCompany)) {
//System.out.println("I worked for only one org/company");
} else {
//System.out.println("You have switched companies");
}

//System.out.println(myPreviousCompany.replace("f", "ves"));

String sentence = "I am learning Java well so far";
String[] split = sentence.split(" ");
System.out.println(split.length);














// when it is array -> length as property
// when it is String -> length() as method
System.out.println(Integer.parseInt("91234"));
// getText -> String ->

boolean isPuncture = true;
System.out.println(String.valueOf(isPuncture));



}

}
package week1.day2;

public class Loops {

public static void main(String[] args) {

// I am reading excel -> 3 test data -> all data
// which loop should I use? for (iteration)

// Browser -> loading a page -> page is keep loading
// when it is fully loaded, click the button
// while(browserLoaded == true || maxTime > 30)
// if -> condition
// I want to run same code multiple times !!

// First time i will check whether is image is loaded
// if it is loaded, continue to next code
// else, continue to wait
boolean imageLoaded = false;
do {
// check if the image is loaded => imageLoaded
}while(imageLoaded);



// i = 1 (Starting point)
// i = 10 (Ending point)
// i++ -> will be incremented by one at a time
// We are planning to run selenium tests for different data
// When I running on friday, I will stop when I reach 5 rounds !!
int day = 3; // day 1 -> Monday
/*for (int i = 1; i <= 10; i++) {
if(day == 5 && i > 5) {
break; // break is keyword to come out of the loop !!
}
System.out.println("Running the round: "+i);
} */


/*boolean browserLoaded = true;
int maxTime = 0;
while(!browserLoaded || maxTime < 30) {
System.out.println("I am waiting");
maxTime = maxTime + 5;
}*/


boolean raining = true;

// Minimum -> 0 iteration
while(!raining) {
System.out.println("I am running");
raining = true;
}

// Minimum -> 1 iteration
do {
System.out.println();
}while(!raining);










}

}
package week1.day2;

public class Question1 {

/*
* Given any number, print the following
* If the number is divisible by 3 -> print "fizz"
* If the number is divisible by 5 -> print "bizz"
* If the number is divisible by 3 & 5 -> print "fizzbizz"
* Else do nothing
*
* -5 , 0
*
* 13 -> 3 or 5 -> will not print anything
* 10 -> 5 -> bizz
* 9 -> 3 -> fizz
* 15 -> 3 & 5 -> fizzbizz
*
* Psuedocode
* 1) Take one of the number (int) -> negative, positive
* 2) if -> 3 & 5 first , else if either 3 or 5 else do nothing
*
*
*/

public static void main(String[] args) {

int input = -11;

if(input%15 == 0) {
System.out.println("fizzbizz");
} else if(input%3 == 0) {
System.out.println("fizz");
} else if(input%5 == 0) {
System.out.println("bizz");
}



}












}
package week1.day2;

public class ReverseString {

public static void main(String[] args) {
// Problem Statement
// Given a string, reverse the string
// Example: testleaf -> faeltset

// Psuedocode
// How to each charcter -> character array
// Loop through every character from the right to left
// concatenate each character inside the loop
// finally, print !!

String input = "testleaf"; // index ->0 , lastIndex -> 8-1 => 7
// 7 -> 6 => 5 => 4 ... 0
// How to convert to character array
char[] everyChar = input.toCharArray();
for (int i = everyChar.length-1; i >= 0; i--) {
System.out.print(everyChar[i]);
}




}

}
package week1.day2;

import java.util.Iterator;

public class ReveseWords {

public static void main(String[] args) {

String company = "Amazon Development Center";
// Amazon
// Development
// Center
// Center Development Amazon

/*
* Psuedocode:
*
* 1) Convert the given String into words
* 2) traverse from right to left
* 3) keep printing with white space between them
*
*/


// Convert the given String into words
String[] words = company.split(" ");

// Traverse from right to left
for (int i = words.length-1; i >= 0 ; i--) {
System.out.print(words[i]+" ");
}


}

}
package week1.day2;

import java.util.Arrays;

public class SortNumbers {

public static void main(String[] args) {

// Find the first missing number in the array
// Input {8,6,5,4,2,1,3} => sort
// Find the missing number -> 7

int[] input = {8,6,5,4,2,1,3};
Arrays.sort(input);
//System.out.println(Arrays.toString(input));
for (int i = 0; i < input.length; i++) {
if(input[i]+1 != input[i+1]) {
System.out.println("The missing number is "+(input[i]+1));
break;
}
}

// Find the smallest valued duplicate number
// int[] numbers = {2,5,3,7,2,7,2,1};

}

}