Question

I'm fairly beginner when it comes to Java, as I've focused more on Js for University rather than the OOP-heavy brother of it and as a result, I can't quite pass my parameters properly.

I'm looking to generate a random number in 'generateMonsterCode' and pass it into 'chooseMonster', but I can't quite grasp how to accomplish this.

/**
 * Randomnly choose a type of monster of varying hp/mp/loot drops
 * @return rndGen
 */
public int generateMonsterCode (){
    Random rndGens = new Random();
    int rndGen = 0;

    for (int i = 1; i < 2; i++) {
       rndGen = rndGens.nextInt(4) + 1;
    }

    return rndGen;
}

/**
 * Define which monster is chosen
 */
public void chooseMonster(){

    switch(rndGen){
        case 1: System.out.println("Monster:" + rndGen);
        case 2: System.out.println("Monster:" + rndGen);
        case 3: System.out.println("Monster:" + rndGen);
        case 4: System.out.println("Monster:" + rndGen);
    }     
}

Any help would be massively appreciated!

Updated regarding feedback

    /**
 * Randomnly choose a type of monster of varying hp/mp/loot drops
 * @return rndGen
 */

public void generateMonsterCode (){
Random rndGens = new Random();
    int rndGen = 0;

    rndGen = rndGens.nextInt(4) + 1;

    chooseMonster(rndGen);

}

/**
 * Define which monster is chosen
 */

private void chooseMonster(int rndGen){

    switch(rndGen){
    case 1: System.out.println("Monster:" + rndGen);
            break;
    case 2: System.out.println("Monster:" + rndGen);
            break;
    case 3: System.out.println("Monster:" + rndGen);
            break;
    case 4: System.out.println("Monster:" + rndGen);
            break;
    }

}

Would this perhaps be better code practice?

Was it helpful?

Solution

Parameters are specified by placing them in the parentheses beside the method declaration.

public void chooseMonster() is a method that takes no parameters.

public void chooseMonster(int i){ takes an int as a parameter and names it i in the scope of the method.

When you specify a parameter for a method, you then must pass a variable of the type specified when calling it. So, in generateMonsterCode(), after you generate the random number, you'll simply call chooseMonster(rndgen).

Within that method, you can refer to the int you passed into it with:

i.doSomething();

Does this Make sense? Let me know if I can explain any of this more carefully: I'm brushing against some of the fundamental aspects of object-oriented programming here, and it's important that you grasp these concepts if you want to have a role in the future.

FYI: On the other end of the method declaration is the return type - in your case, it's void, meaning that nothing is returned. An alternative way of structuring this would be to have your generateMonsterCode() method return an int:

public int generateMonsterCode(){ 
    .... generate random number
    return rdngen();

and call it at the beginning of your chooseMonster() method.

public void chooseMonster() {
      int i = generateMonsterCode();
      ... do work
}

Note that this is not necessarily better or worse than doing it the other way, I'm just mentioning it for purposes of explanation.

Also FYI: @supericy is right about the for loop you're using in your random generation. And you should also probably examine what the public access modifier does and whether you really need it (you probably don't) - but these issues are outside the scope of your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top