dimanche 11 février 2018

Java - Help writing function to return array (with Junit test)

I can really use a hand from someone with more experience to learn about writing a function to create and return an integer array. Any guidance is appreciated, I am extremely confused with the next step I should take in order to pass the Junit test described below.

It seems that I should be able to gain quite a bit of insight from the test, however this is my first exposure to Junit tests. Can anyone help me interpret this unit test snippet?

The Goal

Write a function that creates and returns an array of integers:

  • The values should be from 0 to 100000.
  • Use Math.random() to generate random numbers from 0.0 to 1.0.
  • Must pass Junit test (see snippet below)

NOTE: I will eventually write functions to bubble sort and select sort. I will also report timing on vectors (of random integers) of size 1000 to 10000, stepping by 1000, how long it takes the bubble and selection sort functions.

Junit test for generateNumbers

@org.junit.Test
public void generateNumbers() {
    for (int i = 1; i < 10000; i += 10) {
        int[] newArray = SortingDriver.generateNumbers(i);
        Assert.assertEquals(i, newArray.length);
    }
}

Current Code (SortingDriver)

    public static int[] generateNumbers(int howMany) {
    // TODO: Create and return array of integers
    // TODO: The values should be from 0 to 100000
    // TODO: Use Math.random() to generate random numbers from 0.0 to 1.0

    int size = 100000;

    // Create array of integers
    int[] sortMe = new int[size];

    // Utilize random function to create a random number in between 0 and 100,000
    int rand = (int)(Math.random() * (size + 1));

    // For loop?
}




Aucun commentaire:

Enregistrer un commentaire