Method Vocabulary and Contracts 🌶️
A library, a package, and an API documentation page are three different things — and a Javadoc comment's precondition and postcondition are a real contract between a method and whoever calls it, not just explanatory prose.
1. Math and String both live in java.lang, a collection of classes Java gives every program automatically. What is java.lang, in API terms?
2. You want to know exactly what arguments Math.pow takes and what it returns, without reading its source code. Where do you look?
3. Which of these is a class's ATTRIBUTE rather than one of its BEHAVIORS?
4. Math.pow(5.0, 3.0) is called by writing the CLASS name, Math, before the dot. Which of these is called the exact same way — class name before the dot, not a variable?
5. Consider this documented method. The PRECONDITION is the caller's promise. Exactly one of the four calls below breaks that promise. Type its number.
/** * Returns the average of two test scores. * Precondition: a and b are from 0 to 100 * Postcondition: returns a value from * the smaller score to the larger one */public static double average(int a, int b) { return (a + b) / 2.0;}
1. average(0, 100)2. average(50, 50)3. average(100, 0)4. average(150, 200)
4
6. Using the same average(int a, int b) method: type EXACTLY what average(60, 90) returns. (The postcondition promises a value from 60 to 90.)
75.0
7. A class already has public static int area(int w, int h). Exactly one of these, if added to the same class, compiles without conflict. Type its number.
1. public static double area(double w, double h)2. public static double area(int w, int h)3. public static int area(int width, int height)4. public static int area(int h, int w)
1
Your score for your class is recorded when you click Submit all — checking individual questions doesn't save it. You can submit again to improve your score; your best one counts.