Guest User

Untitled

a guest
Apr 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. package fractions;
  2.  
  3. import java.lang.ArithmeticException;
  4.  
  5. public class Fraction {
  6.     private final int numerator;
  7.     private final int denominator;
  8.    
  9.     public Fraction (int n, int d) throws ArithmeticException {
  10.         if (d == 0) throw new ArithmeticException();
  11.        
  12.         if (n == 0) {
  13.             numerator = 0;
  14.             denominator = 1;
  15.            
  16.             return;
  17.         }
  18.        
  19.         if (d < 0) {
  20.             d *= -1;
  21.             n *= -1;
  22.         }
  23.        
  24.         int gcd = gcd (n, d);
  25.        
  26.         if (gcd != 0) {
  27.             d = d / gcd;
  28.             n = n / gcd;
  29.         }
  30.        
  31.         numerator = n;
  32.         denominator = d;
  33.     }
  34.    
  35.     public static int gcd (int p, int q) {
  36.         if (q == 0) return p;
  37.        
  38.         return gcd (q, p % q);
  39.     }
  40.    
  41.     public int getNumerator () {
  42.         return numerator;
  43.     }
  44.    
  45.     public int getDenominator () {
  46.         return denominator;
  47.     }
  48.    
  49.     public boolean equals (Object obj) {
  50.         if (!(obj instanceof Fraction)) return false;
  51.        
  52.         return (((Fraction) obj).getNumerator() == this.getNumerator() && ((Fraction) obj).getDenominator() == this.getDenominator());
  53.     }
  54.  
  55.     public Fraction add (Fraction f) throws ArithmeticException {
  56.         if (f == null) throw new NullPointerException ();
  57.        
  58.         int normalisedThisN, normalisedThisD, normalisedFN, normalisedFD;
  59.        
  60.         // normalise the two denominators, unless they're the same
  61.         if (this.getDenominator() != f.getDenominator()) {
  62.             // how many more times can this.d go into max_int?
  63.             int d_times = (int) (Integer.MAX_VALUE / this.getDenominator());
  64.            
  65.             // are we going to multiply by more than that?
  66.             if (d_times < f.getDenominator()) throw new ArithmeticException ("Unable to normalise 1");
  67.            
  68.             // how many times can this.n go into max_int?
  69.             int n_times = (int) (Integer.MAX_VALUE / this.getNumerator());
  70.             if (n_times < f.getDenominator()) throw new ArithmeticException ("Unable to normalise 2");
  71.            
  72.             // how many times can f.d go into max_int?
  73.             int fd_times = (int) (Integer.MAX_VALUE / f.getDenominator());
  74.             if (fd_times < this.getDenominator()) throw new ArithmeticException ("Unable to normalise 3");
  75.            
  76.             // how many times can f.n go into max_int?
  77.             int fn_times = (int) (Integer.MAX_VALUE / f.getDenominator());
  78.             if (fn_times < this.getDenominator()) throw new ArithmeticException ("Unable to normalise 4");
  79.            
  80.             normalisedThisN = this.getNumerator()*f.getDenominator();
  81.             normalisedThisD = this.getDenominator()*f.getDenominator();
  82.            
  83.             normalisedFN = f.getNumerator()*this.getDenominator();
  84.             normalisedFD = f.getDenominator()*this.getDenominator();
  85.         } else {
  86.             normalisedThisN = this.getNumerator();
  87.             normalisedThisD = this.getDenominator();
  88.             normalisedFN = f.getNumerator();
  89.             normalisedFD = f.getDenominator();
  90.         }
  91.        
  92.         // add them up.
  93.         Fraction addedFraction;
  94.        
  95.         // will we be adding, or subtracting?
  96.         if (normalisedThisN > 0) {
  97.             // what's the most we can add to the n of this?
  98.             int highest_n = Integer.MAX_VALUE - normalisedThisN;
  99.            
  100.             if (highest_n < normalisedFN) throw new ArithmeticException ("Numerator too big to be stored");
  101.         } else {
  102.             int lowest_n = Integer.MIN_VALUE + normalisedThisN;
  103.            
  104.             if (lowest_n > normalisedFN) throw new ArithmeticException ("Numerator too small to be stored");
  105.         }
  106.        
  107.         // highest we can add to this.d?
  108.         int highest_d = Integer.MAX_VALUE - normalisedThisD;
  109.        
  110.         if (highest_d < normalisedFD) throw new ArithmeticException ("Too big to store when added");
  111.        
  112.         addedFraction = new Fraction (normalisedFN + normalisedThisN, normalisedFD);
  113.        
  114.         return addedFraction;
  115.     }
  116. }
Add Comment
Please, Sign In to add comment