0

I want to choose a function randomly. This is the following code:

public class Thank_you_five implements RandomInterface
{
  public static void Thankyou_five(){...}
}
public class Thank_you_four implements RandomInterface
{
  public static void Thankyou_four(){...}
}
public class Thank_you_three implements RandomInterface
{
  public static void Thankyou_three(){...}
}
public interface RandomInterface
{
  public static void Thankyou_five();
  public static void Thankyou_four();
  public static void Thankyou_three();
}

So my goal here is to pick a function randomly, like in python we random.choose() and some function inside, I want to achieve the same with Java

Please help.

Thanks, Adrija

Adrija
  • 89
  • 1
  • 9
  • *Static methods in interfaces should have a body* – Ruslan Feb 20 '19 at 19:28
  • 2
    how about use a random generator, generate a random number, and if that number falls in a particular range, execute a particular function? A few `if` statements should do the trick – Arun A S Feb 20 '19 at 19:28
  • Generate a random number integer between `0` and `2` or how many functions do you have. Create switch based on the generated number and the functions you have. – Ervin Szilagyi Feb 20 '19 at 19:30
  • I edited the question – Adrija Feb 20 '19 at 19:47
  • The code above cannot and will not compile: when a class implements an interface, it must implement *all* the methods in that interface. Using reflection, as Ervin Szilagyi suggested, is overkill. BEST SUGGESTION: have *one* method (probably a static method) that calls any of n functions randomly. An interface would be useful: but it would have exactly TWO methods: the static "callRandomFunction()" method, and a virtual "doSomethingRandom()" method. – paulsm4 Feb 20 '19 at 19:49
  • @paulsm4 I am trying your approach, but I am having trouble calling the functions randomly(that's my goal from the beginning) . Do you have any suggestions? Thank you. – Adrija Feb 20 '19 at 21:03
  • I thought you already read and accepted Ruslan's approach: 1) Define an interface, 2) Define a bunch of classes that implement that interface, 3) Instantiate and store references to those classes in a List or an array, and 4) use Random() to index into the array, effectively calling a method at random. Q: Does that sound reasonable to you? Q: Did it work? – paulsm4 Feb 20 '19 at 21:46
  • @paulsm4 Thanks :) I did try Ruslan's approach. But your answer seems to be a bit simpler. Hence the question. – Adrija Feb 20 '19 at 21:56

2 Answers2

4

First of all it would be probably better to define one abstarct method inside interface:

public interface RandomInterface{
    void thankYou();
}

Then you can create several implementations:

RandomInterface t1 = () -> System.out.println("thank you 1");
RandomInterface t2 = () -> System.out.println("thank you 2");
RandomInterface t3 = () -> System.out.println("thank you 3");

To get random implementation you can add all objects to array and generate random index:

RandomInterface[] arr = {t1, t2, t3};
int i = new Random().nextInt(arr.length);
arr[i].thankYou();
Ruslan
  • 5,678
  • 1
  • 16
  • 33
  • I will apreciate if downvoter could explain what is wrong with this answer. I can edit or delete it at all if it doesn't address the question – Ruslan Feb 20 '19 at 19:56
2

Frankly, I like Ruslan's suggestion better. But as long as you ask, this is along the lines of what I was thinking:

package com.example;

import java.util.Random;

public abstract class RandomFn {

    public static RandomFn factory() throws Exception {
        int r = new Random().nextInt(3);
        switch (r) {
           case 0: return new ThankYouOne();
           case 1: return new ThankYouTwo();
           case 2: return new ThankYouThree();
           default : throw new Exception ("ThankYou(" + r +"): unsupported value!");
        }
    }

    public abstract void thankYou();

    public static void main(String[] args) {
        try {
            RandomFn.factory().thankYou();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}

class ThankYouOne extends RandomFn {
    public void thankYou() {
        System.out.println("Thank you one");
    }
}

class ThankYouTwo extends RandomFn {
    public void thankYou() {
        System.out.println("Thank you two");
    }
}

class ThankYouThree extends RandomFn {
    public void thankYou() {
        System.out.println("Thank you three");
    }
}

If you don't like the idea of many classes, then

public class RandomFn {

    public void thankYou () throws Exception {
        int r = new Random().nextInt(3);
        switch (r) {
           case 0: 
             thankYouOne(); break;
           case 1: 
             thankYouTwo(); break;
           case 2: 
             thankYouThree(); break;
           default : 
             throw new Exception ("ThankYou(" + r +"): unsupported value!");
        }
    }

    private void thankYouOne() { System.out.println("Thank you one"); }

    private void thankYouTwo() { System.out.println("Thank you two"); }

    private void thankYouThree() { System.out.println("Thank you three"); }
    ...
paulsm4
  • 99,714
  • 15
  • 125
  • 160