Lab #1: OS X Games

advertisement
Programming Assignment 1
Big Positive Reals
Primitive data types provide a limited ability to store numbers. For example, a variable of int
data type cannot store any integer with more than 10 decimal digits and even a long cannot store
integers with more than 19 digits. In order to compensate for this limitation, the standard
libraries include a java.math.BigInteger class that supports integers of any size. Real numbers are
similarly limited. A variable of double data type is limited to 15 decimal digits of accuracy. For
this assignment you are expected to develop a class for storing real numbers of arbitrary length
and accuracy.
In order to test your work you will need to implement an application with a driver class that
creates the following user interface.
The top two text fields in this JFrame allow the user to input two positive real numbers of any
size. Valid input in either of these fields consists of any number of decimal digits optionally
followed by a decimal point and any number of decimal digits, where there must be at least one
digit on one side of the decimal point or the other. When the “ADD” button is clicked the sum of
the two numbers must be displayed in the bottom text field.
In order to complete this assignment you must implement a BigPositiveReal class and use it
wisely in your driver class. The required members of BigPositiveReal are shown in the following
class diagram.
BigPositiveReal
«constructor»
+ BigPositiveReal( String s )
«query»
+ String toString( )
+ BigPositiveReal add( BigPositiveReal r )
The constructor method must translate the parameter s into the proper internal form. Hint: it is
best to store each decimal digit as a separate byte, using two byte arrays - one for the digits left of
the decimal point (the integer part of the number) and another byte array for the digits right of
the decimal point (the fractional part of the number). The toString method must translate the
number back to a String, like those of the input text fields. The add method must return the
number stored within the BigPositiveReal added to the parameter. Also note that you may not
use either the BigInteger or BigDecimal classes in your solution.
The basic assignment is worth 20 points. For an additional 4 points implement BigPositiveReal
so that it never retains leading zeros in the integer part nor trailing zeros in the fractional part.
To submit your solution, please send all of the .java files to riley@cs.uwlax.edu.
Due date: Feb. 18, 2008
Download