Question

I have two files that I am using for my Array median code. The first file( ArrayMedian.java) is used to collect and then calculate the median, the second file is the tester file ( ArrayMedianTest.java)

I was supplied with some source code and needed to modify it accept a set range for each number in the dataset. I got that part done and the random range displays, but now when I get to he array it no longer calculates, I really can't put my finger on what is going wrong.

Another thing I am trying to do is in the ArrayMedian, is put a while loop in there to make it terminate if a '0' is input for the dataset, but it does not seem to want to work in that file, could it be due to no main in the file?

package bonus2.u06.exercise.ex3;

import java.util.Scanner;

public class ArrayMedian {

    private int[] arr;  // just declare array
    Scanner keyboard;   // shared field 

    // initialize keyboard and array
    public void init() {
        keyboard = new Scanner( System.in );    
        System.out.print("Enter the dataset size: ");

        int size = keyboard.nextInt(); // must be odd number
        arr = new int[ size ];   // instantiate 
    }

    // Randomize the array
    public void getRange() {        
        //System.out.println("\nYou entered: ");
        System.out.print("Enter a Range: ");
        int range = keyboard.nextInt(); 

        System.out.print("array: \n");
        for(int i = 0; i < arr.length; i++){
            int myRnd = (int)( range * Math.random() );

            System.out.print(" " + myRnd + " ");
        }
    }

    // find the median of array
    public int calcMedian() {
        int half_length = arr.length/2;

        for (int i = 0; i < arr.length; i++) {

            int count = 0;

            for (int j = 0; j < arr.length; j++) {
                if (arr[i] > arr[j])
                    count++;
            }
            if (count == half_length) {
                 //<=========  terminate this method
                return arr[i];
            }

        }

        return 0;   
    }

}

ArrayMedianTest:

package bonus2.u06.exercise.ex3;

public class ArrayMedianTest {

    public static void main(String args[]) {
        // instantiate 
        ArrayMedian obj = new ArrayMedian();
        // execute all methods 
        obj.init();
        obj.getRange();

        int median = obj.calcMedian();
        System.out.println("\nmedian : " + median);

        System.out.println("\n--- done ---");
    }   

}
Was it helpful?

Solution

Turn out, your algorithm works perfectly fine, except in the getRange() method, you forgot to set the values of the array, so the array is an array of zeros. Here is how it should look:

public void getRange() {        
    //System.out.println("\nYou entered: ");
    System.out.print("Enter a Range: ");
    int range = keyboard.nextInt(); 

    System.out.print("array: \n");
    for(int i = 0; i < arr.length; i++){
        int myRnd = (int)( range * Math.random() );

        System.out.print(" " + myRnd + " ");
        arr[i] = myRnd; // <-- You missed this line right here!
    }
}

Also, as a recomendation, if you want to put code in stackoverflow, it has to have a spacing of four at the begining of the line plus any indenting you might use. Good luck programming!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top