0

sorry to repost this question again but I really need the answer.

What I need is to get the answer to the bubble list (on one class) to appear on another class, as shown below:

public class BubbleSort4 
{
    public static void main(String[] args) {

            int intArray[] = new int[]{5,90,35,45,150,3};

            System.out.println("Array Before Bubble Sort");
            for(int i=0; i < intArray.length; i++)
            {
                    System.out.print(intArray[i] + " ");
            }

            bubbleSort(intArray);

            System.out.println("");

            System.out.println("Array After Bubble Sort");
            for(int i=0; i < intArray.length; i++)
            {
                    System.out.print(intArray[i] + " ");
            }

      }

     private static void bubbleSort(int[] intArray) 
     {                            
            int n = intArray.length;
            int temp = 0;

            for(int i=0; i < n; i++)
            {
                    for(int j=1; j < (n-i); j++)
                    {

                            if(intArray[j-1] > intArray[j])
                            {

                                     temp = intArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    intArray[j] = temp;
                            }

                    }
            }    
    }

}

And get the answer to the above coding to appear on OddArr3:

System.out.println ("Bubble Sort : " + bubbleSort.sortBubble(intArray));

System.out.println ("Bubble Sort : " + bubbleSort.sortBubble(intArray)); does not seem to work.

Below is the full coding to OddArr3

import java.util.ArrayList;
import java.util.List;
import java.util.*;

public class OddArrSe3:
{
     public static void main(final String[] args)
    {
        int[] array_sort = {10,41,21,24,34,15,40,12,32,23,13,25,30,31,22,33,14,35,20,11};

        ArrayList<Integer> OddArr = new ArrayList<Integer>();
        ArrayList<Integer> EvenArr = new ArrayList<Integer>();

        for (int i : array_sort)    
        {
            if ((i & 1) == 1) 
            {
                OddArr.add(i);
            } 
            else 
            {
                EvenArr.add(i);
            }
         }

         Collections.sort(OddArr);
         Collections.sort(EvenArr);
         System.out.println("Odd:" + OddArr);
         System.out.println("Even:" + EvenArr);

          int OddArr2[] = {11, 13, 15, 21, 23, 25, 31, 33, 35, 41};
          int toSearch = 31;  

          int EvenArr2[] = {10, 12, 14, 20, 22, 24, 30, 32, 34, 40};
          int toSearch2 = 32;

          LinearSearch4 linearSearch = new LinearSearch4();  
          BinarySearch4 binarySearch = new BinarySearch4();  
          Bubblesort4 bubblesSort = new BubbleSort4();

          System.out.println("Linear Search Index : "  
          + linearSearch.searchLinear(OddArr2, toSearch));  
          System.out.println("Binary Search Index : "  
          + binarySearch.searchBinary(EvenArr2, toSearch2)); 
          **System.out.println ("Bubble Sort : " + bubbleSort.sortBubble(intArray));**
        }
 }
  • Your question is not clear to me. First of all, where is intArray in OddArrSe3 class?? – codingenious Dec 15 '13 at 10:18
  • possible duplicate of [how to output answer from 2 classes (OddArrSe3 and BubbleSort4)](http://stackoverflow.com/questions/20483558/how-to-output-answer-from-2-classes-oddarrse3-and-bubblesort4) – anestv Dec 30 '13 at 21:31

4 Answers4

0

To answer your question. Your method should be public, not private. Then you can access it.

I have also two remarks:

  • First of all, why do you have two main methods? In small prorgrams you usually need one just to start the program.
  • In addition try to NOT use names for classes/methods like OddArr2/ OddArrSe3. They make code less understandable.
Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
0

1) please remove the colon after the class name 'OddArrSe3' 2) As Marcin Szymczak pointed out, two main methods are not required. 3) change the signature of method bubbleSort to public int[] bubbleSort(int[] intArray) and return intArray after sorting. (private methods are not accessible outside the class in which they are declared)

Balaji Krishnan
  • 1,007
  • 3
  • 12
  • 29
0

You're calling a static method from an instance. It should be BubbleSort4.bubbleSort(...).

David Knipe
  • 3,417
  • 1
  • 19
  • 19
0

Make following changes and re try

  1. Remove : from OddArrSe3:

  2. Make your BubbleSort4.bubbleSort() method public

  3. call BubbleSort4.bubbleSort(array_sort); to short your array

  4. BubbleSort4.bubbleSort() method is void it will give compilation error if you call it as follows:

    System.out.println ("Bubble Sort : "+BubbleSort4.bubbleSort(array_sort)); 
    
  5. Either change return type of your method as public int[] bubbleSort() and return sorted array or use a for loop to traverse your array and print the sorted numbers

Shamse Alam
  • 1,285
  • 10
  • 21