Espresso: A Concentrated Introduction to Java
Summary: In this laboratory, you will extend your knowledge of numeric values in Java.
Corresponding Reading:
Primary Classes Used:
java.lang.Double
java.lang.Float
java.lang.Integer
java.lang.Long
java.lang.Short
java.math.BigDecimal
java.math.BigInteger
java.lang.Math
Contents
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.
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 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.
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.
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.
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?
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.
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 http://www.cs.grinnell.edu/~rebelsky/Espresso/Labs/numbers.html
.
You may wish to validate this page's HTML ; ; Check with Bobby