Showing posts with label to. Show all posts
Showing posts with label to. Show all posts

Sunday, April 6, 2014

Java code to search an element using Linear Search or Binary Search

/*
Program to search for a number in an array entered by the user using
either Linear Search or Binary Search.
*/
import java.io.*;
class searchArray
{
int a[];
int n;
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
public searchArray(int nn) // Constructor
{
a = new int[nn];
n = nn;
}
public static void main(String args[]) throws IOException
{
System.out.print("
Enter the size of the array : ");
int nn = Integer.parseInt(br.readLine());
searchArray call = new searchArray(nn);
System.out.println("
Enter " +nn +" elements :");
call.readArray();
// Ask for the search technique
System.out.println("Choose Search Technique :
");
System.out.println("1 : Linear Search");
System.out.println("2 : Binary Search (the array should be
sorted in ascending order)");
System.out.print("
Your Choice : ");
int choice = Integer.parseInt(br.readLine());
int v;
switch(choice)
{
case 1:
System.out.print("
Enter the number to be searched : ");
v = Integer.parseInt(br.readLine());
call.linearSearch(v);
break;
case 2:
System.out.print("
Enter the number to be searched : ");
v = Integer.parseInt(br.readLine());
call.binarySearch(v);
break;
default :
System.out.println("
Invalid Choice !");
break;
}
}
public void readArray() throws IOException
{
for(int i=0;i<n;i++)
a[i] = Integer.parseInt(br.readLine());
}
public void linearSearch(int v)
{
int f=-1;
for(int i=0;i<n;i++)
{
if(a[i]==v)
{
f=i;
break;
}
}
if(f==-1)
System.out.println("
" +v +" NOT found !");
else
System.out.println("
" +v +" is in location " +f);
}
public void binarySearch(int v)
{
int f=-1;
int l=0,m=0,u=n-1;
while(l<=u && f==-1)
{
m = (l+u)/2;
if(a[m]==v)
f = m;
else
if(a[m]>v)
u = m-1;
else
l = m+1;
}
if(f==-1)
7System.out.println("
" +v +" NOT found !");
else
System.out.println("
" +v +" is in location " +m);
}
}
/**
* Algorithm for Linear Search :
* ---------------------------
* 1. Start
* 2. Construct a single-dimensional array of appropriate size.
* 3. Fill the array with data entries.
* 4. Ask the user for the number to be searched
* 5. Use linear search technique.
* 6. Print a message whether the number is present in the array or
not.
* 7. End
*
* Algorithm for Binary Search :
* ---------------------------
* 1. Start
* 2. Construct a single-dimensional array of appropriate size.
* 3. Fill the array with data entries in ascending order.
* 4. Ask the user for the number to be searched.
* 5. Use binary search technique.
* 6. Print a message whether the number is present in the array or
not.
* 7. End
*/
/*
Enter the size of the array : 5
Enter 5 elements :
46
324
846
54
541
Choose Search Technique :
1 : Linear Search
2 : Binary Search (the array should be sorted in ascending order)
Your Choice : 1
Enter the number to be searched : 54
54 is in location 3
*/


Read More..

Friday, April 4, 2014

How to detect bad sectors on your hard disk

In another article I explained how to repair bad sectors on HDD and I listed some methods. But how you can detect them?

You can supose that your HDD have bad sectors after some signs like the bigger time to load your OS, data losses, or the impossibility to access your data because the OS didnt load. But its pretty unsure that you have bad sectors.

One method is to use a live cd of any Linux kernel( like Ubuntu, Fedora, LinuxMint etc.) and after you boot from cd and select the try that kernel option, you must go to Accessories and then to Disk Utility.
A HDD with some bad sectors
If you had bad sectors on your HDD, the Disk Utility will show you how many there are ( Ex. You have 3 bad sectors) or if there are many bad sectors, "Disk has a few bad sectors". And after this test you can be sure that your disk is broken and you must try to repair it using one of those methods I posted( link here).

Another method is to use an application like MHDD that you can use after you create an bootable cd/ dvd or usb stick and then test your HDD. Using this program you will see a lot of datas about your HDD.
Read More..

Monday, March 31, 2014

How to create menu in Android using xml file

This code will show how to create menu in android, how to perform action when we click on menu item and how to set icon in menu item.

Just create a new project and give "rl" id to layout or paste below code in layout->main xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TestMenu"
    android:id="@+id/rl">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

 
Now open menu folder and create test_menu xml file and paste below code


    <item android:id="@+id/red"
        android:title="RED"
        android:icon="@drawable/lion">
       
    </item>
      <item android:id="@+id/blue"
        android:title="BLUE">
       
    </item>
      <item android:id="@+id/green"
        android:title="GREEN">
       
    </item>
      <item android:id="@+id/yellow"
        android:title="YELLOW">
       
    </item>
      <item android:id="@+id/black"
        android:title="BLACK">
      </item>
           <item android:id="@+id/gray"
        android:title="GREY">
        </item>
      
           <item android:id="@+id/pink"
        android:title="PINK">
      </item>
         <item android:id="@+id/white"
        android:title="WHITE">
      </item>
         <item android:id="@+id/close"
        android:title="CLOSE">
      </item>
</menu>

Now open Main java file and paste below code

package test.menutest; //your package name

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.widget.RelativeLayout;

public class TestMenu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

 //This function will call when we click on menu button
    @Override
    public booleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
//This function will perform action when we click on menu item

    public booleanonOptionsItemSelected(MenuItem item)
    {
      RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
      switch(item.getItemId())
      {
      case R.id.red:rl.setBackgroundColor(Color.RED); break;
      case R.id.blue:rl.setBackgroundColor(Color.BLUE); break;
      case R.id.gray:rl.setBackgroundColor(Color.GRAY); break;
      case R.id.yellow:rl.setBackgroundColor(Color.YELLOW); break;
      case R.id.pink:rl.setBackgroundColor(Color.MAGENTA); break;
      case R.id.white:rl.setBackgroundColor(Color.WHITE); break;
      case R.id.green:rl.setBackgroundColor(Color.GREEN); break;
      case R.id.close:finish(); break;
      default:rl.setBackgroundColor(Color.BLACK);
     
      }
     
      return true;
    }  
}

Run code and Enjoy...

Read More..