Free Java Tutorials >> Table of contents >> Exceptions

4. Tell multiple jokes

Write a complete function definition named tellJokes. This function should not return any values, but it should accept one input, int n. Use a loop in your function to have it make calls to a predefined function aJoke, n times total. The aJoke function does not require any inputs or generate any return values.

Be careful to include all required parts of a function definition, with variable types as described.



 

1. Draw a scene

Complete the function definition for void drawScene(int width, int treeCount). width represents the width of the entire scene. To accomplish this, drawScene will have to make calls to void stars(int width, int size), and then void trees(int width, int count). The function definitions for those functions are hidden from you, but you may still call on them in your code. Make sure you use the proper syntax for function CALLS.

For the input values you pass to the stars and trees functions, get the values from the drawScene function's input variables and match them up appropriately - note that both functions need a width, and it's perfectly fine to use the same variable to provide inputs to two different functions. The stars function also accepts an input value for size - for our scene, let's always use stars of size 3.

For example, if drawScene(40, 7) is called, your code should call stars with inputs of 40 and 3, and then call trees with inputs of 40 and 7.


void drawScene(int width, int treeCount) {

}

3. Randomize coordinates

Write a complete function definition for a function called randomizeCoords. This function should set the values of three global variables, x, y, and z to three different random numbers from 1-100. The global variables are already defined "behind the scenes", so do not define them yourself, just set their values. Your function should not return any values or receive any inputs, only set values.

You can get a random number by calling the random function. That function takes two inputs, the low end of the range of possible numbers followed by the high end. For example:

n = random(5, 40);

Sets n to a random number from 5-40. Since you want your three variables to all be different random numbers, make sure you call random multiple times.

Note: For the purposes of this problem, please have your function assign values to x first, then y, then z, to allow the auto-grader to work correctly.

Be careful with your function definition syntax. Make sure to include every required part: return type (or void), function name, inputs (if any), and code.



 

5. Hypotenuse

The hypotenuse (long diagonal side) of a right triangle can be calculated based on the legs (short sides) using the Pythagorean Theorem: a^2 + b^2 = c^2. a and b represent the leg lengths, and c represents the hypotenuse length. Based on this, you can calculate c by taking the square ROOT of the SUM of a^2 and b^2.

For example, if a is 3 and b is 4, c is the square root of 3^2 + 4^2 = 25, so c is 5. If a is 5 and b is 12, then c is the square root of 5^2 + 12^2 = 169, so c is 13.

Java provides a square root function:

x = Math.sqrt(y);

sets x to the square root of y. It does not provide a squaring function, but you can always square a number by multiplying it by itself.

Write a complete function definition for hypot(double a, double b), which returns a double value equal to the hypotenuse based on the values of inputs a and b. Make sure your function definition includes all required pieces with variable types matching up.


    

2. Name chooser

Complete the function String chooseName(String realName, String nickname, boolean formalOccasion). It should return one of the two String input values. Use the boolean input value and an if statement to decide which one to return. Obey the rule that if it IS a formal occasion, the real name should be used, otherwise, the nickname should be used.

Make sure to use the proper statement for returning a value from a function.

It may be useful to create a local variable to store the String that your function will return.


String chooseName(String realName, String nickname, boolean formalOccasion) {

}

Previous: Common Interfaces

Next: Linked Lists