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:



Exercise 0: Preparation

a. Review Numbers in Java.

b. Create a new project for this lab, called Numbers, and package within that project, called username.numbers. Note that you should use your own username in place of username.

mkdir /home/username/CSC152/Numbers
mkdir /home/username/CSC152/Numbers/username/
mkdir /home/username/CSC152/Numbers/username/numbers

c. Start Eclipse and load this 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: ");

b. Determine experimentally what happens when you add 1, 2, 5, and 100 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.

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

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 you 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.


Monday, 7 February 2005 [Samuel A. Rebelsky]

Thursday, 8 September 2005 [Samuel A. Rebelsky]

Monday, 6 February 2006 [Samuel A. Rebelsky]

Tuesday, 7 February 2006 [Samuel A. Rebelsky]

This page was generated by Siteweaver on Thu Mar 30 15:24:40 2006.
The source to the page was last modified on Tue Feb 7 08:43:43 2006.
This page may be found at

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky