What to practice
- Instantiate objects using overloaded constructors and invoke void and non-void instance methods.
- Construct and analyze Math.random(), Math.pow(), Math.sqrt(), and Math.abs() expressions.
- Use Integer and Double wrapper classes, autoboxing, and unboxing in collections.
Current CED topics: 1.7, 1.9, 1.10, 1.11, 1.12, 1.13, 1.14, 4.7
Current learning objectives: 1.9.A, 1.10.A, 1.11.A, 1.12.A, 1.13.A, 1.13.C, 1.14.A, 4.7.A
1. Core Concept Instruction
An object is an instance of a class created with the new keyword followed by a constructor call. When two reference variables alias the same object (e.g., Widget w2 = w1;), mutating the object through w2 also mutates what w1 observes; calling an instance method through a null reference throws a NullPointerException. A reference field without an explicit initializer defaults to null. A local reference variable has no usable default: Java requires definite assignment before it is read, so using an unassigned local variable is a compile-time error. Static methods of the Math class are invoked via the class name: Math.abs(x), Math.pow(base, exp) (always returns double), Math.sqrt(x) (returns double), and Math.random() (returns a double in [0.0, 1.0), inclusive of 0.0 and strictly less than 1.0). To generate a random integer uniformly from min to max inclusive, use (int)(Math.random() * (max - min + 1)) + min. An inheritance relationship describes a more specific class as a kind of a more general class. The current course includes recognizing that concept, but designing and implementing inheritance relationships is outside its exam scope. The class-writing tasks in this guide use ordinary classes and composition: storing and using another object as a field.
2. Worked Examples
Scaling Math.random() to an Integer Range
(a) Write an expression using Math.random() that generates a random integer from 15 to 42 inclusive. (b) Determine the exact range of integers produced by (int)(Math.random() * 12) - 4.
- Step 1 (Count Distinct Integers in [15, 42]): The number of integers from min = 15 to max = 42 inclusive is max - min + 1 = 42 - 15 + 1 = 28.
- Step 2 (Scale and Shift Math.random()): Since 0.0 <= Math.random() < 1.0, multiplying by 28 gives [0.0, 28.0). Casting to (int) gives {0, 1, through 27}, and adding 15 gives {15, 16, through 42}: (int)(Math.random() * 28) + 15.
- Step 3 (Analyze (int)(Math.random() * 12) - 4): Multiplying Math.random() by 12 gives [0.0, 12.0), and casting to (int) yields integers from 0 to 11 inclusive.
- Step 4 (Apply the Shift - 4): Subtracting 4 shifts {0 through 11} to {-4, -3, through 7}, so the minimum possible output is -4 and the maximum is 7.
Result:
Tracing Math Methods and a Double Wrapper
Trace the output of the following Java statements:
Double tol = Double.valueOf(2.5);
double hyp = Math.sqrt(Math.pow(6.0, 2) + Math.pow(8.0, 2));
double diff = Math.abs(7.0 - hyp) + tol;
- Step 1 (Unbox wrapper & evaluate Math.pow): Math.pow(6.0, 2) = 36.0 and Math.pow(8.0, 2) = 64.0; Math.sqrt(100.0) = 10.0.
- Step 2 (Evaluate Math.abs and autounboxing): Math.abs(7.0 - 10.0) = 3.0; adding unboxed tol (2.5) yields diff = 5.5.
Result: hyp = 10.0 and diff = 5.5