Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How To Call Two Different Classes From the Main Program Using the Same API?

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a main program (public static void main( ... )). I have two classes. Both classes are doing the same thing but each uses its own method to do it. That is to say, class A uses Method A and class B uses Method B. Method A and Method B are different ways to do the same work.

How do I fetch those two different implementations in the main program using the same API calls?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With an interface.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me see if I got the question ... maybe your main looks like this:

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.
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for being willing to help. I am applying for a job. In addition to phone interview, face-to-face interview, and other requirements, I have to write a Java program:


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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1: Give the "BuildInMethod" class a "getRandom()" method, and use it to fetch "x", instead of accessing the variable directly (to get more than one random number, you'll need to eliminate that member variable altogether, of course.) It's almost never good to write code that accesses the member variables of another class.

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.

P.S.

Note that this loop:

for ( int i = 0; i <= 5; i++ )

will execute six times, not five. That "<=" should be a "<".
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have revised the code as suggested.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Now you need another class that implements RandomNumberGenerator.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic