Monday, July 15, 2013

Reverse String

a. Create a HTML page with a text box and a button. 
b. Take a string as input from user and count different types of characters in given string and print and also print the reverse of the string in same page. 
c. Validate for m fields for Empty - 
i. Above validation should happen on button click. 
ii. ON VALIDATION DISPLAY PROPER USER FRIENDLY MESSAGE TO USER. 
d. For example: “apple” would be 
a=1 
p=2 
l=1 and 
e=1 
Reverse: 
apple -> elppa 
e. Display page content in the middle of the page with proper alignment. 
f. Try to design a good looking html page. 

g. You are only allowed to use HTML for design the page and JavaScript for scripting logic. 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
</HEAD>
<script>
function strReverse()
{
var input=document.getElementById('text1').value.trim();
var content=document.getElementById('stringt');
var newTable="<tr><td>";
var cha=new Array();
var reverse=[];
var leng=input.length-1;
var stri='';
//reverse.length=input.length;
var reg=/^[a-zA-Z._]+$/;
if(!reg.test(input))
{
alert("Please enter String");
return false;
}
cha.length=input.length;
var count;
for(var index=0;index<input.length;index++)
{
count=1;
reverse[leng-index]=input[index];
for(var i=0;i<input.length;i++)
{
if(index!=i&&input[index]==input[i])
{
count++;
}
}
if(input[index]==input[index-1]){}
else{
cha[index]=input[index]+'='+count;
newTable=newTable+cha[index]+"</td></tr><tr><td>";
}
}
for(var c=0;c<leng+1;c++)
{
stri=stri+reverse[c];
}
newTable=newTable+"<tr><td>"+stri+"</td></td></table>";
content.innerHTML=newTable;
return true;
}
</script>
<BODY><center>
<form id='form' onsubmit='return strReverse()'>
<div id='con' style="width:250px; background-color:green">Enter Your Name:<input type='text' name='str' id='text1' value=''><br>
<input type='button' name='sbutton' id='strButton' value='submit' onclick='return strReverse()'><table id='stringt' style="width:250px; background-color:red; align:middle"></table>
</div>
</form></center>
</BODY>
</HTML>

Friday, July 12, 2013

Difference between ArrayList and Vector

                 ArrayList is Resizable-Array implementation of the List interface and introduced in 1.2 version. It allow all elements include null. ArrayList is unsynchronized and it is not thread safe.In this class size,isEmpty,get,set,Iterator and ListIterator operations run in constant time.The add operation run in amortized constant time,that is,adding n elements requires O(n) time. Each ArrayList instance has a capacity.The capacity is the size of the array used to store elements in the list.An application can increase  the capacity of  an ArrayList instance before adding a large number of elements using the ensureCapacity operation.
                   Vector class is implements a growable array of objects.The size of a vector can grow or shrink as needed to accommodate adding and removing items after the vector has been created.Each vector maintaining a capacity and a capacityIncrements.  

Difference between ArrayList and Vector:

1. ArrayList class methods are not Synchronized and Vector class methods are Synchronized.So Vector class is used in multi-threading and concurrent environment.

2. When Vector class reaches it's threshold limit,then it automatically increase itself by value specified in capacityIncrement. But ArrayList call ensureCapacity() method to increase its size.

3. ArrayList gives  better performance than Vector.Because ArrayList is not Thread-Safe but Vector is Thread-safe that's why it give low performance.

4. ArrayList is introduced in 1.2 version but Vector is introduced in 1.0 version and it is called as Legacy class.

5. Vector class is Legacy class,we use Enumeration class to print elements of Vector class.For ArrayList we use  Iterator for printing elements of ArrayList objects.

If your project requirement is Threadsafe use Vector class otherwise use ArrayList class.Because ArrayList not Thread-safe so it gives better performance than Vector class.

Can we get Synchronized ArrayList:

Yes,We can get by using Collections.synchronizedList(List l); method.

ArrayList arrayList=new ArrayList();
List l = collections.synchronizedList(arrayList);  


see also this link :
prime number
why array index start with zero
reverse string in different ways in java

Thursday, July 11, 2013

ButterFlyLogic

Write the Program for ButterFlyLogic in java?

5             5
44         44
333     333
2222  2222
111111111
2222  2222
333     333
44         44
5             5

For Logic click on THis link



Wednesday, July 10, 2013

HashMap Vs HashTable

                             HashMap is HashTable based implementation of Map Interface. It doesn't follow any order. This class makes no guarantees as to the order of the Map and it doesn't guarantee that the order will remain constant over time. HashMap allows null valuers and null key. An instance of HashMap has  two parameters that affect its performance: initial capacity and load factor. The default load factor of HashMap is 0.75.
                            HashTable is implemented based on Key-Value pair data-Structure in java. It extends Dictionary and implements Map. In this Key should be unique and allow value as duplicate. It also implement hashCode() and equals() methods for comparing duplicate key.

Difference between HashMap and HashTable:

1. HashMap using  HashTable for storing key-value pairs.but HashTable uses key-value pair data structure.

2. Iterator in HashMap is Fail-Fast but enumaretion in HashTable in not Fail-Fast.

3. HashMap is not Synchronized and HashTable is Synchronized.But we can get Synchronized HashMap by using Collections class method synchronizedMap() .

4.  HashMap is HashTable based implementation of Map Interface. HashTable extends Dictionary and implements Map.

5. HashMap allows one null key and any number of null values but HashTable doesn't allows null keys and null values.

6. HashTable is not Threadsafe and HashTable is  Threadsafe.

7. HashMap is faster than HashTable.

8. We can use ConcurrentHashMap for multi-thread environment.But HashTable doesn't contain this type of facility.
Can we create Synchronization for HashMap?

Yes,We can achieve this by using Collections.synchronizedMap();

HashMap hs=new  HashMap();
Map m=Collections.synchronizedMap(hs);



Tuesday, June 25, 2013

How to add two numbers,each number has more than 1 lakh digits

                                             Today i posted about "How to add two numbers,each number has more than 1 lakhs digits?".If i ask how to add two numbers everyone said, by using '+' operators through primitive type variables.In any programming language,each primitive type has their limits.For example in java, int primitive type range is -2,147,483,648 to 2,147,483,647 and int maximum range is 10 digit number. No such type of primitives to store more than 100000 digits in any language. By using primitives we cannot solve this problem  so we use String type variables.
                                For solving this problem take two String variables, take input from user and store into this variables.After convert String variables into character arrays by using String class method toCharArray() method.
For Example:
  String str="1234567890";
 char ch[]=(int)str.toCharArray();
for(int index=0;index<ch.length;index++){
 System.out.println(ch[index]);
}

output is 1 2 3 4 5 6 7 8 9 0

After converting into character array,take first array of last index with second array of last index.

For more clarity see below program:
Click on for Addition.java



Wednesday, June 19, 2013

Floyd's Triangle

Write a program to print the Floyd's Triangle?  

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

import java.util.*;
class FloydTriangle
{
 public static void main(String[] args)
 {
     Scanner scannerInput=new Scanner(System.in);
     System.out.println("Enter how rows you want");
     int rows=scannerInput.nextInt();
     int num=1;
     for(int index=0;index<rows;index++)
     {
         int repeat=index;
         while(repeat>=0)
         {
             System.out.print(num+" ");
             num++;
             repeat--;
         }
         System.out.println("\n");
     }
 
}


Enter how rows you want 5
1 

2 3 

4 5 6 

7 8 9 10 

11 12 13 14 15 

                          

Saturday, June 1, 2013

Tribonacci Series

                                                 Tribonacci Series is like a Fibonacci Series, but some difference between these two.
                          The Fibonacci sequence is introduced by Leonardo  Pisano,an Italian Mathematician.The Fibonacci sequence is a set of numbers that starts with  a Zero or a one,followed by one and proceds based on the rule that each number  is equal to sum of the preceding two numbers.The Fibonacci sequence is denoted by F(n) ,where n is the first term in the sequence.For n=0,where the first two terms are obtained as 0 and 1.The series is
F(0)=0,1,1,2,3,5,8,.................


                                  The Tribonacci Sequence is a set of numbers that start with a Zero or a one,followed by two one's and proceds based on the rule  that each number is equal to sum of the preceding three numbers.The Tribonacci Sequence is denoted by F(n),where n is the first term in the sequence.For n=1,where the first three terms are 0,1 and 1.
The series is
F(1)=1,1,1,3,5,9,17,31,......