Second Summer 2002

Methods (Chapter 7): The warm-ups.

Questions:

1. Give realistic examples of the following:

1. A method with a `double` parameter and a `double` return value.

`Math.sqrt(...)`

2. A method with an `int` parameter and a `double` return value.

Perhaps `Math.log(...)` used to find out the number of digits?!

3. A method with an `int` parameter and a `String` return value.

`weekday(...)` below

4. A method with two `double` parameters and a boolean return value.

`approximatelyEqual(...)`

5. A method with no parameter and an `int` return value.

`nextInt(...)`

6. A method with an `Ellipse2D.Double` parameter and a `double` return value.

`area(...)` below

7. A method with a `Line2D.Double` parameter and a `Point2D.Double` return value.

`midPoint(...)`

Just describe what these methods do. Do not program them. For example, some answers to the first question are "sine" and "square root".

2. True or false?

1. A method has exactly one `return` statement

False.

2. A method has at least one `return` statement

False.

3. A method has at most one return value

False.

4. A method with return value `void` never has a `return` statement

False.

5. When executing a `return` statement, the method exits immediately

True.

6. A method without parameters always has a side effect.

False.

7. A method with return value `void` always has a side effect.

False.

8. A method without side effects always returns the same value when called with the same parameters

True if that's the only thing that happens.

3. Write detailed method comments. Be sure to describe those conditions under which the method cannot compute its result. Just write the comments, not the methods.
```/** Computes the square root
@param x a floating point number
@return
*/
public static double sqrt(double x)

/** Computes the midpoint of two points

*/
public static Point2D.Double midpoint(Point2D.Double a, Point2D.Double b)

/** Computes area of ellipse

*/
public static double area(Ellipse2D.Double c)

/** Translates int into Roman numeral

*/
public static String romanNumeral(int n)

/** Computes slope (tangent of angle)

*/
public static double slope(Line2D.Double a)

/** Predicate reports whether year is leap year or not

*/
public static boolean isLeapYear(int year)

/** Translates number into weekday

*/
public static String weekday(int day)
```

4. Consider these methods:
```public static double f(double x)
{
return g(x) + Math.sqrt(h(x));
}

public static double g(double x) { return 4 * h(x); }

public static double h(double x) { return x * x + k(x) - 1; }

public static double k(double x) { return 2 * (x + 1); } ```
Without actually compiling and running a program, determine the results of the following method calls:
```double x1 = f(2);
double x2 = g(h(2));
double x3 = k(g(2) + h(2));
double x4 = f(0) + f(1) + f(2);
double x5 = f(-1) + g(-1) + h(-1) + k(-1);```
To double check you should be writing a program.

Here's x1 = f(2) = g(2) + Math.sqrt(h(2)) = 4 * h(2) + Math.sqrt(h2))

h(2) = 22 + k(2) - 1 = 4 + 2 * ( 2 + 1) - 1 = 9

That makes x1 = 4 * 9 + 3 = 39

All the others will be done in the same way.

5. A predicate method is a method with return type `boolean`. Give an example of a predicate method and an example of how to use it.

See problem set 9.

6. What is the difference between a parameter value and a return value?

It's the same difference as the one between input and output.

What is the difference between a parameter value and a parameter variable?

The first is initialized upon invocation, the other one must be initialized by the method designer.

What is the difference between a parameter value and a value parameter?

No difference, page 279 (advanced topic 7.1 has something on that).

7. Ideally, a method should have no side effect.

We need a definition for that, but let's not be picky now.

Can you write a program in which no method has a side effect?

By all means, yes: recursive factorial, recursive Fibonacci, recursively checking for palindromes.

Would such a program be useful?

Yes, of course. Sometimes though other ways might be more efficient.

8. What is the difference between a method and a program?

Method just part of a program.

The `main` method and a program?

A `main` just one entry point in a program.

9. What preconditions do the following methods from the standard Java library have?
```Math.sqrt
Any double, should be >= 0, but fails safely.

Math.tan
Mostly not a whole number of right angles.

Math.log
The argument should be >= 0.

Math.exp
Any double.

Math.pow
Tricky as in some cases the first argument must be non-negative.

Math.abs
No restrictions.
```

10. When a method is called with parameters that violate its precondition, it can terminate, or it can fail safely. Give two examples of library methods (standard, or the library methods used in this book) that fail safely when called with invalid parameters, and give two examples of library methods that terminate.

`Math` methods fail safely. Methods that throw an exception that is not caught will cause the program to terminate.

11. Consider the following method that is intended to swap the values of two floating point numbers:
```public static void falseSwap(double a, double b)
{
double temp = a;
a = b;
b = temp;
}

public static void main(String[] args)
{
double x = 3;
double y = 4;
falseSwap(x, y);
System.out.println(x + " " + y);
}```
Why doesn't the method swap the contents of `x` and `y`?

Because it works on copies of those values.

12. How can you write a method that swaps two floating-point numbers?

Hint: Use `Point2D.Double`.

```static void swap(Point2D.Double p) {
p.setLocation(new Point2D.Double(p.getY(), p.getX()));
}```

Last updated: Jun 16, 2002 by Adrian German for A201