Java Fractions as Abstract Data Type, programming homework help
Java Fractions as Abstract Data Type, programming homework help
This assignment provides you the opportunity to work with fractions as a new abstract data type!
You should be able to help third and fourth graders with their homework when you complete this exercise!
What to hand in:
Please hand in code listings and output that are legible and formatted for ease of reading.
Canvas submission instructions:
Please combine multiple files into a single “zip” archive, and save it in a location that you will remember. When you are ready to submit the assignment, open the assignment you are submitting a solution for, and attach your file.
Problems:
- Derive a new kind of Number called Fraction. Give it appropriate methods to add, subtract, multiply and divide fractions. Also, provide the required methods for a Number (to be sortable, it should also implement Comparable<Fraction>).
- Determine some appropriate way to convert a Fraction to a String, and implement the appropriate method. Simply outputting the double equivalent is not a satisfactory answer.
- You must provide a method (which could be private) to simplify your Fraction into lowest terms. That method could be used after every operation changing the Fraction, and certainly is required to provide a reasonable comparison operation.
- Make sure to implement equals so you have a way to compare Fractions.
- Create a main method or test class with a main method with adequate number of test cases to demonstrate that your Fraction behaves appropriately.
Notes:
- A fraction has a numerator and a denominator (the part above the line and the part below the line).
- The denominator may not be zero (0).
- If the denominator is one (1), the Fraction represents the integer in the numerator, and should be converted to a String just like an integer.
- Remember the rules for addition, subtraction, multiplication, and division of fractions:
- To simplify a Fraction, one divides the numerator and denominator by the greatest common divisor, which is the largest number that divides each of them evenly.
- If you are not using BigInteger, you can use the following gcd method. It can be modified to use long instead of int:/* As a static method using int */public static int greatestCommonDivisor (int int1, int int2){ int temp; if ( int1 < 0 ) int1 = -int1; if ( int2 < 0 ) int2 = -int2; while ( int2 != 0) { temp = int1 % int2; // % is the modulus operator int1 = int2; int2 = temp; } return int1;}
- Additionally, one should ensure the denominator is positive. If you are using BigInteger to represent the components, you compare to zero with compareTo and use negate to reverse the sign.
- Once you simplify, you can compare Fractions by manipulating denominators and numerators. Hint: For compareTo you can subtract them and look at the result.
- If you use BigInteger to represent these components, you will get a gcd method to go along with it!
- BigInteger also provides a satisfactory set of name expectations for the operations one might expect on a Fraction. These are worth consulting!
- To create fractions, it is sensible to have a String constructor. A sample constructor is provided below (this one uses the BigInteger data members; a somewhat simpler, but similar one would work with long data members):
public Fraction (String fractionString) throws NumberFormatException {
String[] parts = fractionString.split(“/”);
denominator = BigInteger.ONE;
numerator = BigInteger.ZERO;
// The BigInteger constructor may throw NumberFormatExceptions
if ( parts.length == 0 || parts.length > 2 ) {
throw new NumberFormatException(“Illegally formatted Fraction”);
}
numerator = new BigInteger(parts[0].trim());
if ( parts.length == 2 ) {
BigInteger d = new BigInteger(parts[1].trim());
if ( d.equals(BigInteger.ZERO))
throw new NumberFormatException(“Fraction may not have zero denominator”);
denominator = d;
}
simplify();
}
- Remember: Delegate, delegate, delegate!
Evaluation Criteria:
|
Criteria |
Weight |
|---|---|
|
Creation of the Fraction type and its components, including a String constructor. |
20 |
|
Add, Subtract, Multiply, and Divide operations |
20 |
|
All required Number methods |
20 |
|
simplify and toString methods |
10 |
|
Method for comparison of Fractions (equals, compareTo) |
10 |
|
Main program with an adequate number of test cases to demonstrate the above |
20 |
"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.
Discount Code: CIPD30
Click ORDER NOW..


