Espresso: A Concentrated Introduction to Java


Laboratory: Numeric Values in Java

Summary: In this laboratory, you will extend your knowledge of numeric values in Java.

Corresponding Reading:

Primary Classes Used:

Contents

Exercises

Exercise 0: Preparation

a. Start Eclipse, and create a new package named username.numbers within the Code project.

Exercise 1: Limits on Integer Values

a. Read the documentation for the various class forms of integers and write a main class that prints out the largest and smallest legal value for int and long. For example, to print out the largest int, you would use something like:

pen.print("Largest int: ");
pen.println(Integer.MAX_VALUE);

b. Determine experimentally what happens when you add 1, 2, 5, and 100 to each largest legal value. Note that you may get some strange results because, well, it doesn't make a lot of sense to add beyond the largest value.

c. Determine experimentally what happens when you subtract 1, 2, 5, and 100 from each smallest legal value.

Exercise 2: Limits on Real Values

a. Determine experimentally the smallest positive non-zero float value you can represent. (In other words, before looking up the value, think of a way to use arithmetic to compute smaller and smaller numbers. See how long you can do this.)

Note that Java interprets any numeric literal with a decimal point (e.g., 2.71) as a double. Further, Java will not allow you to assign a double value to a float variable since doing so may lose information. A statement similar to the following will help you work around this. In this statement, the "cast" operator converts the double to a float before the assignment takes place.

float f = (float)2.71;

b. Read the documentation for java.lang.Float and see if it provides further guidance on what values are representable.

c. Repeat the exercise to determine the smallest postive double value you can represent.

d. Try adding a few small numbers to the largest float (or double) value that can be represented, and print out the result. Try multiplying the largest float (or double) by 1.0001, and print out the result. What do you think might be going on?

Exercise 3: Square Roots and Approximations

You may recall that in a previous course, you answered the following question when you first explored numeric computation:

Have DrScheme compute the square of the square root of 2 and subtract 2 from the result. Ideally, the difference should be 0; why isn't it? How big is the difference?

Redo this exercise in Java, using doubles for the computation.

Note that java.lang.Math.sqrt is useful for computing square roots.

Exercise 4: A Simple Calculator

a. Write a main class that reads in two int values and prints out their sum, difference, product, and quotient. Note that you will probably need to use a method of the Integer class to convert the strings (which you already know how to read) to integers.

b. What quotient do you get when you enter the integers 5 and 2? Why?

c. Rewrite your program so that it prints quotients including their fractional component.

d. What happens when you enter two integers whose product is larger than the largest legal integer?

Exercise 5: An Improved Calculator

a. Rewrite your calculator from the previous exercise to use java.math.BigInteger values rather than int values. Note that instead of the infix operations, you should instead use the appropriate methods from the BigInteger class.

b. Confirm that it is possible to represent some very large integers using the new calculator.

Exercise 6: Other Cool Math Functions

Read the documentation for java.lang.Math and summarize for yourself what methods are available.


Written and revised by Samuel A. Rebelsky, 2005-2006.
Revised further by Marge M. Coahran, 2006-2007.
Samuel A. Rebelsky
rebelsky@grinnell.edu
--->