Showing posts with label recursion in java. Show all posts
Showing posts with label recursion in java. Show all posts

Tuesday, April 16, 2013

Greatest Common Divisor or GCD : JAVA OOP

Write Java Source Code on Printing the Greatest Common Divisor or GCD in Recursion

Sample Output:
Enter the base number: 100
Enter the power: 45
The GCD of 2 numbers is: 5

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//java class
public class GCD
{
    public static int gcd(int num1, int num2)
    {
        if(num2 == 0)
        {
            return num1;
        }
        else
        {
            return gcd(num2, num1%num2);
        }
    }
}
//main class
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
       Scanner input = new Scanner(System.in);
      System.out.print("Enter the base number: ");
      int num1 = input.nextInt();
      System.out.print("Enter the power: ");
      int num2 = input.nextInt();
      GCD access = new GCD();
      System.out.print("The GCD of 2 numbers is: " + access.gcd(num1, num2));
    }
}

Binary Search in Recursion : JAVA OOP

Write Java Source Code: Binary Search in Recursion

Sample Output 1:
Enter the size of the array: 5
Enter 5 numbers: 2 1 3 5 4
The sorted numbers are:
1 2 3 4 5
Enter the number you want to search: 3
The search number is on the index 2
Source Code:

//Java Class
public class binarySearch //Example of Java Class
{
    //biSearch is an example of a method or a function
    public int binSearch(int[] arr, int fIndex, int lIndex,int search)
    {
int middle = (fIndex + (lIndex - fIndex) / 2);
        if(fIndex<lIndex ){
            if (search == arr[middle]){
                return middle;
            }
            else if(search < arr[middle]){
                if(search == arr[0])
                    return 0;
                return binSearch(arr, fIndex, middle, search);
            }
            else if(search > arr[middle]){
                if(search == arr[middle+1])
                    return middle + 1;
                return binSearch(arr, middle+1, lIndex, search);
            }
        }
       return -1;
    }
//this is also a class method
 public void sort(int[] arr)
{
       for(int i=0; i<arr.length; i++)
        {
            for(int j=i+1; j<arr.length; j++ )
            {
                if(arr[i] > arr[j])
                {
                    int temp = arr[j];
                    arr[j]=arr[i];
                    arr[i]= temp;
                }
            }
        }
       for(int i=0; i<arr.length; i++)
       {
           System.out.print(arr[i] + " ");
       }
}
}
//main class
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
         Scanner input = new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];
        System.out.print("Enter "+ n +" numbers: ");
        int middle;
        for(int i=0; i<n; i++)
        {
            x[i] = input.nextInt();
        }
        binarySearch access = new binarySearch(); //this is how to instantiate an object to access a class
        System.out.println("The sorted numbers are: ");
        access.sort(x);//this is how to access a method
        System.out.println();
         
        System.out.print("Enter the number you want to search: ");
        int value = input.nextInt();
        System.out.print("The search number is on the index ");
        System.out.print(access.binSearch(x, 0, x.length-1, value)); //how to access a class
    }
}

A Recursive Asterisk Diamond Shape : JAVA OOP

Write Java Source Code: A Recursive Asterisk Diamond Shape

Sample Output 1:
Enter a number: 3
The shape for this is:
---*--
--*-*--
-*-*-*--
--*-*--
---*--
Sample Output 2:
Enter a number: 4
The shape for this is:
----*--
---*-*--
--*-*-*--
-*-*-*-*--
--*-*-*--
---*-*--
----*--

Sample Output 3:
Enter a number: 5
The shape for this is:
-----*--
----*-*--
---*-*-*--
--*-*-*-*--
-*-*-*-*-*--
--*-*-*-*--
---*-*-*--
----*-*--
-----*--

Source Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//java class
public class Diamond
{
    public String Diamond_Asterisk(int num) //method1
    {
        if(num>0)
        {
            return "*-" + Diamond_Asterisk(num-1);
        }
        else
        {
            return "-";
        }
    }
     public String Diamond_Asterisk2(int num)//method2
    {
        if(num>0)
        {
            return "-*-" + Diamond_Asterisk(num-1);//access method1
        }
        else
        {
            return "-";
        }
    }
    public String Space(int num) //method3
    {
        if(num>0)
        {
            return "-" + Space(num-1);
        }
        else
        {
            return "-";
        }
    }
    public void DiamondResult(int num)//method4
    {
        for(int i=1; i<num; i++)
        {
            System.out.print(Space(num-i));//access method3
             System.out.println(Diamond_Asterisk(i));//access method1
             
        }
         for(int i=0; i<num; i++)
        {
            System.out.println(Diamond_Asterisk2(num-i));//access method2
            System.out.print(Space(i));//access method3
        }
      
    }
     
}
//main class
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = input.nextInt();
        Diamond access = new Diamond();
        System.out.println("The shape for this is: ");
        access.DiamondResult(num);
         
    }
}