Chapter 4: Short Answers

4.1 Write a method header for a method named translate that takes an integer parameter and returns a double.

4.2 Write a method header for a method named find that takes a String and a double as parameters and returns an integer.

4.3 Write a method header for a method named printAnswer that takes three doubles as parameters and doesn’t return anything.

4.4 Write the body of the method for the following header. The method should return a welcome message that includes the user’s name and visitor number. For example, if the parameters were “Joe” and 5, the returned string would be “Welcome Joe! You are visitor number 5.” String welcomeMessage (String name, int visitorNum)

4.5 Write a method called powersOfTwo that prints the first 10 powers of 2 (starting with 2). The method takes no parameters and doesn’t return anything.

4.6 Write a method called alarm that prints the string”Alarm! ” several times on separate lines. The method should accept an integer parameter that tells it how many times the string is printed. Print an error message if the parameter is less than 1.

4.7 Write a method called sum100 that adds up all the numbers from 1 to 100, inclusive and returns the answer.

4.8 Write a method called maxOfTwo that accepts two integer parameters from the user and returns the larger of the two.

4.9 Write a method called sumRange that accepts two integer parameters that represent a range such as 50 to 75. Issue an error message and return zero if the second parameter is less than the first. Otherwise, the method should return the sum of the integers in that range (inclusive).

4.11 Write a method called countA that accepts a String parameter and returns the number of times the character ‘A’ is found in the string.

4.13 Write a method called average that accepts two integer parameters and returns their average as a floating-point value.

4.14 Overload the average method of Exercise 4.13 so that the method returns the average of three integers.

4.15 Overload the average method of Exercise 4.13 to take four integer parameters and return their average.

4.16 Write a method called multiConcat that takes a String and an integer as parameters. Return a String made up of the string parameter concatenated with itself count times, where count is the integer.
For example, if the parameter values are “hi” and 4, the return value is “hihihihi”.Return the original string if the integer parameter is less than 2.

4.17 Overload the multiConcat method from Exercise 4.16 so that if the integer parameter is not provided, the method returns the string concatenated with itself. For example, if the parameter is
“test”, the return value is “testtest”.

4.19 Write a method called floatEquals that accepts three floating-point values as parameters. The method should return true if the first two parameters are no further apart from each other than the third parameter.
For example, floatEquals (2.453, 2.459, 0.01) should return true because 2.453 and 2.459 are 0.006 apart from each other and 0.006 is less than 0.01.
Hint: recall the discussion on comparing floating-point values for equality.

Chapter 3: Comparing Floating Point Numbers