Programming Assignment 09
Due: Week of Nov. 7 in lab
-
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 methodGCD
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 aprivate 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 typePeriodicTable
that reads values from a file to create an array ofElement
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
andPeriodicTable.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
andScanner
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
-
-
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.
- Name each file and program as listed in the instructions.
- Name all variables descriptively.
- Indent all programs appropriately.
- Use comments to help explain the steps and logic of your program.
- The top of your program should have a comment block where the first line
is your name, second line is your section, and third line is a description
of your java program.
//Name: Jory Denny
//Section: 00
//This program debates Batman vs Superman!
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).