SHARE
TWEET

Untitled

a guest Nov 8th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package pack;
  2. public class Fraction implements Comparable<Fraction> {
  3.     private int numerator, denom;
  4.  
  5.     public Fraction() {
  6.         numerator = 0;
  7.         denom = 1;
  8.     }
  9.  
  10.     public Fraction(int num) {
  11.         numerator = num;
  12.         denom = 1;
  13.     }
  14.  
  15.     public Fraction(int numerator, int denom) {
  16.         this.numerator = numerator;
  17.         this.denom = denom;
  18.         reduce();
  19.     }
  20.  
  21.     private void reduce() {
  22.         int new_demoniator = denom;
  23.         boolean bool = false;
  24.         if (denom < 0) {
  25.             new_demoniator = denom * -1;
  26.             bool = true;
  27.         }
  28.         int gcf = gcf(numerator, new_demoniator);
  29.         numerator = numerator / gcf;
  30.         denom = new_demoniator / gcf;
  31.         if (bool)
  32.             denom *= -1;
  33.     }
  34.  
  35.     private int gcf(int a, int b) {
  36.         int gcf = 1;
  37.         for (int i = 2; i <= Math.min(a, b); i++) {
  38.             if (numerator % i == 0 && denom % i == 0)
  39.                 gcf = i;
  40.         }
  41.         return gcf;
  42.     }
  43.  
  44.     public Fraction add(Fraction f) {
  45.         int commondenom = f.denom * denom;
  46.         int frac_nume = (commondenom / f.denom) * f.numerator;
  47.         int frac_denom = commondenom;
  48.         int replaced_num = (commondenom / denom) * numerator;
  49.         replaced_num += frac_nume;
  50.  
  51.         return new Fraction(replaced_num, commondenom);
  52.     }
  53.  
  54.     public Fraction subtract(Fraction f) {
  55.  
  56.         int commondenom = f.denom * denom;
  57.         int frac_nume = (commondenom / f.denom) * f.numerator;
  58.         int frac_denom = commondenom;
  59.         int replaced_num = (commondenom / denom) * numerator;
  60.  
  61.         replaced_num -= frac_nume;
  62.         if (replaced_num < 0) {
  63.             commondenom *= -1;
  64.             replaced_num *= -1;
  65.         }
  66.  
  67.         return new Fraction(replaced_num, commondenom);
  68.     }
  69.  
  70.     public Fraction multiply(Fraction f) {
  71.         int frac_nume = f.numerator * numerator;
  72.         int frac_denom = f.denom * denom;
  73.         return new Fraction(frac_nume, frac_denom);
  74.     }
  75.  
  76.     public Fraction divide(Fraction f) {
  77.         int temporary = f.numerator;
  78.         ;
  79.         int replaced_num = f.denom;
  80.         int new_demoniator = temporary;
  81.  
  82.         replaced_num *= numerator;
  83.         new_demoniator *= denom;
  84.  
  85.         return new Fraction(replaced_num, new_demoniator);
  86.     }
  87.  
  88.     public double toDecimal() {
  89.         return (numerator + 0.0) / denom;
  90.     }
  91.  
  92.     public int compareTo(Fraction f) {
  93.         double decimal_one = numerator / (denom + 0.0);
  94.         double decimal_two = f.numerator / (f.denom + 0.0);
  95.         if (decimal_one > decimal_two)
  96.             return 1;
  97.         else if (decimal_one == decimal_two)
  98.             return 0;
  99.         else
  100.             return -1;
  101.     }
  102.  
  103.     public boolean equals(Fraction f) {
  104.         double decimal_one = numerator / (denom + 0.0);
  105.         double decimal_two = f.numerator / (f.denom + 0.0);
  106.         return decimal_one == decimal_two;
  107.     }
  108.  
  109.     public String toString() {
  110.         if (denom == 1)
  111.             return numerator + ".0";
  112.         else
  113.             return numerator + "/" + denom;
  114.     }
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top