Programming Assignment 10


Due: Wed. Nov. 15 before lab


  1. (30 points) Write a description of an object-oriented framework. You can choose anything you would like, except for the aquarium and the world of Harry Potter as was discussed in class. You need at least four objects interacting with each other. Provide both a written description (typed, one paragraph per object with one paragraph overview) and a UML diagram (computer generated, not hand drawn!). Be sure to include correct notations for aggregation and composition in the diagram.
  2. (20 points) Rational numbers. Implement a data type Rational for rational numbers that supports addition, subtraction, multiplication, and division. Use the following UML diagram:

    Rational

    -intnumerator
    -intdenominator

    + 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
    +booleanequals(Rational b) //Equality comparison of Rationals
    -intgcd(int p, int q) //Greatest common denominator of a and b
    -voidreduce() //Helper to reduce numerator and denominator to lowest terms
    -static booleantestPlus() //Unit test for sum
    -static booleantestMinus() //Unit test for difference
    -static booleantestTimes() //Unit test for product
    -static booleantestOver() //Unit test for quotient
    -static booleantestToString() //Unit test for toString


    The rational should always be in reduced form, using the GCD function. The toString() method should only output the denominator if it is not one. Additionally, implement a main method inside of the class that executes all of the unit tests. Do not worry about testing and protecting against integer overflow.
    • Name the file and program Rational.java.
    • Example execution (java Rational):
      Specific to your main function.
  3. (50 points) Periodic Table. 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, responds to queries from standard input so that a user can type a molecular equation like "H 2 O", and prints the resulting molecular weight. Develop APIs and implementations for each data type.
    Hints: In Periodic table - (1) You can read a file twice, likely your first read through will simply count the number of elements to initialize your array and a second read through to construct the elements. (2) You can define a delimiter like ',' instead of ' ' (space) with a Scanner or make a Scanner over a whole string. Look these up in the API. (3) Make a single function to compute the molecular weight from a String. Use a Scanner object to read each element from the string. Recall there are functions like hasNextInt() inside of Scanner.
    • Name the files and programs Element.java and PeriodicTable.java.
    • Find a csv file for the elements and their properties here.
    • Example execution (java PeriodicTable) user provides input:
      Enter a molecular equation like "H 2 O": C 2 H 6 O
      Molecular weight is: 46.08
    • Example execution (java PeriodicTable) user provides input:
      Enter a molecular equation like "H 2 O": Cu Sn
      Molecular weight is: 182.23
  4. Bonus. (20 points) Quaternions. 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 main function 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 the file and program Quaternion.java.
    • Example execution (java Quaternion):
      Specific to your main function.


General Instructions, Turning in assignments, and Grading

General Instructions

Turn in Instructions

Each assignment will be turned in to both Blackboard (soft copy) and in class (hard copy). Assignments are due BEFORE, let me repeat, before class starts. This does not mean five minutes after class starts.

Points