Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com
Showing posts with label fresher. Show all posts
Showing posts with label fresher. Show all posts

Thursday, July 16, 2015

Java program to validate the PAN card Number : Regular expression

Write a java program to validate a PAN Card number. A PAN card number is said to be valid when it starts with 5 uppercase letters 4 digits and Followed by single upper case letter. A Valid PAN have only 10 letters.

For example: a pan card number AMEPJ2585B is valid where as AM525LDKF is invalid.

Given below is the java program which makes use of regular expressions to validate the PAN number. 

ValidPan.java


import java.util.*;
class ValidPAN 
{
 public static void main(String[] args) 
 {
  Scanner scn=new Scanner(System.in);
  int t=scn.nextInt();
  String s[]=new String[t];
  boolean output[]=new boolean[t];
  for(int i=0; i<s.length;i++)
  {
   s[i]=scn.next();
   boolean ss=s[i].matches("[A-Z]{5}\\d{4}[A-Z]{1}");
   output[i]=ss;
  }
  for(boolean b:output)
  {
   System.out.println(b);
  }
  
  

 }
}



Sample Output: 





Monday, July 13, 2015

Java program to convert the number in time format


Given below is the java program to print the time given in number format to String format.

For example: if you give input as 5 45, the below program will print Quarter to six


import java.util.*;
class TimeTest 
{
 public static void main(String[] args) 
 {
  String addString="";
  HashMap<Integer,String> hm=new HashMap<Integer,String>();
  hm.put(1,"one");hm.put(2,"two");hm.put(3,"three");hm.put(4,"four");hm.put(5,"five");
  hm.put(6,"six");hm.put(7,"seven");hm.put(8,"eight");hm.put(9,"nine");hm.put(10,"ten");
  hm.put(11,"eleven");hm.put(12,"twelve");hm.put(13,"thirteen");hm.put(14,"forteen");hm.put(15,"fifteen");
  hm.put(16,"sixteen");hm.put(17,"seventeen");hm.put(18,"eighteen");hm.put(19,"ninteen");hm.put(20,"twenty");
  hm.put(21,"twenty one");hm.put(22,"twenty two");hm.put(23,"twenty three");hm.put(24,"twenty four");hm.put(25,"twenty five");
  hm.put(26,"twenty six");hm.put(27,"twenty seven");hm.put(28,"twenty eight");hm.put(29,"twenty nine");hm.put(30,"thirty");

  Scanner scn=new Scanner(System.in);

  int hour=scn.nextInt();
  int minute=scn.nextInt();

  if(minute==00)
  {
   addString=hm.get(hour)+" o' clock";
  }
  else
   if(minute==15)
  {
   addString="quarter past "+hm.get(hour);
  }
  else
   if(minute==30)
  {
   addString="half past "+hm.get(hour);
  }
  else 
   if(minute>30&&minute==45)
  {
   addString="quarter to "+hm.get(hour+1); 
  }
  else
   if(minute>30&&minute!=45)
  {
   minute=60-minute;
   String midString="minute";
   String min=String.valueOf(minute);
   if(min.length()>1)
   {
    midString="minutes";
   }
   addString=hm.get(minute)+" "+midString+" to "+hm.get(hour+1); 
  }
  
  else
  {
   String midString="minute";
   String min=String.valueOf(minute);
   if(min.length()>1)
   {
    midString="minutes";
   }
   
   addString=hm.get(minute)+" "+midString+" past "+hm.get(hour); 
  }

  System.out.println(addString);

 }
}

Sample output

Tuesday, October 14, 2014

HashSet in collection framework


HashSet

HashSet is the direct subclass of Set interface , HashSet doesn’t allow duplicates element insertion and insertion order is not preserved.

  1.  Underlined data structure is HashTable.
  2. HashSet doesn't allow duplicates.
  3.  Insertion order is not preserved.
  4.  Null insertion is allowed.
  5.  Heterogeneous objects are allowed. 
  6. Implements Serializable and clonable interfaces.

Constructor in HashSet

HashSet hs=new HashSet();  // creates an empty HashSet


HashSet hs=new HashSet(int initialCapacity); 

//Creates and empty HashSet with passed initialCapacity and default load factor 0.75.

HashSet hs=new HashSet(Collection c); 

//Creates a HashSet representation of Passed collection object.

HashSet hs=new HashSet(int initialCapacity, float loadFactor); 

//creates an empty HashSet with given initialCapacity and given loadFactor.

Example:

import java.util.*;
class HashSetDemo 
{
 public static void main(String[] args) 
 {
  HashSet hs=new HashSet();
  hs.add(10);
  hs.add(20);
  hs.add(30);
  hs.add(10);
  hs.add("Hello");
  hs.add(null);
  hs.add(new HashSetDemo());
  System.out.println(hs);
  System.out.println(hs.remove(30)); /*removes the passes argument object from 
           the HashSet and returns true, false otherwise.*/
  System.out.println("Total number of elements in the HashSet "+hs.size());
  hs.clear(); //clear the entire HashSet 
  System.out.println(hs);
  
 }
}

Output:



Tuesday, October 7, 2014

9 Key Interfaces of Collection framework and Collection framework Hierarchy

Collection framework in Java


When we want to represent a group of individual object as a single entity we should choose Collection. 



Difference between Collection and Collections

Collection is an interface, whereas Collections is a class in java. Collection is treated as base interface in Collection framework, whereas Collections class consists exclusively of static methods that operate on or return collections. Collections class is defined in java.util package.


9 Key Interfaces of Collection framework

  • Collection
  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Map
  • SortedMap
  • NavigableMap

s       Collection framework Hierarchy

        






Wednesday, September 17, 2014

LinkedList In Java: A Complete Overview

LinkedList

LinkedList is the subclass of List interface. LinkedList is an implementation of doubly linked list data structure.

LinkedList implements List as well as Serializable, and Clonable interfaces.

Few Important Points About LinkedList

  •  Underlined data structure is doubly linked list . 
  • Heterogeneous data elements are allowed.     
  • Null insertion is allowed. 
  • Duplicates are allowed. 
  • Insertion order is preserved.

     Constructor In ArrayList


LinkedList()
Constructs an empty list.

LinkedList(Collection<? extends E> c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.



Methods In ArrayList

In addition to methods which are available in List, Collection interface, there are several LinkedList specific methods which you can be used only on LinkedList object.

  • Void addFirst(Object o) //Adds a new node at first place
  • Void addLast(Object o) //Adds a new node at last place
  • Object removeFirst() //remove and return the object at first node
  • Object removeLast() //remove and return the object at last node
  • Object getFirst() //return the object at first node
  • Object getLast() //return the object at last node

//Sample Program For LinkedList

import java.util.*;
class LinkedListDemo
 {
public static void main(String [] args)
{
LinkedList l=new LinkedList();
l.add("10");
l.add("10");
l.add(null);
l.add(1,100);
l.set(1,"DHT");
l.addFirst("10");
System.out.println(l);
}
}

Output of the above program



Scenarios When To Choose LinkedList And When Not To.

LinkedList is best choice when our main function is insertion at middle or deletion from middle. LinkedList could be worst choice if our main function is retrial of data from middle of the List.

More details:


If we want to insert new node at 2 place, we need not to perform shift operation. 


If you want to go to Node 2 of the list , it will first go to node 1 since node 1 has the address of node 2 then it will go to node 2.

Think of the same problem when we have 10 millions elements in the LinkedList and at some point if you want to retrieve some data element at 10000 position, it will have to go through 9999 nodes starting from node 1.

Retrieval of data from the middle of list is a performance violating task in LinkedList.

Read about ArrayList here



United health group hiring 2014 batch passed out as Associate Software Engineer




Job Title:                                     Associate Software Engineer
Job Family:                                 Information Technology
Business Segment:                   Optum Technology                           

Job Location Information:
Region:Asia Pacific
Country:India
City:Hyderabad
State - Province:Andhra Pradesh


Additional Job Detail Information:
Employee Status:Regular
Schedule:Full-time

Apply here:  



Tuesday, September 16, 2014

OCJP practice question #27



What will be the output of following java program ?

class SomeClass
{
static int i ;

public SomeClass()
{
i = 1;
}

private static int increment(int i)
{
return ++i;
}

public static void main(String[] args)
{
System.out.print(i);
System.out.print(increment(i));
System.out.print(i);
}
}


Thursday, September 11, 2014

ArrayList in Java: A complete overview

ArrayList

ArrayList is the subclass of List interface. ArrayList is an implementation of grow able or re sizable array data structure. It addition to that ArrayList also permits the random retrieval of elements.

ArrayList implements List as well as RandomAccess, Serializable, and Clonable interfaces.

RandomAccess interface is a marker interface. 

Few important points about ArrayList

  • Underlined data structure is grow able array or resizable array.
  • Heterogeneous data elements are allowed.
  • Null insertion is allowed.
  • Duplicates are allowed. 
  • Insertion order is preserved.

    Constructor in ArrayList

Constructs an empty list with an initial capacity of 10.

ArrayList(Collection<? extends E> c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.


//ArrayListConsDemo.java

import java.util.*;
class ArrayListConsDemo
{
public static void main(String[] args) 
{
ArrayList ar1=new ArrayList(); /* Create an empty ArrayList with default capacity 10 */

ArrayList ar2=new ArrayList(50); /* Creates an empty ArrayList with capacity 50 */

ArrayList ar3=new ArrayList(ar1); /* Creates a list containing elements of the specified collection */
}
}


//Sample program for ArrayList

import java.util.*;
class ArrayListDemo
{
public static void main(String[] args) 
{
ArrayList ar1=new ArrayList(); 

ar1.add(10);
ar1.add("Hello");
ar1.add(new Object());
ar1.add(3,20);

System.out.println("Element at index 1 is "+ar1.get(1)+"\n");
System.out.println("List is empty ? "+ar1.isEmpty()+"\n");
System.out.println("Hello was removed from list "+ar1.remove("Hello")+"\n");
System.out.println("List contains Hello "+ar1.contains("Hello")+"\n");

ar1.clear();
System.out.println("List is empty ? "+ar1.isEmpty()+"\n");

}

}

Output of the above program




Scenarios when to choose ArrayList and when not to.

ArrayList is best choice when our main function is retrieval of data. ArrayList could be worst choice if our main function is insertion at middle or deletion from middle.

More details:




If we want to insert some element x at index 2, we have to perform shift operation of all the elements from index 2. 


Think of the same problem when we have 1 million elements in the ArrayList and at some point we want to store element in the middle of the List. Number of shift operations performed in that case will be higher , which will decrease the performance of ArrayList.




Wednesday, September 10, 2014

Java program to remove duplicate word from a file without using Collection framework.

In an interview with Oracle one of my friend found this problem how to remove duplicate occurrence of a given word from a file without using collection framework. Since, Set interface of Collection API is a best and easy choice in the case of no duplication. But what to do in the problems when we are asked not to use Collection.

INPUT: TEST TEST TEST RESULT RESULT TEST

WORD TO REMOVE DUPLICATE: TEST

OUTPUT: TEST RESULT RESULT 

Java program 

//RemoveDuplicate.java


import java.io.*;
class RemoveDuplicate 
{
public final String INPUT_FILE="Test.txt";
public final String OUTPUT_FILE="Output.txt";
public int count=0;

public void checkDuplicate(String wordToCheck) throws Exception
{
BufferedReader br=new BufferedReader(new FileReader(INPUT_FILE));
BufferedWriter bw=new BufferedWriter(new FileWriter(OUTPUT_FILE));
StringBuilder sb=new StringBuilder();
while(br.ready())
{
sb=sb.append(br.readLine());
}

String data=sb.toString();
String values[]=data.split(" ");
for(int i=0; i<values.length;i++)
{
String arrVal=values[i].trim();
if(arrVal.equalsIgnoreCase(wordToCheck))
{
count=count+1;
if(count>1)
{
continue;
}
else
{
bw.write(arrVal+" ");
}
}
else
{
bw.write(arrVal+" ");
}
}

bw.flush();
br.close();
bw.close();
}

public static void main(String [] args)throws Exception
{
new RemoveDuplicate().checkDuplicate("Result");
}
}

Note: Make sure input file is available at given location. 


Input file 
Text.txt
                                   
Output file


Output.Txt

Above java code file can be downloaded by clicking here