-2

I am required to implement an interface as part of my final school assignment for my Java class. The overridden interface is supposed to print a short message describing how a football player celebrates being drafted in the NFL, something like this:

System.out.println("name dances to celebrate his draft");

However my interface is not printing the message when I put it into the methods which allow the user to add a player to their roster. Here is the overridden interface in the program:

//Overridden celebrate method
    @Override
    public void celebrate(int i, int place){
        int randomInteger;

        if (place == 0) //Player is offensive
        {
            randomInteger = random1Thru6();

            //players() is a method that contains all the players in the team
            //'place' refers to the players location in the array of players on the team
            switch (randomInteger) 
            {

                case 1: System.out.println(players(i).get(place) + " dances to celebrate his draft!");
                break;

                case 2: System.out.println(players(i).get(place) + " cartwheels to celebrate his draft!");
                break;

                case 3: System.out.println(players(i).get(place) + " throws a party to celebrate his draft!");
                break;

                case 4: System.out.println(players(i).get(place) + " sings to celebrate his draft!");
                break;

                case 5: System.out.println(players(i).get(place) + " gets root beer for everyone to celebrate his draft!");
                break;

                case 6: System.out.println(players(i).get(place) + " gets donuts to celebrate his draft!");
            }
        }

        else if (place == 1) //Player is defensive
        {
            randomInteger = random1Thru6();

            switch (randomInteger) 
            {

                case 1: System.out.println(players(i).get(place) + " dances to celebrate his draft!");
                break;

                case 2: System.out.println(players(i).get(place) + " cartwheels to celebrate his draft!");
                break;

                case 3: System.out.println(players(i).get(place) + " throws a party to celebrate his draft!");
                break;

                case 4: System.out.println(players(i).get(place) + " sings to celebrate his draft!");
                break;

                case 5: System.out.println(players(i).get(place) + " gets root beer for everyone to celebrate his draft!");
                break;

                case 6: System.out.println(players(i).get(place) + " gets pizza to celebrate his draft!");
            }
        }



    } 

I am supposed to have one different celebratory response for offensive and defensive positions as is shown in the code above. Here is the referenced random1Thru6() method:

public int random1Thru6() { //used to get a random number from 1 to 6
        int randomInteger = (int)Math.random() * 10;
        //this is supposed to call the method over and over again until it gives a number from 1-6 but it has been printing 7-9 as well
        if (randomInteger > 6)
            random1Thru6();

        return randomInteger;
    }

And here is the players() method:

//holds and prints all the players
    public ArrayList<String> players(int i) {

        ArrayList<String> returnedList = new ArrayList<>();

        // Christian McCaffrey is a Running Back, Corn Elder is a Corner Back for the Carolina Panthers
        ArrayList<String> Players1 = new ArrayList<String>();
        Players1.add("Christian McCaffrey");
        Players1.add("Corn Elder");

        //Jake Butt is a Tight End, Brendan Langley is a Corner Back for the Denver Broncos
        ArrayList<String> Players2 = new ArrayList<String>();
        Players2.add("Jake Butt");
        Players2.add("Brendan Langley");

        //Ryan Switzer is a Wide Receiver, Taco Charlton is a Defensive End for the Dallas Cowboys
        ArrayList<String> Players3 = new ArrayList<String>();
        Players3.add("Ryan Switzer");
        Players3.add("Taco Charlton");

        //Dalvin Cook is a Running Back, Ifeadi Odenigbo is a Defensive Line for the Minnesota Vikings
        ArrayList<String> Players4 = new ArrayList<String>();
        Players4.add("Dalvin Cook");
        Players4.add("Ifeadi Odenigbo");

        switch (i)
        {
        case 1: returnedList.addAll(Players1);
                break;

        case 2: returnedList.addAll(Players2);
                break;

        case 3: returnedList.addAll(Players3);
                break;

        case 4: returnedList.addAll(Players4);
                break;      
        }

        return returnedList;

    }

Here is how the celebrate() method is called:

for (int l = 0; l < players(i).size(); l++)
        {
            if (choosePlayer.equalsIgnoreCase(players(i).get(l)))
            {
                addPlayer(players(i).get(l));
                celebrate(i, l);
                enterRoster();
            }
        }

And:

addPlayer(players(i).get(place));
celebrate(i, place);
enterRoster();

addPlayer(int i, int place) is a method that adds the player for team 'i' in the position of 'place' in the team's player array into the ArrayList of the user's roster.

NOTE: I checked what number was being called by random1Thru6() as suggested in a comment and now I understand why it wasn't printing the celebrate message, since I had (int)Math.random() * 10 it was always returning 0 so I changed it to:

double randomDouble = Math.random() * 10;
int randomInteger = (int)randomDouble;

Now it prints the celebrate messages but random1Thru6() is now returning all numbers 1-9, please explain how I can make this method call itself recursively until it will return a number 1-6.

Thank you delephin for your comment!

NOTE: I have accepted delephin's answer shown below, thank you all for the help!

Will.O
  • 59
  • 6
  • 1
    Except in the case of default methods, interfaces don't _do_ anything. It sounds like you instead are writing a class which perhaps implements an interface. Could you clarify? – CollinD Jun 15 '17 at 21:28
  • @CollinD Thank you, I have edited my question. I hope it simplifies the problem. – Will.O Jun 15 '17 at 21:32
  • 6
    Not enough code to help. Potential reasons nothing is printed: 1) You don't call the method. --- 2) Value of `place` is not 0 or 1. --- 3) `random1Thru6()` returns value outside range 1-6. --- Best help we can give is this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Jun 15 '17 at 21:35
  • 1
    Show us how you call this method and how you generate the random number for the switch. – takendarkk Jun 15 '17 at 21:36
  • 1
    Print the method's input, and the value returned by random1Thru6() – delephin Jun 15 '17 at 21:38
  • You're not returning the result of `random1Thru6()` when you call it recursively. It should be `if (randomInteger > 6) return random1Thru6();`. But in general, it's not a good idea to use recursion for open-ended retries. – shmosel Jun 15 '17 at 22:16

1 Answers1

0

Add to your main class:

static Random r = new Random();

static {
    r.setSeed(System.currentTimeMillis());
}

and change your randomizer method to something like this:

public int random1Thru6() {
    return r.nextInt(6) + 1;
}

From a quick test, your previous randomizer was returning zeros.

delephin
  • 1,085
  • 1
  • 8
  • 10
  • Thank you for this answer! The method is working now and is generating the proper messages. – Will.O Jun 15 '17 at 22:15
  • 1
    why setting the seed? is that really needed? – user85421 Jun 15 '17 at 22:22
  • Indeed not. The Javadoc says: "This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor." So no need for that static block. – Klitos Kyriacou Jun 15 '17 at 23:01