Programming Assignment 10
Due: Week of Apr. 3 before lab
-
(40 points) Rational numbers. Implement a data type Rational for rational
numbers that supports addition, subtraction, multiplication, and division.
Use the following UML diagram:
Rational - int numerator - int denominator + 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 + boolean equals(Rational b) //Equality comparison of Rationals - int gcd(int p, int q) //Greatest common denominator of a and b - void reduce() //Helper to reduce numerator and denominator to lowest terms - static boolean testPlus() //Unit test for sum - static boolean testMinus() //Unit test for difference - static boolean testTimes() //Unit test for product - static boolean testOver() //Unit test for quotient - static boolean testToString() //Unit test for toString
The rational should always be in reduced form, using the GCD function. ThetoString()
method should only output the denominator if it is not one. Additionally, implement amain
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.
- Name the file and program
-
(60 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 typePeriodicTable
that reads values from a file to create an array ofElement
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 likehasNextInt()
inside of Scanner.- Name the files and programs
Element.java
andPeriodicTable.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
- Name the files and programs
-
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.
- Name the file and program
General Instructions, Turning in assignments, and Grading
General Instructions
- Name each file and program as listed in the instructions.
- The top of each program should have a comment block where the first line
is your name and following lines are a description of your program.
//Name: Jory Denny
//This program debates Batman vs Superman! - Use proper coding style (described more in Grading below)
- Follow turn-in instructions precisely.
- Failure to complete any of these steps will result in a significant loss of points.
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.
- Soft copy (Online submission)
- Create a compressed
.zip
file of all Java programs needed to compile your program and all input files (if needed) to run your program.- If you do not know how to create a compressed
.zip
file, there is this cool new website you can use to search for instructions by entering"How to create .zip Windows 10"
or"How to create .zip MAC OSX"
for example.
- If you do not know how to create a compressed
- Submit
.zip
file on Blackboard by the stated due date and time.
- Create a compressed
- Hard copy (In-class submission)
- The first page of your hard copy must be a signed coverpage.
- Next put the programs in order as described in the description.
- If you do not know how to print a java file, there is this cool
new website you can use to
search for instructions by entering
"How to open and print .java file Windows 10"
or"How to open and print .java file MAC OSX"
for example. In combination with this, you may have to consult University of Richmond webpages to learn how to use campus printers. I recommend printing directly from sublime text editor on University computers (has printing feature enabled) and will print with syntax-highlighting (colors).
- If you do not know how to print a java file, there is this cool
new website you can use to
search for instructions by entering
- Staple all pages together.
- Turn in packet before class begins.
- I reserve the right to assign a 0 to any assignment failing to comply with these instructions. Even for something as small as a missing staple.
Points
- Each assignment is graded out of 100 points (not including bonus).
- Criteria and point distribution
- If the code is not named precisely or does not compile, -75% on a problem.
- If the code does not generate the correct output, -50% on a problem.
- Following instructions and algorithm used to solve, 25% on a problem. Following instructions is extremely important in computer science, train yourself to think like a computer. There are many ways to solve a problem, some may be better or worse than others.
- Stylistic elements of written code,
25% of a problem. Style includes (but is not
limited to):
- Descriptive comments on intent and purpose of code
- Descriptive and consistant naming conventions
- Indenting properly (after an opening brace, tab right by 1 indent; after a closing brace, tab left by 1 indent)
- Consistent spacing
- Consistent bracket placement (same line or on new line)
- Avoiding code duplication
- 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).