Created
December 11, 2022 20:01
-
-
Save ezragol/2cab223d6d8c210599a8f35b39d6f6f1 to your computer and use it in GitHub Desktop.
codecheck fraction v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* The Fraction class from http://skylit.com/javamethods3/studentfiles.zip | |
* where the following are also implemented: | |
* - all methods of FractionI; | |
* | |
* JM3e Chapter 10.3 - Author: Alex | |
* @author EZRA GOLDNER <[email protected]> | |
*/ | |
public class Fraction implements FractionI /*, Comparable<Fraction> */ | |
{ | |
/////////////////////////////// FIELDS /////////////////////////////// | |
private int num; | |
private int den; | |
//////////////////////////// CONSTRUCTORS //////////////////////////// | |
public Fraction() { // no-args constructor | |
num = 0; | |
den = 1; | |
} | |
public Fraction(int n) { | |
num = n; | |
den = 1; | |
} | |
public Fraction(int num, int den) { | |
if (den == 0) | |
throw new IllegalArgumentException( | |
"Fraction construction error: denominator is 0"); | |
// Otherwise... initialize fields and reduce to canonical form | |
this.num = num; | |
this.den = den; | |
this.reduce(); | |
} | |
// Copy constructor | |
public Fraction(Fraction other) { | |
this.num = other.num; | |
this.den = other.den; | |
} | |
////////////////////////////// METHODS /////////////////////////////// | |
// Accessor methods | |
public int getNumerator() { return num; } | |
public int getDenominator() { return den; } | |
// Returns the value of this fraction as a double | |
public double doubleValue() { | |
return (double) num / (double) den; | |
} | |
// Returns a string representation of this fraction | |
@Override | |
public String toString() { | |
return num + "/" + den; | |
} | |
// Returns the sum of this fraction and other | |
public Fraction add(Fraction other) { | |
int common = gcd(this.den, other.den); | |
int thisDivided = this.den / common; | |
int thatDivided = other.den / common; | |
return new Fraction(this.num * thatDivided + other.num * thisDivided, this.den * thatDivided); | |
} | |
// Returns the sum of this fraction and m | |
public Fraction add(int m) { | |
return add(new Fraction(m)); | |
} | |
// Returns the product of this fraction and other | |
public Fraction multiply(Fraction other) { | |
int aDcommon = gcd(this.num, other.den); | |
int bCcommon = gcd(this.den, other.num); | |
int thisNewNum = this.num / aDcommon; | |
int thatNewDen = other.den / aDcommon; | |
int thisNewDen = this.den / bCcommon; | |
int thatNewNum = other.num / bCcommon; | |
return new Fraction(thisNewNum * thatNewNum, thisNewDen * thatNewDen); | |
} | |
// Returns the product of this fraction and m | |
public Fraction multiply(int m) { | |
// return new Fraction(num * m, den); | |
return multiply(new Fraction(m)); | |
} | |
// Return a hashcode for this fraction | |
@Override | |
public int hashCode() { | |
int hash = 17; | |
hash = 31 * hash + num; | |
hash = 31 * hash + den; | |
return hash; | |
} | |
////////////////////////// PRIVATE METHODS /////////////////////////// | |
// Reduce this fraction to canonical form: gcd(num, den) == 1 and den > 0 | |
private void reduce() { | |
if (num == 0) { | |
den = 1; | |
return; | |
} | |
if (den < 0) { | |
num = -num; | |
den = -den; | |
} | |
int q = gcd(num, den); | |
num /= q; | |
den /= q; | |
} | |
// Returns the greatest common divisor of two integers | |
private static int gcd(int n, int d) { | |
if (d == 0) return Math.abs(n); | |
return gcd(d, n % d); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment