CSCI Homework Assignment 3 - Rationals and Mixed Numbers
Due Mon Sep 17 at 11:59pm
Introduction
For this assignment, you will be making two (!) simple classes that interact with one another. This will give you more practice designing and developing Java classes from scratch, as well working with arithmetic expressions.
A rational number is a number that consists of two integer parts: a numerator and a denominator. A rational number expresses the ratio of the numerator to the denominator. For example, the rational number 1/2 expresses the ratio of 1 to 2, while the rational number 3/2 expresses the ration of 3 to 2. A mixed number is the combination of a whole number and a proper fraction--a rational number where the numerator is smaller than the denominator. For example, the rational number 3/2 can be expressed as the whole number 1 1/2.
The Assignment.
For this program, you will define a class called Rational
to represent a rational number, and a class called Mixed
to represent a mixed number. Objects of each of these classes can be used for basic arithmetic operations (adding, subtracting, multiplying, dividing), as well as printing out a string version of themselves. Furthermore, objects can be converted from one type to the other. The Javadoc documentation for these classes is shown below.
Start by downloading and unzipping a copy of Homework3_fixed.zip. This project will let you submit your assignment whe completed
A basic recipe for writing programs:
- Write the comments and method signatures for each method you will need.
- Identify the state fields that you'll need and declare them (e.g.,
private int numerator;
) - Write constructors and test them. You should have two constructors as detailed in the below documentation.
- Write your getters (accessors) and setters (mutators) for each field and test them.
- Write one method at a time, testing each as you go. Hint! Your mathematical methods should figure out the new numerator and denominator for the result, and then create a
new
object that has those attributes! - Test the finished class definition
- Be sure to check that have commented your code to explain what you've done.
- Double-check that you put your name and other important components in the code. Look at the "Documentation" version of your code. It should look a lot like the specification below.
Hint! Start by implementing the Rational class first, and then consider how you can use that class's functionality to easily implement the Mixed class. What data type(s) can you use for the Mixed class's state? Is there any way we can take advantage of having already written the various mathematical methods for the Rational class?
You can use the included testers to help you make sure your code works--just comment out the methods of the tester class that aren't being used. You are also free to write your own testing methods if you want!
Submitting
Before submitting, test each of your methods thoroughly and double check for comments above each method and make sure your name is in each class. When you're convinced it's ready to go, submit the project through BlueJ just like you did in the previous Assignments (selecting Hwk3 of course).
Submitter will look for the Rational.java
and Mixed.java
files.
Grading
This assignment will be graded on the following criteria:
- Both the Rational and Mixed classes have been implemented, following the format described below.
- Each class has appropriate getters and setters.
- Each class has a toString() method that provides an appropriate output.
- Each class has the four methods for mathematical operations, and these methods work correctly.
- Rationals can be converted to Mixed number using toMixed(), and Mixed numbers can be converted to Rationals using toRational(). For example, I should be able to change 4/3 to 1 1/3 and back to 4/3.
- Your code is documented with comments explaining what you've done. In particular make sure that each method you create is preceeded by a comment explaining: what the method does, what the inputs (parameters) to the method are, and what the output (return value) of the method is.
Class Documentation
Class Rational
java.lang.Object Rational
-
public class Rational
- extends java.lang.Object
This class implements a Rational number with basic operations.
- Version:
- 2012.09.06
- Author:
- Joel Ross
Constructor Summary | |
---|---|
Rational()
Create a new Rational of the form 0/1 |
|
Rational(int numerator,
int denominator)
Create a new Rational of the form numerator/denominator. |
Method Summary | |
---|---|
Rational |
add(Rational r)
Adds another Rational r and returns a new Rational as the result. |
Rational |
divide(Rational r)
Divides by another Rational r and returns a new Rational as the result. |
int |
getDenominator()
Returns the denominator of the Rational. |
int |
getNumerator()
Returns the numerator of the Rational. |
Rational |
multiply(Rational r)
Multiplies by another Rational r and returns a new Rational as the result. |
void |
setDenominator(int denominator)
Sets the denominator of the Rational to the given value d. |
void |
setNumerator(int numerator)
Sets the numerator of the Rational to the given value n. |
Rational |
subtract(Rational r)
Subtracts another Rational r and returns a new Rational as the result. |
Mixed |
toMixed()
Returns a Mixed number form of the Rational |
String |
toString()
Returns a String representation of the Rational |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Class Mixed
java.lang.Object Mixed
-
public class Mixed
- extends java.lang.Object
This class implements a Mixed number with basic operations.
- Version:
- 2012.09.06
- Author:
- Joel Ross
Constructor Summary | |
---|---|
Mixed()
Create a new Mixed number with a form of 0 and 0/1 |
|
Mixed(int whole,
int numerator,
int denominator)
Create a new Mixed number of the form whole + numerator/denominator. |
Method Summary | |
---|---|
Mixed |
add(Mixed m)
Adds another Mixed m and returns a new Mixed as the result. |
Mixed |
divide(Mixed m)
Divides by another Mixed m and returns a new Mixed as the result. |
int |
getDenominator()
Returns the denominator of the Mixed. |
int |
getNumerator()
Returns the numerator of the Mixed. |
int |
getWhole()
Returns the whole number part of the Mixed. |
Mixed |
multiply(Mixed m)
Multiplies by another Mixed m and returns a new Mixed as the result. |
void |
setDenominator(int d)
Sets the denominator of the Mixed to the given value d. |
void |
setNumerator(int n)
Sets the numerator of the Mixed to the given value n. |
void |
setWhole(int w)
Sets the whole number part of the Mixed to the given value w |
Mixed |
subtract(Mixed m)
Subtracts another Mixed m and returns a new Mixed as the result. |
Rational |
toRational()
Returns a Rational number form of the Mixed |
String |
toString()
Returns a String representation of the Mixed |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |