Saturday, February 28, 2015

Write a Java program to print the following pattern. [Interview Question]

Write a Java program to print the following pattern.

1
3*2
4*5*6
10*9*8*7
11*12*13*14*15

Given above is a frequently asked question in interviews from Java developers.


class TrianglePattern 
{
 public static void main(String[] args) 
 {
  int n=15;
  int i=1;
  int cnt=1;
  boolean ltr=true;
  while(i<n)
  {
   String s="";
     if(ltr)
      for(int j=0;j<cnt;j++)
    s=s+(s.length()>0?"*":"")+i++;
     else
     for(int j=0;j<cnt;j++)
    s=i++ +(s.length()>0?"*":"")+s;
     cnt=cnt+1;
     ltr=!ltr;
     System.out.println(s);
  }
  
 }
}


Output:





Friday, February 27, 2015

[Webdriver] Selenium test script to scroll and print all items name from a web page.

Write a selenium web driver test script to scroll and print all items name from a web page. Below is an example using FlipKart.

Scenario

  • Log in to Flipkart.

  • Type Samsung in search box and Click search button.

  • Click mobile link.
  • Scroll and print all the names.


Scrolling is the only challenging portion here, until when we have to scroll is the question. In the above scenario we will scroll the web page until no-more-results are displayed.


package chubJava;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FKart_Test {
 
 static WebDriver driver=new FirefoxDriver();
 
 public static void main(String[] args) {
  driver = new FirefoxDriver();
  driver.get("http://www.flipkart.com");
  driver.findElement(By.id("fk-top-search-box")).sendKeys("Nokia");
  driver.findElement(By.xpath("//form[@id='fk-header-search-form']/div/div/div[2]/input[1]")).click();
  driver.findElement(By.partialLinkText("Mobiles")).click();
  JavascriptExecutor jse = (JavascriptExecutor) driver;
  int count = 1500;
  while (!driver.findElement(By.id("no-more-results")).isDisplayed()) {
   count = count + 500;
   jse.executeScript("scroll(0, " + count + ")");
   try {
    Thread.sleep(500);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   if (driver.findElement(By.id("show-more-results")).isDisplayed()) {
    driver.findElement(By.id("show-more-results")).click();
   }
  }

  List<WebElement> al = driver.findElements(By.xpath("//div[contains(@class,'pu-title')]/a"));

  System.out.println(al.size());

  for (int i = 0; i < al.size(); i++) {
   System.out.println(al.get(i).getText());
  }
  
  driver.close();
 }

}

Ping us for any help. Like us at Facebook.

Tuesday, February 17, 2015

Story of the Week-Facts of Life (Cup or Coffee)


The happiest people in the world are not those who have no problems, but those who learn to live with things that are less than perfect.

A group of graduates, highly established in their careers, got together to visit their old university professor.

Conversation soon turned into complaints about stress in work and life. Offering his guests coffee, the professor went to the kitchen and returned with a large pot of coffee and an assortment of cups – porcelain, plastic, glass, crystal, some plain looking, some expensive, some exquisite – telling them to help themselves to the coffee.

When all the students had a cup of coffee, the professor said:
“If you noticed, all the nice looking expensive cups were taken up, leaving behind the simple and cheap ones.

Now consider this:


While it is normal for you to want only the best for yourselves, that is the source of your problems and stress.

Be assured that the cup itself adds no quality to the coffee. In most cases, it is just more expensive and in some cases even hides what we drink. What all of you really wanted was coffee, not the cup, but you consciously went for the best cups… Then you began eyeing each other’s cups.
– Life is the coffee; the jobs, money and position in society are the cups.

– They are just tools to hold and contain Life, and the type of cup we have does not define, nor change the quality of Life we live.

– Sometimes, by concentrating on the cup, we fail to enjoy the coffee God has provided us. Enjoy your coffee!.”
The happiest people don’t have the best of everything. They just make the best of everything.

1. Live simply.
2. Love generously.
3. Care deeply.
4. Speak kindly.
5. Leave the rest to God.

Shine a light & Enjoy the Coffee!!!
You are the miracle, my friend; Your life either shines a light OR casts a shadow!

 

© 2014 DieHardTechy . All rights resevered. Designed by Diehardtechy