Fraction Matcher Overview Read in a list of Fraction objects, prompt the user for floating-point numbers, and print the closest Fraction matches. Fraction Class Use
Overview
Read in a list of Fraction objects, prompt the user for floating-point numbers, and print the closest Fraction matches.
Fraction Class
Use the code from yourprevious programsas a starting point:FractionReader.javaandFraction.java.
Interface
Have Fraction implement theComparableinterface. You will need to implementcompareTo.
compareTo
Returns a negative value if this Fraction'sfloating-point equivalentis smaller than the parameter's, zero if they are equal, and a positive value if this is larger.compareToreturns aninteger.
Constructor
The Fraction constructor should accept 2 integers: numerator and denominator. Initialize your instance variables with those values.
toString
Return a String representingnumerator/denominator.
Additional Methods
Create any other methods you need.
FractionMatcher Class
Themainmethod should be in theFractionMatcherclass. Create any additional methods you wish.
- Read in a list of fractions from the filefractions.txt.
- Sort the Fraction objects (UsingCollections).
- Prompt the user for floating-point values (until zerois entered).
- For each value, print theFractionthat is theclosestto the value and its decimal equivalent in parentheses.
Finding the Closest
I recommend you create a constantTINY = .000001to assist with floating-point comparison. There are 3 main cases:
- The providedvalue is less than or equal tothefirstFraction (value + TINY =>returnthefirstFraction.
- The providedvalue is greater than or equal tothelastFraction (value - TINY >= last fraction in the list):returnthelastFraction.
- The providedvalueisbetween2 values. Find the 2 fractions the value is between, and return the fraction with thesmaller difference. (absolute value of (input - first fraction's value)compared toabsolute value of (input - second fraction's value)).
Fraction.txt
1/15
13/16
7/16
15/16
4/13
7/10
1/12
7/11
2/5
3/13
3/8
2/15
13/15
12/13
4/5
11/13
3/7
2/11
1/2
3/4
5/7
3/14
8/9
5/12
3/16
3/5
5/8
8/15
8/11
9/13
4/9
1/4
1/7
3/10
11/12
9/10
7/13
1/8
5/9
7/15
1/9
4/15
3/11
5/13
1/3
1/16
11/14
2/9
1/6
9/11
7/8
8/13
5/11
7/9
1/5
14/15
5/16
11/15
6/11
9/14
7/12
1/11
1/10
10/13
5/14
2/13
4/11
6/13
9/16
6/7
1/1
4/7
2/7
13/14
1/14
5/6
10/11
1/13
2/3
11/16
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started