Problem 3.2.7 on page 408.
Implement a data type Rational
for rational numbers that
supports addition, subtraction, multiplication, and division.
public class Rational
Rational(int numerator, int denominator) Rational plus(Rational b) //sum of this number and b Rational minus(Rational b) //difference of this number and b Rational times(Rational b) //product of this number and b Rational over(Rational b) //quotient of this number and b String toString() //string representation
Implement a private
helper method GCD
using
recursion to ensure that the numerator and denominator never have any
common factors. Include a test client (main
) that exercises all
of your methods. Specifically, design unit tests for each method. Separate
each unit test into a private static
helper functions that are
called inside of the main function. Do not worry about testing overflow.
Name your file Rational.java
. Worth 40 points.
Examples:
Input: | $ java Rational |
Output: | (this is specific to your unit tests) |
Problem 3.2.30 on page 413.
Elements. Create a data type Element
for entries in the
Periodic Table of Elements. Include data type values for element, atomic
number, symbol, and atomic weight and accessor methods for each of these
values. Then, create a data type PeriodicTable
that reads
values from a file to create an array of Element
objects
(file found
here)
and responds to queries on standard input so that a user can type a
molecular equation like "H 2 O" and the program responds by printing the
molecular weight. Develop APIs and implementations for each data type.
Name your files Element.java
and
PeriodicTable.java
. Worth 60 points.
Remember - BREAK THE PROBLEM DOWN INTO PIECES TO MAKE IT
EASIER. Equations are assumed to be a space separated list of an atomic
symbol (correctly capitalized) followed by an optional number.
Look at Java's String
and Scanner
APIs for useful
functions.
Examples:
Input: | $ java PeriodicTable |
Output: | (user enters string after prompt...) |
Enter a molecular equation like "H 2 O": C 2 H 6 O
|
Input: | $ java PeriodicTable |
Output: | (user enters string after prompt...) |
Enter a molecular equation like "H 2 O": Cu Sn
|
Bonus. Problem 3.2.25 on page 411. In 1843, Sir William Hamilton discovered an extension to complex numbers called quaternions. A quaternion is a vector a=(a_0, a_1, a_2, a_3) with the following operations: magnitude, conjugate, inverse, sum, product, quotient (more details in your book). Create a data type for quaternions and a test client that exercises all of your code. Quaternions extend the concept of rotation in three dimensions to four dimensions. They are used in computer graphics, control theory, signal processing, and orbital mechanics.
Name your file Quaternion.java
. Worth up to 20 points.