binibiningtinamoran

Fraction

Nov 6th, 2019
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. /* Term 2 Assignment 1 - Fraction */
  2. /* A class which is used to represent fractions */
  3. public class Fraction {
  4.     private int numerator;
  5.     private int denominator;
  6.  
  7.     private static final int NUMERATOR_VAL = 1;
  8.     private static final int DENOMINATOR_VAL = 1;
  9.  
  10.     // default constructor which creates a fraction 1/1
  11.     public Fraction() {
  12.         this.numerator = NUMERATOR_VAL;
  13.         this.denominator = DENOMINATOR_VAL;
  14.     }
  15.  
  16.     // If n is positive, set numerator to n. Otherwise, set numerator to 1.
  17.     // If d is positive, set denominator to d. Otherwise, set denominator to 1.
  18.     public Fraction(int n, int d) {
  19.         this.numerator = (n < 0) ? NUMERATOR_VAL : n;
  20.         this.denominator = (d < 0) ? DENOMINATOR_VAL : d;
  21.  
  22.         // OR USE BELOW!
  23.         /*
  24.         if (n < 0) {
  25.             this.numerator = NUMERATOR_VAL;
  26.         } else {
  27.             this.numerator = n;
  28.         }
  29.  
  30.         if (d < 0) {
  31.             this.denominator = DENOMINATOR_VAL;
  32.         } else {
  33.             this.denominator = d;
  34.         }
  35.  
  36.          */
  37.     }
  38.  
  39.     // Returns the fraction as a string in the format “numerator/denominator”
  40.     public String toString() {
  41.         return numerator + "/" + denominator;
  42.     }
  43.  
  44.     // Returns any improper (top-heavy) fraction as a mixed number, for example, 2 3/5.
  45.     // If the numerator of the fraction part is 0, return only the integer part of the mixed number.
  46.     // If the fraction is proper, return only the fraction part.
  47.     public String mixedNumber() {
  48.         int whole = (this.numerator / this.denominator);
  49.         int mixedNumerator = (numerator % denominator);
  50.         if (mixedNumerator == 0) {
  51.             return String.valueOf(whole);
  52.         } else if (this.numerator < this.denominator) {
  53.             return this.numerator + "/" + this.denominator;
  54.         } else {
  55.             return whole + " " + mixedNumerator + "/" + this.denominator;
  56.         }
  57.     }
  58.  
  59.     // If n and d are both positive, add the fraction n/d to this fraction.
  60.     // Otherwise, leave the fractions unchanged.
  61.     // In general the sum of the fractions a/b and c/d is(a*d + c*b)/(b*d).
  62.     public Fraction add(int n, int d) {
  63.         Fraction sum = this;
  64.  
  65.         if (n > 0 && d > 0) {
  66.             int newNumerator = ((this.numerator * d) + (n * this.denominator));
  67.             int newDenominator = (this.denominator * d);
  68.             sum = new Fraction(newNumerator, newDenominator);
  69.         }
  70.         return sum;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment