Programming Assignment 09


Due: Week of Nov. 7 in lab


  1. 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)
    Rationalplus(Rational b)//sum of this number and b
    Rationalminus(Rational b)//difference of this number and b
    Rationaltimes(Rational b)//product of this number and b
    Rationalover(Rational b)//quotient of this number and b
    StringtoString()//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)

  2. 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
      Molecular weight is: 46.08
    • Input:$ java PeriodicTable
      Output:(user enters string after prompt...)
      Enter a molecular equation like "H 2 O": Cu Sn
      Molecular weight is: 182.23

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


Grading

Each assignment is graded out of 100% based on a combination of the criteria listed in the assignment description and programming style (e.g., good comments and naming) -- following instructions is extremely important in computer science, train yourself to think like the computer; programming style is important to be able to communicate your solutions to another programmer; both of which you are graded upon.

Specific instructions. Failure to do these steps will result in a loss of points.

Turn in instructions. Each programming assignment is to be turned in before your lab section. You will turn in a soft copy of the assignment (.java or .zip for multiple java files) through blackboard. You will also turn in a hard copy of the assignment in your lab section with a signed coverpage (each program should start a new page of the hard copy).

If there are any discrepencies in grades please see the instructor during his office hours or by appointment (do not discuss with the lab assistants or graders).