Saturday 1 October 2016

I am back

Hey guys i am finally back after a long break. Sorry, i was busy with my life for 3-4 years and gave up programming. Last sunday my younger brother was studying Java and he asked me a question. I felt the same passion for it again!!
So i went to my shelf and undusted ' Complete Reference Java'. After turning few pages my interest regained. Then i decided to visit my blog again. Feel free to ask me anything guys.

Sunday 25 September 2011

Frequent error

I was going through my answers on my Yahoo Answers profile
It was amazing
Atleast 25% were related to a common error
Using == to compare strings
We could not use == to compare two strings because they are the objects of String class present in java.lang package
To compare two strings we should use equals method
String s= "Hello";
if( s.equals("Hello") )
or
if( "Hello".equals(s) )
The second one is said to be more faster
To compare two string ignoring case
if( s.equalsIgnoreCase( "Hello"))

Friday 12 August 2011

Magic Square

I designed an interesting program

I wrote code for the creation and initialization
of a magic square of the given odd size. I implemented the
following algoritm:

- put 1 in the middle cell of the first row;
- every next value must be placed in a cell
one row up and one column right relative to the previous one;
- if the destinaton of the next cell appears outside of the
array, enter the later from the opposite end;
- if the destinaton of the next cell is already occupied,
then move just one row down relative to the previous cell.

For example,

for size = 3,

8    1    6
3    5    7
4    9    2

the sum of all rows and columns is 15


Here is the code
class Grid
{
int num;
public void fillGrid(int n)
{
int ar[][]=new int[n][n];
num=n;
fill(ar);
int r=0,c=n/2,i;
for(i=1;i<=(n*n);i++)
{
if(ar[r][c]==0)
ar[r][c]=i;
else
ar[++r][c]=i;

/*printArray(ar);
System.out.println("\n\n\n\n");*/

if((r-1)<0 && (c+1)>=n)
r++;
else
{
if((r-1)<0)
{
r=n-1;
c++;
}
else if((c+1)>=n)
{
r--;
c=(c+1)-n;
}
else if(ar[r-1][c+1]==0)
{
r--;
c++;
}
}
}
printArray(ar);
}
void fill(int Ar[][])
{
int i,j;
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
Ar[i][j]=0;
}
}
void printArray(int Ar[][])
{
int i,j;
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
System.out.print("\t "+Ar[i][j]);
System.out.println("");
}
}
}


Saturday 6 August 2011

Binary Search in an array

import java.util.Arrays;
import java.util.Scanner;
public class BinarySearch
{
// precondition: array a[] is sorted
public int search(int[] a,int key)
{
int lo = 0;
int hi = a.length - 1;
while (lo <= hi)
{
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < a[mid])
hi = mid - 1;
else if (key > a[mid])
lo = mid + 1;
else return mid;
}
return -1;
}
public static void main(String[] args)
{
int arr[] = new int[10];
System.out.println("Enter 10 numbers");
Scanner input = new Scanner(System.in);
for (int i = 0; i < arr.length; i++)
{
arr[i] = input.nextInt();
}
BinarySearch search = new BinarySearch();
System.out.print("Enter the element to search: ");
int num=input.nextInt();
int n = search.find(arr, num);
if (n!=-1 )
{
System.out.println("Found at index: " + n);
}
else
{
System.out.println("Not Found");
}
}

}