hey all I found out today I needed to do a testdriver for my programme as we no it took me forever to do the programme so will some one explain the test driver to me or even help me I no this is a silly thing but im so new to java its a big learning curve for me cheers

import java.util.Random;

Public class Array
{
 public static void main(String[] args)
   {
       Random r = new Random();
       int array[] = new int[50];
       int a = 0;
       while (a < 50)
       {
           boolean foundNumber = false;
           int random = r.nextInt(999);

           // this does a linear search to find if the number
           // is already there, and if it is, it changes the variable
               for(int i = 0; i < a; i++)
               {
                       if(array[i] == random)
                       {
                               foundNumber = true;
                       }
               }

               
           if(foundNumber == false)
           {
               array[a] = random;
                       a++;
           }
       }

       int i = 0;
           for(int row = 0; row< 10; row++)
           {
               for(int c = 0; c<=4; c++)
              {
                   System.out.format("%03d ", array[i]);
                   i++;
              }
                   System.out.println("");
           }

   }
}

Edited 6 Years Ago by shane1987: n/a

Explain the problem and your approach, don't expect everyone to guess what you need to do and code it for you.

im not asking no one to do it for im asking what it is and if you no any sites you can recomend

They ask you to build a class that will test the class you have previously created.

They ask you to build a class that will test the class you have previously created.

ok cheers apine : )

Edited 6 Years Ago by shane1987: n/a

im forgetting the test for a min is this a psuedo code?

would you say this is right ?

Loop while there are not 50 different random numbers
Generate a new random number

Loop through all of the elements in the existing numbers

If the new random number was not found in the array
Insert the random number into the next place in the array

Edited 6 Years Ago by shane1987: n/a

Your pseudo-code looks OK.
Now, back to the test driver.
You have all your code in main, which makes this difficult - so the first thing is to move the number generator into a new method that returns an array of 50 different random numbers, eg

public int[] generateNumbers() {
  // generate and return the numbers
}

Now you have your code in its own method you can use main(...) to test that method, eg

public static void main(String[] args) {
   int[] numbers = generateNumbers();
   // now print the numbers in the array to see if they are OK
}

This is a particularly simple example, but that's how test drivers for small programs or classes typically work. You put the code into methods, then write a small main(...) method that calls your methods and displays or checks the results.

This question has already been answered. Start a new discussion instead.