google search

Custom Search

Friday, August 8, 2008

Summary of Numbers and Questions and Exercises: Numbers

You use one of the wrapper classes – Byte, Double, Float, Integer, Long, or Short – to wrap a number of primitive type in an object. The Java compiler automatically wraps (boxes) primitives for you when necessary and unboxes them, again when necessary.

The Number classes include constants and useful class methods. The MIN_VALUE and MAX_VALUE constants contain the smallest and largest values that can be contained by an object of that type. The byteValue, shortValue, and similar methods convert one numeric type to another. The valueOf method converts a string to a number, and the toString method converts a number to a string.

To format a string containing numbers for output, you can use the printf() or format() methods in the PrintStream class. Alternatively, you can use the NumberFormat class to customize numerical formats using patterns.

The Math class contains a variety of class methods for performing mathematical functions, including exponential, logarithmic, and trigonometric methods. Math also includes basic arithmetic functions, such as absolute value and rounding, and a method, random(), for generating random numbers.
Questions

1. Use the API documentation to find the answers to the following questions:

a. What Integer method can you use to convert an int into a string that expresses the number in hexadecimal? For example, what method converts the integer 65 into the string "41"?

b. What Integer method would you use to convert a string expressed in base 5 into the equivalent int? For example, how would you convert the string "230" into the integer value 65? Show the code you would use to accomplish this task.

c. What Double method can you use to detect whether a floating-point number has the special value Not a Number (NaN)?

2. What is the value of the following expression, and why?

Integer.valueOf(1).equals(Long.valueOf(1))

Exercises

1. Change MaxVariablesDemo to show minimum values instead of maximum values. You can delete all code related to the variables aChar and aBoolean. What is the output?

2. Create a program that reads an unspecified number of integer arguments from the command line and adds them together. For example, suppose that you enter the following:

java Adder 1 3 2 10

The program should display 16 and then exit. The program should display an error message if the user enters only one argument. You can base your program on ValueOfDemo.

3. Create a program that is similar to the previous one but has the following differences:

* Instead of reading integer arguments, it reads floating-point arguments.
* It displays the sum of the arguments, using exactly two digits to the right of the decimal point.

For example, suppose that you enter the following:

java FPAdder 1 1e2 3.0 4.754

The program would display 108.75. Depending on your locale, the decimal point might be a comma (,) instead of a period (.).

0 comments:

 

blogger templates | Make Money Online