Automation Using Selenium Webdriver

Monday, 14 November 2016

Find top two maximum numbers in a array java

  • Hi Friends today we will discuss about how to find top two maximum numbers in an array using java program.
  • For this we have written separate function to perform logic
  • findTwoMaxNumbers method takes integer  array as an argument
  • Initially take two variables to find top to numbers and assign to zero.
  • By using for each loop iterating array and compare current value with these values
  • If our value is less than current array value then assign current value to max1 
  • And assign maxone to maxtwo because maxtwo should be second highest.
  • After completion of all iterations maxone will have top value and maxtwo will have second maximum value.
  • Print first maximum and second maximum values.
  • So from main method create array and pass to findTwoMaxNumbers(int [] ar).


Program #1: Java interview programs to practice: find top two maximum numbers in an array without recursion 

package arraysInterviewPrograms.java;
public class FindTopTwo {
public void findTwoMaxNumbers(int[] array){
       
 int maxOne = 0;
 int maxTwo = 0;
for(int i:array){
    if(maxOne < i){
           maxTwo = maxOne;
           maxOne =i;
     } else if(maxTwo < i){
                maxTwo = i;
     }
}
        
  System.out.println("First Maximum Number: "+maxOne);
  System.out.println("Second Maximum Number: "+maxTwo);
}
     
public static void main(String a[]){
        int num[] = {4,23,67,1,76,1,98,13};
        FindTopTwo obj = new FindTopTwo();
        obj.findTwoMaxNumbers(num);
        obj.findTwoMaxNumbers(new int[]{4,5,6,90,1});
}
}


Output:


First Maximum Number: 98
Second Maximum Number: 76
First Maximum Number: 90
Second Maximum Number: 6

Calling static method from non static method in java

  • Static means class level and non static means object level.
  • Non static variable gets memory in each in every object dynamically.
  • Static variables are not part of object and while class loading itself all static variables gets memory.
  • Like static variables we have static methods. Without creating object we can access static methods.
  • Static methods are class level. and We can still access static methods in side non static methods.
  • We can call static methods without using object also by using class name.
  • And the answer to the question of  "is it possible to call static methods from non static methods in java" is yes.
  • If we are calling a static method from non static methods means calling a single common method using unique object of class which is possible. 


Program #1: Java example program to call static method from non static method. 


;
public class StaticMethodDemo {
void nonStaticMethod(){
        
        System.out.println("Hi i am non static method");
        staticMethod();
 }
    
 public static void staticMethod(){
        
        System.out.println("Hi i am static method");
  }
    
 public static void main(String[] args) {
        StaticMethodDemo obj= new StaticMethodDemo();
        
        obj.nonStaticMethod();
    }
}
 Output:

Hi i am non static method
Hi i am static method

In the above program we have created object of the class and called a non static method on that object and in side non static method called a static method.
So it is always possible to access static variables and static methods in side non static methods

 Program #2: Java example program to call static method from non static method.



Sunday, 13 November 2016

Collections List

List Interface:
List allows Duplicate Elements.
List having index.
List allows n number of null values.
List will display Insertion order with index.
List having classes like :
Vector
ArrayList
LinkedList
Vector:
Vector is a legacy class.
Vector is synchronized.
Vector initial capacity is 10.
Vector allows n number of null values
Vector can be accessible by index.
Vector allows Duplicate Elements.
Program for Vector:

package com.instanceofjavaforus;
import java.util.Enumeration;

import java.util.List;

import java.util.Vector;



public class A{

 public static void main(String[] args) {

 

 Vector vector=new Vector();



 vector.add("india");

 vector.add("jagan");

 vector.add("sam");

 vector.addElement(null);

 vector.addElement(null);
 
  Enumeration em=vector.elements();
 while(em.hasMoreElements()){
  System.out.println(em.nextElement());
 
 }
 }

}
Output:

india
jagan
sam
null

null



ArrayList:

ArrayList is not Synchronized.
Arraylist also allows Duplicate Elements.
ArrayList allows n number of null values.
Arraylist  having insertion order with index.
For retrieve purpose ArrayList is best choice.
ArrayList having Randaom Access Nature.
Program for ArrayList ;



package com.instanceofjava;
import java.util.ArrayList;
import java.util.ListIterator;

import java.util.List;



public class A{

 public static void main(String[] args) {

 

             ArrayList arrayList=new ArrayList();




 arrayList.add("jagan");

 arrayList.add("jagan");

 arrayList.add("upendra");

 arrayList.add(null);

 arrayList.add(null);
 
  ListIterator it=arrayList.listIterator();
 while(it.hasNext()){
  System.out.println(it.hasNext());
 
 }
 }
}

Output:
jagan
jagan
upendra
null
null

LinkedList:

LinkedList is not synchronized.
LinkedList allows n number of null values.
LinkedList allows Duplicate Elements.
LinkedList  having insertion order with index.
Insertion and Deletion purpose LinkedList is better choice.
LinkedList Follows Doubly linked list Structure.
Program for LinkedList:


package com.instanceofjava;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.List;

public class A{

 public static void main(String[] args) {

   LinkedList linkedList=new LinkedList();




 linkedList.add("jagan");

 linkedList.add("jagan");

 linkedList.add("naresh");

 linkedList.add(null);

 linkedList.add(null);


  ListIterator it=linkedList.listIterator();
  while(it.hasNext()){
  System.out.println(it.hasNext());
 
 }
 }
}

Output:


jagan
jagan
naresh
null
null
In List interface if you want  to add elements we can use add() method.
If you want  to remove elements we can use remove() method.