| CSC 207 | Algorithms and Object Oriented Design | Spring 2011 |
Summary: In this lab, you will write and experiment with
objects of your first class. Our goal is to design, implement, and
test a Fraction class -- a way to represent exact
rational numbers as objects in Java.
mkdir somewhere/objects
cd somewhere/objects
cp ~weinman/public_html/courses/CSC207/2011S/labs/code/objects/TestBigInteger.java .
java.math.BigInteger
class allows programmers to compute with arbitrarily large integer
values. The limitation is that all of the operations are formulated
as method calls rather than expressions with infix or prefix
operators. For instance, the program TestBigInteger,
which you copied in the preparations, computes and prints out the sum
of three octillion and five octillion.
import java.math.BigInteger;
/** Demonstration of BigInteger objects
*
* @author John David Stone
* @author Jerod Weinman
* @author YOUR NAME HERE
*/
public class TestBigInteger
{
public static void main(String[] args)
{
BigInteger augend = new BigInteger("3000000000000000000000000000");
BigInteger addend = new BigInteger("5000000000000000000000000000");
BigInteger sum = augend.add(addend);
System.out.println(sum);
}
}
BigInteger class
and acquaint yourself with the methods for addition, subtraction,
negation, multiplication, and division.
TestBigInteger so that it also
prints out the quotient and the remainder when the product of
seven trillion and ninety-six trillion is divided by seventeen
million three hundred and nineteen.
BigInteger value. One of these
fields will be the numerator of a fraction expressing the rational
number and the other will be the denominator.
Fraction.java, write the definition
for a class that has two BigInteger fields
called numerator and
denominator. Should these fields be static? What
visibility should they have? Justify your answers.
Fraction
class. It should take two arguments, each
a BigInteger, and store the arguments in the
appropriate fields.
Fraction class. It should take two arguments,
each an int. From each argument, the constructor
should then build a BigInteger with the same value
and store that BigInteger into the corresponding
field.
(Hint: Invoke a this-constructor to make the
code very short.)
Note that the BigInteger constructor needs
a String as its argument. That's actually a pretty
inefficient way to get a BigInteger object. Examine
the class methods (static) of BigInteger
for a better way.
ArithmeticException (which is an unchecked
exception type) when an attempt is made to construct
a Fraction in which the denominator is zero. (If you
followed the hint in part c, you'll only have to change one of the
constructors.)
BigInteger class
supports a method that computes the greatest common divisor of
two BigIntegers.)
multiply
method to your Fraction class that takes
a Fraction as argument and multiplies the fraction to
which the message is sent by the fraction supplied as argument,
returning the product (as a Fraction, of course).
Will the product that you return always be expressed in lowest
terms?
add
method to your Fraction class that takes a
Fraction as argument and adds it to the fraction to
which the message is sent, returning the sum as
a Fraction.
subtract method to
your Fraction class.
negate method that takes no arguments and
returns the negative of the fraction
to which the message is sent.
reciprocal method that takes no arguments and
returns the reciprocal of the fraction to which the message is
sent (i.e., a new fraction with the numerator and denominator
swapped). Ensure that your method throws
an ArithmeticException if it is sent to a fraction in
which the numerator is zero.
quotient method to
your Fraction class, throwing
an ArithmeticException if the numerator of the
divisor is zero. Note that the quotient of any two non-zero
rational numbers can always be expressed exactly as a rational
number, so there is no need of a remainder in this case.
Fraction objects that show what their
numerators and denominators are.
toString method to your Fraction
class that takes no arguments and returns a string formed by
concatenating the string representation of the numerator, a slash
character, and the string representation of the denominator.
main method that
constructs some fractions, prints out their string
representations, performs some arithmetic on them, and prints out
the results.