Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * Benjamin Kim
- * Term 2 Assignment 4 - Fraction Comparable
- *
- * A class which is used to represent fractions and implements
- * the comparable interface
- */
- public class Fraction
- {
- private int numerator;
- private int denominator;
- // Default constructor: creates fraction 1/1
- public Fraction()
- {
- numerator = 1;
- denominator = 1;
- }
- // Constructor for fraction n/d where n, d > 0
- public Fraction(int n, int d)
- {
- this();
- if(n > 0)
- {
- numerator = n;
- }
- if(d > 0)
- {
- denominator = d;
- }
- }
- // Return the fraction as a String e.g. "2/3"
- public String toString()
- {
- return numerator + "/" + denominator;
- }
- // Return the fraction as a mixed number String
- public String mixedNumber()
- {
- // If fraction is proper return fraction using toString method
- if(numerator < denominator)
- {
- return toString();
- }
- // If numerator is a multiple of denominator return just integer part
- if (numerator % denominator == 0)
- {
- return "" + numerator/denominator;
- }
- // Otherwise return mixed number string
- return numerator/denominator + " " + numerator%denominator + "/" + denominator;
- }
- // Adds the fraction n/d to this fraction if n and d are both greater than 0
- public void add(int n, int d)
- {
- if(n > 0 && d > 0)
- {
- numerator = numerator * d + n * denominator;
- denominator *= d;
- }
- }
- public int compareTo(Object other)
- {
- //casting
- Fraction numFract= (Fraction) other;
- //if statements to test and return the bigger or smaller fraction
- if(numerator*numFract.denominator < numFract.numerator*denominator)
- {
- return -1;
- }
- else if(numerator*numFract.denominator > numFract.numerator*denominator)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- static int gcd(int a, int b)
- {
- //if statements for testing the bigger number
- if(a>b)
- {
- //for loops to test every number
- for(int i=0; i<= b;i++)
- {
- //if statements to test the limits
- if(b%a==0)
- {
- return i;
- }
- }
- }
- else
- {
- for(int i=0; i<= a; i++)
- {
- if(a%b==0)
- {
- return i;
- }
- }
- }
- return 1;
- }
- void simplify()
- {
- gcd(numerator,denominator);
- numerator = numerator/gcd(numerator,denominator);
- denominator = denominator/gcd(numerator,denominator);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement