Guest User

Untitled

a guest
Feb 26th, 2020
215
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *
  3.  * Benjamin Kim
  4.  * Term 2 Assignment 4 - Fraction Comparable
  5.  *
  6.  * A class which is used to represent fractions and implements
  7.  * the comparable interface
  8.  */
  9. public class Fraction
  10. {
  11.   private int numerator;
  12.   private int denominator;
  13.  
  14.   // Default constructor: creates fraction 1/1
  15.   public Fraction()
  16.   {
  17.     numerator = 1;
  18.     denominator = 1;
  19.   }
  20.  
  21.   // Constructor for fraction n/d where n, d > 0
  22.   public Fraction(int n, int d)
  23.   {
  24.     this();
  25.     if(n > 0)
  26.     {
  27.       numerator = n;
  28.     }
  29.     if(d > 0)
  30.     {
  31.       denominator = d;
  32.     }
  33.   }
  34.  
  35.   // Return the fraction as a String e.g. "2/3"
  36.   public String toString()
  37.   {
  38.     return numerator + "/" + denominator;
  39.   }
  40.  
  41.   // Return the fraction as a mixed number String
  42.   public String mixedNumber()
  43.   {
  44.     // If fraction is proper return fraction using toString method
  45.     if(numerator < denominator)
  46.     {
  47.       return toString();
  48.     }
  49.    
  50.     // If numerator is a multiple of denominator return just integer part
  51.     if (numerator % denominator == 0)
  52.     {
  53.       return "" + numerator/denominator;
  54.     }
  55.    
  56.     // Otherwise return mixed number string
  57.     return numerator/denominator + " " + numerator%denominator + "/" + denominator;
  58.   }
  59.  
  60.   // Adds the fraction n/d to this fraction if n and d are both greater than 0
  61.   public void add(int n, int d)
  62.   {
  63.     if(n > 0 && d > 0)
  64.     {
  65.       numerator = numerator * d + n * denominator;
  66.       denominator *= d;
  67.     }
  68.   }
  69.   public int compareTo(Object other)
  70.   {
  71.     //casting
  72.     Fraction numFract= (Fraction) other;
  73.     //if statements to test and return the bigger or smaller fraction
  74.     if(numerator*numFract.denominator < numFract.numerator*denominator)
  75.     {
  76.       return -1;
  77.     }
  78.     else if(numerator*numFract.denominator > numFract.numerator*denominator)
  79.     {
  80.       return 1;
  81.     }
  82.     else
  83.     {
  84.       return 0;
  85.     }
  86.   }
  87.   static int gcd(int a, int b)
  88.   {
  89.     //if statements for testing the bigger number
  90.     if(a>b)
  91.     {
  92.       //for loops to test every number
  93.       for(int i=0; i<= b;i++)
  94.       {
  95.         //if statements to test the limits
  96.         if(b%a==0)
  97.         {
  98.           return i;
  99.         }
  100.       }
  101.     }
  102.     else
  103.     {
  104.       for(int i=0; i<= a; i++)
  105.       {
  106.         if(a%b==0)
  107.         {
  108.           return i;
  109.         }
  110.       }
  111.     }
  112.     return 1;
  113.   }
  114.   void simplify()
  115.   {
  116.     gcd(numerator,denominator);
  117.     numerator = numerator/gcd(numerator,denominator);
  118.     denominator = denominator/gcd(numerator,denominator);
  119.   }
  120. }
RAW Paste Data