/* Fraction.java ** An instance of this class represents a common (or simple) fraction. ** ** Authors: P.M.J., R.W.M., and < Student's Name > ** Course: CMPS 134 ** Semester: Fall 2025 ** ** Collaborated with: ** Known defects: */ public class Fraction { // symbolic constant // ----------------- private final String SLASH = "/"; // instance variables // ------------------ private int numerator, denominator; // Class invariants: // -- denominator > 0 // -- GCD(numerator,denominator) == 1 // (and thereby the fraction as stored here is in simplest form) // constructors // ------------ /* Initializes this Fraction to be as described by the given String. ** pre: The given String is in the form n/d, where each of n and d ** is a decimal integer numeral (meaning a sequence of decimal ** digit characters (in the range '0'..'9') optionally preceded ** by a minus sign (i.e., hyphen)). ** Examples: "13/25", "-4/17", "16/-2", "-3/-7" */ public Fraction(String fraction) { int numer = parseOutNumerator(fraction); int denom = parseOutDenominator(fraction); setTo(numer, denom); } /* Initializes this Fraction to be equal to numerator/denominator ** (the ratio between the two arguments). If denominator == 0, ** an exception is thrown. */ public Fraction(int numerator, int denominator) { // Stubbed this.numerator = 0; this.denominator = 1; } // observers // --------- /* Returns this fraction's numerator. */ public int getNumerator() { return numerator; } /* Returns this fraction's denominator. */ public int getDenominator() { return denominator; } // mutators // -------- /* Increases this fraction by the value of the given fraction. ** Uses fact that a/b + c/d = (ad + cb) / bd. */ public void addTo(Fraction addend) { // Stubbed } /* Reduces this fraction by the value of the given fraction. ** Uses fact that a/b - c/d = (ad - cb) / bd. */ public void subtractFrom(Fraction subtrahend) { // Stubbed } /* Multiplies this fraction by the value of the given fraction. ** Uses fact that a/b * c/d = ac/bd. */ public void multiplyBy(Fraction multiplier) { // Stubbed } /* Divides this fraction by the value of the given fraction. ** Uses fact that a/b / c/d = ad/bc. */ public void divideBy(Fraction divisor) { // Stubbed } // conventional/standard // --------------------- /* Returns true if and only if this fraction is equivalent ** to the given object (meaning that it is also a Fraction ** and represents the same rational number). */ @Override public boolean equals(Object obj) { boolean result = true; if (obj instanceof Fraction) { Fraction f = (Fraction)obj; // Stubbed } else { result = false; // obj isn't even a Fraction } return result; } /* Returns a string representaion of this fraction in the form n/d */ public String toString() { return "" + this.numerator + SLASH + this.denominator; } // private methods // --------------- /* Makes this Fraction equal to numer/denom (the ratio between the ** two arguments). However, if denom == 0, an exception is thrown ** without any change having been made to this Fraction. ** Note that this method should be the only one in which assignments ** are made to the instance variables. */ private void setTo(int numer, int denom) { if (denom == 0) { throw new IllegalArgumentException("Zero denominator not allowed!"); } else { this.numerator = numer; this.denominator = denom; simplify(); } } /* Given a String of the form n/d, where n and d are integer numerals, ** returns the integer represented by n. If there is no slash, or a ** slash is the first character in the given String, zero is returned. ** Examples: "13/25" yields 13; "12/" yields 12; "/47" yields 0. */ private int parseOutNumerator(String fraction) { int result = 0; // Stubbed return result; } /* Given a String of the form n/d, where n and d are integer numerals, ** returns the integer represented by d. If there is no slash, or a ** slash is the last character in the given String, zero is returned. ** Examples: "13/25" yields 13; "12/" yields 0; "/47" yields 47. */ private int parseOutDenominator(String fraction) { int result = 1; // Stubbed return result; } /* Method that (potentially) modifies the values of the instance variables ** ('numerator' and 'denominator') for the purpose of ensuring that the ** class invariants are satisfied. More specifically, the class ** invariants dictate that ** ** - the value of denominator must be positive (i.e., greater than zero) ** - the fraction is in "simplest terms", meaning that the GCD of ** numerator and denominator is 1. (To achieve this, it suffices ** to divide each one by the GCD of their absolute values.) ** ** Note that what this method does is often referred to as "reducing" ** a fraction, but that is a misnomer because it does not change the ** fraction's value. Rather, it (potentially) reduces the (absolute) ** values of its numerator and denominator while keeping the ratio ** between them the same. */ private void simplify() { int gcd = GCD(Math.abs(numerator), Math.abs(denominator)); // Stubbed } /* Functional method that returns the GCD (Greatest Common Divisor) ** of the given nonnegative integers. Note that this method employs ** the Euclidean algorithm (remainder version) and is implemented ** using recursion (i.e., the body of the method includes a call to ** itself). ** pre: j >= 0 && k >= 0 */ private int GCD(int j, int k) { int result; if(k == 0) { result = j; } else { result = GCD(k,(j % k)); } return result; } }