Showing posts with label Core Java Example. Show all posts
Showing posts with label Core Java Example. Show all posts

Tuesday, May 27, 2014

Multiply Matrix


import java.util.Scanner;

public class MultipleOfMatrix{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("enter the number of rows: ");
        int r = in.nextInt();
        System.out.println("enter the number of columns: ");
        int c = in.nextInt();
        int[][] m1 = new int[r][c];
        int[][] m2 = new int[c][r];
        int[][] m3 = new int[r][r];
        System.out.println("Enter the numbers for the 1st matrix: ");
        m1 = getNumOfMatrix(in, r, c, m1);
        System.out.println("Enter the numbers for the 2nd matrix: ");
        m2 = getNumOfMatrix(in, c, r, m2);
        m3 = multiplyMatrix(r,r,c,m1,m2,m3);
       
        System.out.println("1st matrix m1: ");
        printMatrix(m1);
        System.out.println("2nd matrix m2: ");
        printMatrix(m2);
        System.out.println("Multiplication of both the matrix m1 and m2: ");
        printMatrix(m3);
    }

    public static int[][] getNumOfMatrix(Scanner in, int r, int c, int[][] m){
        for(int i=0; i<r; i++){
            for(int j=0; j<c; j++){
                m[i][j] = in.nextInt();
            }
        }
        return m;
    }

    public static int[][] multiplyMatrix(int r1, int c2, int c1,int[][] m1, int[][] m2, int[][] m3){
        int sum=0;
        for(int i=0; i<r1; i++){
            for(int j=0; j<c2; j++){
                sum=0;
                for(int k=0; k<c1; k++){
                    sum = sum+m1[i][k]*m2[k][j];
                }
                m3[i][j] = sum;
            }
        }
        return m3;
    }
    public static void printMatrix(int[][] m){
        for(int[] i: m){
            for(int j: i){
                System.out.print(" "+j);
            }
            System.out.println();
        }
    }
}


Output-
enter the number of rows:
3
enter the number of columns:
2
Enter the numbers for the 1st matrix:
1
2
3
4
5
6
Enter the numbers for the 2nd matrix:
2
3
4
5
6
7
1st matrix m1:
 1 2
 3 4
 5 6
2nd matrix m2:
 2 3 4
 5 6 7
Multiplication of both the matrix m1 and m2:
 12 15 18
 26 33 40
 40 51 62

Bubble Sort

package SeleniumMakeItEasy;

import org.apache.commons.lang3.ArrayUtils;

public class BubbleSort{
    public static void main(String[] args){
    int[] a = {2,3,2,5,3,3,6,1,2,5};
    int l = a.length;
   
    for(int i=0;i<l; i++){
       
        for(int j=0; j<l-1; j++){
            if(a[j]>a[j+1]){
                a[j] = a[j] + a[j+1];
                a[j+1] = a[j] - a[j+1];
                a[j] = a[j] - a[j+1];
            }else if(a[j]==a[j+1]){
                a = ArrayUtils.remove(a,j);
                l = a.length;
            }
        }
    }
    for(int s: a){
        System.out.println(s);
    }

 }
}

Sunday, May 25, 2014

write a program so that whenever you create a object, you will get to know how many object u have created.

public class ObjectCount {
    static int numOfObj;
    ObjectCount(){
        numOfObj = numOfObj+1;
        System.out.println("Total Number of object created: "+numOfObj);
    }
    public static void main(String[] args) {
        ObjectCount obj1 = new ObjectCount();
        ObjectCount obj2 = new ObjectCount();
        ObjectCount obj3 = new ObjectCount();
    }
}

Monday, May 12, 2014

How to get the dropdown value in notepad ?

Note: FileWriter("path of the directory with filename.txt");

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class DropdownNotepad extends Thread {
    public static WebDriver driver;
    public static void main(String[] args) throws IOException {
        driver = new FirefoxDriver();
        driver.get("http://www.etouch.net/home/index.html");
        WebElement service = driver.findElement(By.xpath("//a[text()='Services']"));
        Actions act = new Actions(driver);
        act.moveToElement(service).perform();
        List<WebElement> dropdown = driver.findElements(By.xpath("//li[@id='services']//ul//ul/li"));
        System.out.println(dropdown.size());
       
        FileWriter fileWriter = new FileWriter("C:\\selenium\\out1.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
       
       
       
        for(WebElement ele: dropdown){
            System.out.println(ele.getText());
            bufferedWriter.write(ele.getText()+"\n");
        }
        bufferedWriter.close();
       
        driver.close();
    }
}

Write a program to check number is Palindrome or not / Write a program to reverse a number.

import java.util.Scanner;

class Palindrome{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("enter a number");
        int n = in.nextInt();
        int a = n;
        int palindrome =0;
        int r = 0;
        while(n>0){
            r = n%10;
            n = n/10;
            palindrome = palindrome*10 + r;
        }
        System.out.println("Reverse of the entered num is: "+palindrome);
        if(palindrome==a){
            System.out.println(a+" number is palindrome.");
        }else{
            System.out.println(a+" number is not palindrome.");
        }
    }
}

How to swap the two numbers without using 3rd variable.

import java.util.Scanner;

public class Swapping{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("enter 1st number a: ");
        int a = in.nextInt();
        System.out.println("enter 2nd number b: ");
        int b = in.nextInt();
       
        System.out.println("before swapping a="+a+" and b= "+b);
        a = a+b;
        b = a-b;
        a = a-b;
        System.out.println("after swapping a="+a+" and b= "+b);
    }
}

Sunday, May 11, 2014

How to login, get the number of mails in inbox and logout from the gmail account.


import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

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

public class InboxCount {

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        System.out.println("enter ur gmail id: ");
        String id = kb.nextLine();
        System.out.println("enter ur gmail pass: ");
        String password = kb.nextLine();
       
       
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&hl=en&emr=1&elo=1");
        driver.findElement(By.id("Email")).sendKeys(id);
        driver.findElement(By.xpath("//input[@id='Passwd']")).sendKeys(password);
        driver.findElement(By.xpath("//input[@type='checkbox']")).click();
        driver.findElement(By.name("signIn")).click();
       
        String inbox = driver.findElement(By.xpath("//a[@href='https://mail.google.com/mail/u/0/#inbox']")).getText();
        System.out.println(inbox);
       
        //sign out
        driver.findElement(By.xpath("//span[@class='gb_V gbii']")).click();
        driver.findElement(By.linkText("Sign out")).click();
       
        driver.close();
       
    }
}

How to get the current date and time.


import java.util.*;

class GetCurrentDateAndTime
{
   public static void main(String args[])
   {
      int day, month, year;
      int second, minute, hour;
      GregorianCalendar date = new GregorianCalendar();

      day = date.get(Calendar.DAY_OF_MONTH);
      month = date.get(Calendar.MONTH);
      year = date.get(Calendar.YEAR);

      second = date.get(Calendar.SECOND);
      minute = date.get(Calendar.MINUTE);
      hour = date.get(Calendar.HOUR);
     
      System.out.println("Current date is  "+day+"/"+(month+1)+"/"+year);
      System.out.println("Current time is  "+hour+" : "+minute+" : "+second);
   }
}   

write a program to get the Factorial of a number


import java.util.Scanner;

public class Fac{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.println("enter the num for which u want the factorial");
        int num = in.nextInt();
        for(int i=num-1; i>0; i-- ){
            num = num*i;
        }
        System.out.println(num);
    }
}

Friday, May 9, 2014

Fibonacci Series

import java.util.Scanner;

public class Fibonacci {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("How many numbers fibonacci series you want: ");
        int number = in.nextInt();
        int[] ser = new int[number];
        ser[0] = 0;
        ser[1] = 1;
        for(int i=2; i<number; i++){
            ser[i] = ser[i-1] + ser[i-2];
            System.out.print(ser[i]+" ");
        }
       
    }
}

How to sort and remove the duplicates from array without using sort() method.


import org.apache.commons.lang3.ArrayUtils;

public class SortAndRemoveDuplicate {
    public static void main(String[] args) {
        int[] a = {1,5,3,2,1,11,7,5};
        int l = a.length, s=0;       
        for(int j=0; j<l; j++){
            for(int i=0; i<l-1; i++){
                if(a[i]>a[i+1]){
                    s = a[i];
                    a[i] = a[i+1];
                    a[i+1] = s;
                }else if(a[i]==a[i+1]){
                    a = ArrayUtils.remove(a, i);
                    l = a.length;
                }
            }
        }
        for(int n: a){
            System.out.println(n);
        }
    }
}