Question: Fraction.Java /** * Code Authors: Dr. Stephen Roehrig * and David G. Cooper * * Date: Feb 19, 2023 * Purpose: An object for holding
Fraction.Java
| /** | |
| * Code Authors: Dr. Stephen Roehrig | |
| * and David G. Cooper | |
| * | |
| * Date: Feb 19, 2023 | |
| * Purpose: An object for holding and adding fractions | |
| */ | |
| public class Fraction { | |
| private int numerator; | |
| private int denominator; | |
| public Fraction() { | |
| this(0,1); | |
| } | |
| public Fraction(int n, int d) { | |
| numerator = n; | |
| denominator = d; | |
| int common = Math.abs(gcd(n,d)); | |
| numerator /= common; | |
| denominator /= common; | |
| } | |
| // greatest common divisor: | |
| public static int gcd(int a, int b) { | |
| if (b == 0) { | |
| return (a); | |
| } else { | |
| return (gcd(b, a % b)); | |
| } | |
| } | |
| public String toString() { | |
| return numerator + "/" + denominator; | |
| } | |
| public String toDecimal() { | |
| return "" + (numerator / (double) denominator); | |
| } | |
| public Fraction add(Fraction f) { | |
| int num = this.numerator * f.denominator + f.numerator * this.denominator; | |
| int denom = this.denominator * f.denominator; | |
| return new Fraction(num,denom); | |
| } | |
| } |
TestFraction.Java
| /** | |
| * Code Authors: Dr. Stephen Roehrig | |
| * and David G. Cooper | |
| * Purpose: Test the Fraction class | |
| */ | |
| public class TestFraction { | |
| public static void main(String[] args) { | |
| Fraction f1 = new Fraction(3, 3); | |
| Fraction f2 = new Fraction(4, 12); | |
| Fraction f3 = new Fraction(5, 10); | |
| Fraction f4 = new Fraction(-6, 8); | |
| System.out.println("f1 = " + f1); | |
| System.out.println("f2 = " + f2); | |
| System.out.println("f3 = " + f3); | |
| System.out.println("f4 = " + f4); | |
| System.out.println("f1 + f2 = " + f1.add(f2)); | |
| System.out.println("f3 + f4 = " + f3.add(f4)); | |
| System.out.println("f2 in decimal is: " + f2.toDecimal()); | |
| } | |
| } |
Give feedback





Starting with a Fraction class that is capable of doing arithmetic with fractions with integer values, develop 3 new classes Fraction, the base class, IntegerFraction, that functions exactly how the current fraction class works, and UnitFraction, that handles arithmetic considering the units involved, if units don't match then the fraction should become a List of Fractions that are added together, but if units match any Fractions on the List so far, then instead of being added to the list, the fraction on the list should be updated to the new value. An outline of the current Fraction class is given below, followed by an outline of the hierarchy of the updated Fractions. Fractions are held in lowest terms, for integer values gcd() is used to help this. When you develop UnitFraction, it should have the same API as the current Fraction class, and it should be able to add Either a UnitFraction or an IntegerFraction to it. 1. toString ( ) of a UnitFraction should return the fraction part followed by the units part of the UnitFraction. For example 2/3 meter per second 2. the units in the numerator should be separated by spaces for the unitString () method 3. each unit in the denominator should be prefixed by per and separated by spaces for the 4. the method should make the numeric part of the fraction a whole number if the denominator is 1, for example the Fraction 4/1 should read as 4. 5. of the UnitFraction should include the decimal value as a double followed by the 6. If there are no units then the numeric part of the Fraction should be returned without a units part. (i.e. should return the empty string when both the unit numerator and the unit denominator are either empty or null. 7. All Fractions should be immutable after construction. The add() method should return a new Fraction that is the same type as the Fraction that is being called. If information would be lost when doing this, then an error message should be printed and the program should be exited either using System. exit (-1) or a RuntimeException. 8. The members(fields, constructors, and methods) of each class listed in the UML diagram of the Target Class Hierarchy are required. 9. you may have additional private fields and methods as support for your classes, however the protected fields must be used by the subclasses either directly or indirectly, and the data that they hold should not be replicated in any other fields of your classes. 10. Except for optionally adding an import statement, the TestUnitFraction program below should not be altered. You should copy it and paste it into a file called TestUnitFraction. java . It needs to be at the base directory of this repository for the auto-grading tests run. 11. You may put your classes in a package, but it is not required. If you do so, make sure to import them so that the TestunitFraction program can find them. 12. The unit strings in the UnitFraction constructor should be comma separated to separate the name of each unit. Unit names should not have spaces in them. There can be multiple units in the same string and they should both become part of the units before symplifying. For instance new UnitFraction (98, 10, "meters", "second, second") would represent 98/10 meters per second per second or 9.8m/s2 esources 1. Fraction.java and TestFraction.java are included as a starting point. Fraction. java is an implemetation of IntegerFraction without inheritance. You'll be modifying it so that it works with the class hierarchy described in this assignment. 2. String indexOf() method or String split() method can help you split strings by commas. 3. Class Reuse Slides. README.md Submission You should submit 4 files: Fraction. java (this should represent the UML from the second Fraction UML diagram), UnitFraction. java , IntegerFraction.java and TestUnitFraction.java The last file should be a direct copy and past of the listing in this README.md file. All files should be submitted to this repository on GitHub for autograding. The TestUnitFraction class must stay in the default package, but the other classes may be put in a package of your choosing as long as TestUnitFraction imports from that package
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
