Advertisement
incomplete

Fraction

Apr 15th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. public class Fraction {
  2.  
  3.     private int numerator;
  4.     private int denominator;
  5.  
  6.     public Fraction(){
  7.         this.numerator = 0;
  8.         this.denominator = 1;
  9.     }
  10.    
  11.     /****************************************************/
  12.     /* the ctor takes in the numerator and denominator  */
  13.     /* then sets them to the numerator and denominator  */
  14.     /* of this class and runs them through the gcd      */
  15.     /* then reduces the fraction, and sets the reduced  */
  16.     /* numerator and denominator to the numerator and   */
  17.     /* denominator of this class to be used by          */
  18.     /* FractionCounter                                  */
  19.     /****************************************************/
  20.     public Fraction(int numerator, int denominator){   
  21.         int [] fractionHolder = new int[2];
  22.         fractionHolder = reduce(numerator,denominator);
  23.         this.numerator = fractionHolder[0];
  24.         this.denominator = fractionHolder[1];
  25.     }
  26.    
  27.     //the gcd forumla
  28.     static int gcd(int numerator, int denominator)
  29.     {
  30.       while(numerator!=0 && denominator!=0)
  31.       {
  32.          int c = denominator;
  33.          denominator = numerator % denominator;
  34.          numerator = c;
  35.       }
  36.       return numerator+denominator;
  37.     }
  38.    
  39.     /****************************************************/
  40.     /* takes numerator and denominator values from ctor */
  41.     /* finds applies numerator and denominator to gcd   */
  42.     /* */
  43.     /****************************************************/
  44.     public int[] reduce(int numerator, int denominator){
  45.         int gcd = gcd(numerator, denominator);
  46.         int[] reducedFractions = new int[2];
  47.         int reducedNumerator = numerator/gcd;
  48.         int reducedDenominator = denominator/gcd;
  49.         reducedFractions[0] = reducedNumerator;
  50.         reducedFractions[1] = reducedDenominator;
  51.         return reducedFractions;
  52.     }
  53.    
  54.     public boolean equals(Fraction other){
  55.         return (getNumerator() == (other.getNumerator())
  56.                 && getDenominator() == (other.getDenominator()));
  57.     }
  58.  
  59.     public String toString(){
  60.         return this.getNumerator() + "/" + this.getDenominator();
  61.     }
  62.  
  63.     //getters
  64.     public int getNumerator() { return numerator;   }
  65.     public int getDenominator() {   return denominator; }
  66.  
  67.     //setters
  68.     public void setNumerator(int numerator) {   this.numerator = numerator; }
  69.     public void setDenominator(int denominator) {   this.denominator = denominator; }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement