Monday, 30 July 2018

Prime Number with different approach, 1) Find prime number withing a range 2) Prime number using math.sqrt 3)Prime number using while loop ? Selenium webdriver experienced interview qus/Java /Prime Number qus/Most asked interview qus

 //PRIME Number within a range
public class PrimeNumber {

public static void main(String[] args) {
  // TODO Auto-generated method stub

  PrimeNumber p = new PrimeNumber();
  System.out.println("====method1========");
  p.isPrime_1(10,100);

  System.out.println("===================");

public void isPrime_1(int s1, int s2) {
         int  flag = 0, i, j;
         System.out.println ("The prime numbers in between the entered limits are :");
         for(i = s1; i <= s2; i++)
         {
             for( j = 2; j < i; j++)
             {
                 if(i % j == 0)
                 {
                     flag = 0;
                     break;
                 }
                 else
                 {
                     flag = 1;
                 }
             }
             if(flag == 1)
             {
                 System.out.println(i);
             }
         }
}

//PRIME Number using math.sqrt

public class PrimeNum {

   public static void main(String[] args) 
  {
       Scanner pnc = new Scanner(System.in);
       System.out.print("Enter a number : ");
       int n = pnc.nextInt();
       if (IsPrimeNumber(n)) 
      {
           System.out.println(n + " is a prime number");
       } else 
          {
           System.out.println(n + " is not a prime number");
          }
   }

   public static boolean IsPrimeNumber(int n) {
       if (n <= 1) 
      {
           return false;
       }
       for (int i = 2; i < Math.sqrt(n); i++) 
      {
           if (n % i == 0)
          {
               return false;
           }
       }
       return true;
   }
}

//PRIME number using while loop 

 public class PrimeNumberWhileLoop {

    public static void main(String[] args) {
         Scanner pn = new Scanner(System.in);
                       System.out.print("Enter a number : ");
                       int number = pn.nextInt();
int i = 2; boolean flag = false; while(i <= number/2) { // condition for not a prime number if(number % i == 0) { flag = true; break; } ++i; } if (!flag) System.out.println(number + " :is a prime number."); else System.out.println(number + " :is not a prime number."); } }

Thursday, 26 July 2018

Give a real time scenario example for Polymorphism/method overloading ? Interview qus selenium webdriver/java/method overloading




🔺 LinkedIn:https://www.linkedin.com/in/sidharth-shukla-77b53145/
🔺 Telegram Group: https://t.me/+FTf_NPb--GQ2ODhl
📌 YouTube channel: https://lnkd.in/gGUGmb-P

Method Overloading in Java 


Method overloading is a feature in object-oriented programming languages like Java that allows a class to have multiple methods with the same name but with different parameters. These methods can perform similar or related tasks but may accept different types or numbers of parameters.

In method overloading:

  1. Methods must have the same name.
  2. Methods must be defined within the same class.
  3. Methods must have different parameter lists (different types or different numbers of parameters).
  4. The return type of the methods may or may not be the same.

When a method is invoked, the compiler determines which version of the overloaded method to call based on the number and types of arguments provided. This decision is made at compile time, and the appropriate method is then called at runtime.

Method overloading provides several benefits:

  • It improves code readability and maintainability by allowing developers to use intuitive method names for different variations of a task.
  • It enhances code reusability by allowing methods to perform similar tasks with different inputs.
  • It provides flexibility in method design, enabling the creation of more intuitive and expressive APIs.

Overall, method overloading is a powerful tool that allows developers to write cleaner, more expressive code while providing flexibility and versatility in method design.


Scenario: Handling Different Types of Click Actions

In Selenium test automation, you often encounter scenarios where you need to perform various types of click actions on web elements. These actions can include simple clicks, double clicks, and right clicks. By using method overloading, you can create flexible and intuitive methods to handle these different types of click actions efficiently.

java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class ClickActionsHelper {

    private WebDriver driver;
    private Actions actions;

    public ClickActionsHelper(WebDriver driver) {
        this.driver = driver;
        this.actions = new Actions(driver);
    }

    // Method for performing a simple click
    public void click(WebElement element) {
        actions.click(element).build().perform();
    }

    // Method for performing a double click
    public void click(WebElement element, boolean doubleClick) {
        if (doubleClick) {
            actions.doubleClick(element).build().perform();
        } else {
            click(element); // Invokes the simple click method
        }
    }

    // Method for performing a right click
    public void click(WebElement element, boolean doubleClick, boolean rightClick) {
        if (rightClick) {
            actions.contextClick(element).build().perform();
        } else {
            click(element, doubleClick); // Invokes the appropriate click method based on parameters
        }
    }
}

In this example, we have a ClickActionsHelper class that encapsulates methods for handling different click actions using Selenium's Actions class. We use method overloading to define multiple click methods with varying parameters:

  • The first click method takes a WebElement parameter and performs a simple click action.
  • The second click method overloads the first one and adds a boolean parameter to indicate whether it should perform a double click. Depending on the value of this parameter, it either performs a double click action or invokes the simple click method.
  • The third click method further overloads the second one and adds another boolean parameter to indicate whether it should perform a right click. Similarly, it either performs a right click action or invokes the appropriate click method based on the parameters provided.

By using method overloading in this manner, we can handle different types of click actions with ease and readability, enhancing the maintainability and flexibility of our test automation code.


GENERIC EXAMPLES



public class testuu{
public static void main(String as[]) throws Throwable {
System.out.println("sum is: " + sum(14,0) + " sum of three params is: " + sum(1,2,3));
}
public static int sum(int a, int b) throws Exception {
int sum = a+b;
return sum;
}
private static int sum(int a, int b, int c) {
int sum = a+b+c;
return sum;
}
}

Output:
sum is: 14
sum of three params is: 6

//In above example we have two method with same name as "sum" but we have pass different parameters which is an perfect example for method overloading or polymorphism.


*****

📌1:1 Call on Career Guidance: https://lnkd.in/ddayTwnq

****

📣 Looking to understand the advanced concepts in Java with practical examples from Test Automation ?

Learn SDET or Automation Testing along with API Testing, Rest Assured, Selenium, Appium, Jenkins, GIT, Docker, Generative AI with live doubt sessions, explore the details here: https://lnkd.in/giCxnJJ7.

All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...