Skip to content

Instantly share code, notes, and snippets.

@ezragol
Created December 11, 2022 20:02
Show Gist options
  • Save ezragol/5fc550bbc551e1b73cc781a5cf91d26e to your computer and use it in GitHub Desktop.
Save ezragol/5fc550bbc551e1b73cc781a5cf91d26e to your computer and use it in GitHub Desktop.
fraction codecheck v2
/*
* 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 <24goldnere@brooklinek12.org>
*/
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) {
num = other.num;
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));
}
// Returns the opposite of this fraction
public Fraction negate() {
return multiply(-1);
}
// Returns the difference of this fraction and other
public Fraction subtract(Fraction other) {
return add(other.negate());
}
// Returns the difference of this fraction and m
public Fraction subtract(int m) {
return add(m * -1);
}
// Returns the reciprocal of this fraction
public Fraction reciprocal() {
return new Fraction(den, num);
}
// Returns the quotient of this fraction and other
public Fraction divide(Fraction other) {
return multiply(other.reciprocal());
}
// Returns the quotient of this fraction and m
public Fraction divide(int m) {
return multiply(new Fraction(1, m));
}
// Return a negative integer, zero, or a positive integer as this Fraction is <, ==, > than the other Fraction.
public int compareTo(Fraction other) {
Fraction subtracted = subtract(other);
if (subtracted.num < 0)
return -1;
else if (subtracted.num > 0)
return 1;
return 0;
}
// Return true if this equals that
public boolean equals(Fraction that) {
return that.num == num && that.den == den;
}
// http://www.technofundo.com/tech/java/equalhash.html
// Returns true if this has the same value as other, otherwise false
@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (other.getClass() != this.getClass()) return false;
Fraction that = (Fraction) other;
return equals(that);
}
// 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