• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With an interface.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have revised the code as suggested.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Now you need another class that implements RandomNumberGenerator.
 
Crusading Chameleon likes the size of this ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic