How do I fetch those two different implementations in the main program using the same API calls?
and you'd like to eliminate the if test and different method calls? If that's the challenge, the interface suggestion is a good hint. Let us know if that's near the mark.
The random number generation service should allow at least two different implementations of the random number generator to be called from the same "main" program without code rewrites. The main program should be a Java program that fetches five random numbers from the random number generator service and displays them on the screen.
One of the random number generator is the Java built-in generator and I have to come up with the other random number generator (question is posted under another JavaRanch Forum topic and yet to be worked on).
I already have a scratch of the main program calling the Java built-in randome number generator:
I am trying to figure out how "the main program is able to fetch from either random number generator implementation using the same API calls", which is a requirement of this exercise.
Step 2: Define an interface RandomNumberGenerator with a getRandom() method. Have "BuildInMethod" declare that it implements that method.
Step 3: Write another random number generator. Have it also implement that interface.
Now, as far as how main() should work: for this kind of program, I'd be partial to having the random number generator class's name on the command line, and using Class.forName().newInstance() to load it, and then cast it to RandomNumberGenerator before calling the getRandom() method.
Note that this loop:
for ( int i = 0; i <= 5; i++ )
will execute six times, not five. That "<=" should be a "<".
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
Rocket Oven Kickstarter - from the trailbosshttps://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss