Object-Oriented Programming with Java, part I – Exercise 106

This exercise implements a binary search method for an array of numbers halving the search area each time.

My Code

Main

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // Test your program here
        int[] taulukko = {-3, 2, 3, 4, 7, 8, 12};
        Scanner lukija = new Scanner(System.in);

        System.out.print("Numbers in the array " + Arrays.toString(taulukko));
        System.out.println();

        System.out.print("Enter searched number: ");
        String etsittavaLuku = lukija.nextLine();
        System.out.println();

        boolean tulos = BinarySearch.search(taulukko, Integer.parseInt(etsittavaLuku));

        // Print here the result
        if(tulos){
            System.out.println("Value "+ etsittavaLuku +" is in the array");
        } else {
            System.out.println("Value "+ etsittavaLuku +" is not in the array");
        }
    }
}

BinarySearch

public class BinarySearch {
    public static boolean search(int[] array, int searchedValue) {
        int beginning = 0;
        int end = array.length - 1;

        while (beginning <= end) {
            int middle = (beginning + end) / 2;
            if (array[middle] == searchedValue) {
                return true;
            } 
            
            if (array[middle] > searchedValue) {
                end = middle -1;
            } else {
                beginning = middle + 1;
            }

            // restrict the search area 
        }
        return false;
    }
}

Model Code

Main

import java.util.Arrays;

import java.util.Scanner;

 

public class Main {

 

    public static void main(String[] args) {

        // Test your program here

        int[] taulukko = {-3, 2, 3, 4, 7, 8, 12};

        Scanner lukija = new Scanner(System.in);

 

        System.out.print("Numbers in the array " + Arrays.toString(taulukko));

        System.out.println();

 

        System.out.print("Enter searched number: ");

        String etsittavaLuku = lukija.nextLine();

        System.out.println();

 

        boolean tulos = BinarySearch.search(taulukko, Integer.parseInt(etsittavaLuku));

 

        // Print here the result

        if (tulos) {

            System.out.println("Number " + etsittavaLuku + " is in the array.");

        } else {

            System.out.println("Number " + etsittavaLuku + " is not in the array.");

        }

    }

}

BinarySearch

public class BinarySearch {

    public static boolean search(int[] array, int searchedValue) {

        int beginning = 0;

        int end = array.length - 1;

 

        while (beginning <= end) {

            int middle = (beginning + end) / 2;

            if (array[middle] == searchedValue) {

                return true;

            }

 

            // restrict the search area 

            if (searchedValue > array[middle]) {

                beginning = middle + 1;

            } else {

                end = middle - 1;

            }

        }

        return false;

    }

}

Comments

My code is the same as the model code. And that was the end of Object-Oriented Programming with Java, part I. Now, onward to part 2.

Leave a comment