UO.8 & UO.9: WRAPPER CLASSES AND USING THE MATH CLASS
(Last Q1 Objectives)
OBJECTIVES
I can:
create Integer and Double objects for wrapper classes
call Integer and Double methods for wrapper classes
call static methods
evaluate expressions that use the Math class methods
For every primitive type in Java, there is a built-in object type called a wrapper class.
The wrapper class for int is called Integer (notice the capital I), and for double it is called Double (notice the capital D).
Sometimes you may need to create a wrapped object for a primitive type so that you can give it to a method that is expecting an object.
To wrap a value, call the constructor for the wrapper class in earlier versions of Java.
In Java 9 on, this is depreciated which means it's not the best way to do this anymore, and you should instead just set it equal to a value.
However, the Exam covers Java 7 which does allow using the constructor.
// in older versions of Java (and on the Exam)
Integer i = new Integer(2); // create an Integer object with 2 in it
Double x = new Double(3.5); // create a Double object with 3.5 in it
// in newer versions of Java (9+)
Integer i = 2;
Double x = 3.5;
This is similar to using the constructor to create a new String object or creating a String object using a string literal:
String message = new String("Hello, World!");
String message = "Hello, World!";
But why should we use wrapper classes?
These wrapper classes (defined in the java.lang package) are useful because they have special values (like Integer.MAX_VALUE and Integer.MIN_VALUE) and methods that you can use.
We have already talked about the min and max values of an int (back in lesson 1.5) but as a reminder...
Recall that int values are stored using 4 bytes or 32 bits of memory.
2 ^ 32 = 4,294,967,296 possible integer values that can be stored using Java.
Half of these possible values are positive, and the other half are negative.
Therefore, there is an Integer.MAX_VALUE defined as 2,147,483,647 and an Integer.MIN_VALUE defined as -2,147,483,648. Zero is considered one of the positive integers.
If you try to store any number larger or smaller than these numbers as an int variable, it will result in integer overflow or integer underflow where an incorrect value could be stored.
Subtracting 1 from Integer.MIN_VALUE will result in Integer.MAX_VALUE and is called underflow.
Adding 1 to Integer.MAX_VALUE will result in Integer.MIN_VALUE and is called overflow.
Why would you ever need to use these?
These values are handy if you want to initialize a variable to the smallest possible value and then search through a sequence of values for a larger value.
AUTOBOXING
Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes.
This includes converting an int to an Integer and a double to a Double.
The Java compiler applies autoboxing when a primitive value is passed as a parameter to a method that expects an object of the corresponding wrapper class or assigned to a variable of the corresponding wrapper class.
As usual, that is a lot of words, so here is an example of autoboxing:
Integer i = 2;
Double x = 3.5;
UNBOXING
The Java compiler can also make an automatic conversion (called unboxing) from the wrapper class to the primitive type.
This includes converting an Integer to an int and a Double to a double.
The Java compiler applies unboxing when a wrapper class object is passed as a parameter to a method that expects a value of the corresponding primitive type or assigned to a variable of the corresponding primitive type.
Again, that is a lot of words, so here is an example of unboxing:
Integer i = 2; // autoboxing - wrap 2
int number = i; // unboxing - back to primitive type
USEFUL METHODS
The main reason you might want to use wrapper classes is to get access to useful methods!
Check out the following code which introduces a few:
USING THE MATH CLASS
There are lots of methods in the Math class defined in the java.lang package.
These are static methods which means you can call them by just using Classname.methodName() without creating an object.
For example, Math.abs(int) calculates the absolute value of an int argument (which is the value of a number without its sign, for example Math.abs(-4) = 4).
We can use Math.abs() without having to define an object of the class Math.
Games would be boring if the same thing happened each time you played the game.
Games often use random numbers to generated different possibilities!
So, we need to know how to use the Math.random() method to generate a random number.
The Math.random() method returns a number greater than or equal to 0.0, and less than 1.0.
You can see that it would be useful to cast the value to an integer.
It would also be awesome to return a random integer between some starting and ending value.
It is important to remember that casting a double value to an integer (int) will truncate (or throw away) any values after the decimal point.
The following code will create a random integer from 0 to 9:
Math.random returns a random number between 0.0 and 0.99, so when you multiply it by 10 and typecast to an int, it returns a value between 0 and 9.
The 10 is the range of values that you might want.
But what if you wanted to return a value between 1 and 10?
You can easily add or subtract from the random value to move the random number into a range starting from a new minimum number:
int(Math.random() * range) + min
If you are not sure what the range should be but you know the min and max values that you want, you can use this formula to calculate the necessary range:
range = max number - min number + 1
The following code has several examples that move a random number into a specific range:
Math.random() isn't the only Math class method that you can use.
Math.abs(int) calculates the absolute value of an int argument (which is the value of a number without its sign, for example Math.abs(-4) = 4).
Math.abs(double) returns the absolute value of a double value.
Math.pow(double, double) returns the value of the first parameter raised to the power of the second parameter.
Math.sqrt(double) returns the positive square root of a double value.
Taking the square root of a negative number in math results in complex solutions!
But attempting to take the square root of a negative number in Java results in NaN.
These are all listed on the Java Quick Reference Sheet that you can use during the exam.
SUMMARY
The Integer class and Double class are wrapper classes that create objects from primitive types.
The following Integer methods and constructors, including what they do and when they are used, are part of the Java Quick Reference Guide given during the exam:
Integer(value) constructs a new Integer object that represents the specified int value.
Integer.MAX_VALUE the maximum value represented by an int or Integer.
Integer.MIN_VALUE the minimum value represented by an int or Integer.
int intValue( ) returns the value of this Integer as an int.
The following Double methods and constructors, including what they do and when they are used, are part of the Java Quick Reference Guide given during the exam:
Double(double value) constructs a new Double object that represents the specified double value.
double doubleValue( ) returns the value of this Double as a double.
Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double.
The Java compiler applies autoboxing when a primitive value is:
passed as a parameter to a method that expects an object of the corresponding wrapper class.
assigned to a variable of the corresponding wrapper class.
Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double.
The Java compiler applies unboxing when a wrapper class object is:
passed as a parameter to a method that expects a value of the corresponding primitive type.
Assigned to a variable of the corresponding primitive type.
Static Math methods can be called using Math.method( ); for each method.
The following static Math methods are part of the Java Quick Reference:
int abs(int) returns the absolute value of an int value (which means no negatives).
double abs(double) returns the absolute value of a double value.
double pow(double, double) returns the value of the first parameter raised to the power of the second parameter.
double sqrt(double) returns the positive square root of a double value.
double random( ) returns a double value greater than or equal to 0.0 and less than (but not including) 1.0.
The values returned from Math.random can be manipulated to produce a random int or double in a defined range.
(int)*(Math.random( ) * range) + min moves the random number into a range starting from a minimum number.
Easily calculate the needed range using max number - min number + 1. For example, to get a number in the range of 5 to 10, use the range 10 - 5 + 1 = 6 and the min nunmber 5: (int)(Math.random( ) * 6) + 5.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.