Java
TOPIC 28 – INTRO TO FUNCTIONS, PART 1

AP EXTRA

 

AP LESSON WORK

 

 

QUESTION 1


Write the function called plus10() that gets an integer x as parameter.  It simply returns the value x+10.  Then, from the main function, test your new function by calling it and outputting its value to screen.

QUESTION 2


Write the function called damage() that gets three parameters:

 

·         int lowDamage;

·         int highDamage;

·         double accuracy;

 

The value of accuracy is a decimal that expresses the percentage chance of getting a hit.  So, if the accuracy is 0.8, then there will be a hit 80% of the time and a miss 20% of the time.  If a miss occurs, the function should simply return -1.

 

If a hit occurs, a random value between lowDamage and highDamage is returned.

 

You should test your function with the following main function:

 

public static void main(String[] args)

{

    int linkDamage = damage(4, 10, 0.6);

    if (linkDamage == -1)
        System.out.println("Link missed.");

    else

        System.out.println("Link did " + linkDamage + " dmg.");

    int ganonDamage = damage(10, 20, 0.3);

    if (ganonDamage == -1)

        System.out.println("Ganon missed.");

    else

        System.out.println("Ganon did " + ganonDamage + " dmg.");

}