Problem

Wrapper objects: The wrapper classes discussed in Chapter 5 also provide you with the abil...

Wrapper objects: The wrapper classes discussed in Chapter 5 also provide you with the ability to instantiate objects that are wrapped versions of primitive variables. For example, to create a wrapped version of the double number x, you can do this:

double x = 55.0;Double xWrapped = new Double(x)

This instantiates an object of type Double, which is a wrapped version of the primitive variable, x. Then it assigns a reference to that object to the reference variable, xWrapped. The Double class has a number of pre-built methods that work with Double objects. You can read about these methods in Sun’s documentation on the Double class. The following program illustrates some of these methods.

/************************************************************** Wrapper.java* Dean&Dean** This program exercises some wrapped primitive numbers.*************************************************************/public class Wrapper{public static void main(String[] args){double x = 44.5;double y = 44.5;Double xW = new Double(x); // the object: wrapped xDouble yW = new Double(y); // the object: wrapped ySystem.out.println("object == object? " + (xW == yW));System.out.println("value == value? " +(xW.doubleValue() == yW.doubleValue()));System.out.println("object.equals(object)? " + xW.equals(yW));System.out.println("object.compareTo(object)? " +xW.compareTo(yW));yW = new Double(y + 3.0);System.out.println("object.compareTo(largerObject)? " + xW.compareTo(yW));yW = new Double(Double.NEGATIVE_INFINITY);System.out.println("-infinity isInfinite()? " + yW.isInfinite());} // end main} // end Wrapper class

Compile and run this program, and display the output. Read about the Double class in Sun’s documentation, and explain why each of the outputs comes out the way it does.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 6
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT