|
Second Summer 2002 |
Questions:
double parameter and a double return value.
Math.sqrt(...)
int parameter and a double return value.
Perhaps Math.log(...) used to find out the number of digits?!
int parameter and a String return value.
weekday(...) below
double parameters and a boolean return value.
approximatelyEqual(...)
int return value.
nextInt(...)
Ellipse2D.Double parameter and a double return value.
area(...) below
Line2D.Double parameter and a Point2D.Double return value.
midPoint(...)
return statement
False.
return statement
False.
False.
void never has a return statement
False.
return statement, the method exits immediately
True.
False.
void always has a side effect.
False.
True if that's the only thing that happens.
/** 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)
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:
To double check you should be writing a program.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);
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.
boolean. Give an example of a predicate method and an
example of how to use it. See problem set 9.
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).
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.
Method just part of a program.
The main method and a program?
A main just one entry point in a program.
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.
Math methods fail safely. Methods that
throw an exception that is not caught will cause the program to terminate.
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.
Hint: Use Point2D.Double.
static void swap(Point2D.Double p) {
p.setLocation(new Point2D.Double(p.getY(), p.getX()));
}