Hengameh
Hengameh

Reputation: 902

How use other written methods with present method

I wrote a program for this problem:
“Write a program that, given an array array[] of n numbers and another number x, determines whether or not there exist two elements in array whose sum is exactly x.”

Which is this:

boolean hasArrayTwoCandidates (int array[], int sum) {

    int length = array.length;

    quickSort(array, 0, length-1);

    int first, last;
    first = 0;
    last = length-1; 

    while(first < last){

         if( array[first] + array[last] == sum )
              return true; 
         else if( array[first] + array[last] < sum )
              first++;
         else // array[i] + array[j] > sum
              last--;
    }    
    return false;
}

At first place, I don't know where should I put or add "quick sort" codes. I have this problem with other programs, as well; when I want to add written methods to the present one.

  1. Should I create a "new class" under this "project" and put "quicksort" codes there?
  2. Should I put them in this class? but how can I use it?

At second place, I don't know what should I write in my "main method"?

this is my quicksort codes:

public void sort(int[] values) {

  if (values == null || values.length == 0){
    return;
  }

  this.array = values;
  length = values.length;
  quickSort(this.array, 0, length - 1);
}


private void quickSort(int[] array, int low, int high) {

  int i = low, j = high;
  int pivot = array[low + (high-low)/2];

    while (i <= j) {                

    while (array[i] < pivot) {
      i++;
    }       

    while (array[j] > pivot) {
      j--;
    }   

    if (i <= j) {
      exchange(i, j);
      i++;
      j--;
    }
  }

  if (low < j)
    quickSort(array, low, j);
  if (i < high)
    quickSort(array, i, high);
}    

private void exchange(int i, int j) {
  int temp = array[i];
  array[i] = array[j];
  array[j] = temp;
}

actually, I dont know what should I write in my "main method" to run this program?

Upvotes: 1

Views: 201

Answers (3)

rafaelbattesti
rafaelbattesti

Reputation: 552

Answering your questions: you can write methods and call them within the same class, just write them with the static modifier:

    private static <return_type> <methodName> (<type> param1, <type> param2) {
            // Your code here
    }

For a program like this, I don't get why you are thinking about sorting the array before checking the sum of 2 numbers within it, when you could do all at once. Check out this code, this may shine a light on you. It is straight-forward and only to see if it clarifies your logic.

import java.util.Random;

public class VerifySum {

public static void main(String[] args) {

    Random rand = new Random();
    int[] array = new int[10];

    // Produce a random number from 10 to 20
    int randomSum = rand.nextInt(11) + 10;

    // Fill out the array with random integers from 0 to 10
    for (int i = 0; i < array.length; i++) {
        array[i] = rand.nextInt(11);
    }

    // Check all array indexes against each other
    for (int i = 0; i < array.length - 1; i++) {
        for (int j = i + 1; j < array.length; j++) {
            if (array[i] + array[j] == randomSum) {
                System.out.println(array[i] + " + " + array[j] + " = " + randomSum);
            }
        }
    }

    // Print "x"
    System.out.println("randomSum = " + randomSum);

    // Print array for verification of the functionality
    for (int i = 0; i < array.length; i++) {
        System.out.println("array [" + i + "] = " + array[i]);
    }


}

}

Sometimes making it simpler is more efficient. ;-)

Upvotes: 1

DnR
DnR

Reputation: 3507

you can put all those method in same class, make hasArrayTwoCandidates() static (Note that main method is static and a static method can have access only to static methods)

public static boolean hasArrayTwoCandidates (int array[], int sum) {
    ....
}

and in your main method you can test it like this :

public static void main(String[] args){
    int[] arr = {2,5,12,5,2,7,15};
    System.out.print(hasArrayTwoCandidates(arr, 27));
}

Upvotes: 4

Anjula Ranasinghe
Anjula Ranasinghe

Reputation: 584

For you Question you can do simply this kind of coding in main method:

public static void main(String[]args) {
        int x = 20;
        int[] arr = {2,5,4,10,12,5};
        System.out.println(hasArrayTwoCandidates(arr,x));

    } 

make the methods static

static boolean hasArrayTwoCandidates (int array[], int sum)

But there are porblems in your coding:

private void exchange(int i, int j) {
  int temp = array[i];
  array[i] = array[j];
  array[j] = temp;
}

Here the array is not defined. you'll get an error. you have to pass the array too to the method make it as.

private void exchange(int i, int j,int[] array)

But since you are not necessary to do sorting. I recommend this.

static boolean hasArrayTwoCandidates (int array[], int sum) {
        boolean flag = false;
    for(int i=0;i<array.length-1;i++){
        for(int j=i+1;j<array.length ;j++){
            if(array[i]+array[j] == sum)
                flag = true;
        }
    }
    return flag;
}

this will get one element and check while adding other elements that it is true Then the main method come same way.

Upvotes: 4

Related Questions