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("");
           }

   }
}

Recommended Answers

All 7 Replies

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 : )

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

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.

thanx for your help but i still understand this sorry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.