Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.11 KB | None | 0 0
  1. /*
  2.  * PROJECT 0
  3.  *
  4.  * This file is a SKELETON file. That means that a number of functions have
  5.  * not been implemented. Your task is to complete the class by filling in the
  6.  * missing methods.
  7.  *
  8.  * Tasks:
  9.  *
  10.  * 1) First an important note. This file contains names of public methods
  11.  *    which you should NOT change. Each method is already "preprogrammed" so
  12.  *    the interface (the functions, their parameters and what should be
  13.  *    returned) is fixed and you should NOT change it. Every function has a
  14.  *    description, which outlines:
  15.  *
  16.  *    - The purpose of the function, and intended use;
  17.  *    - The parameters it accepts (if any) indicated by @param;
  18.  *    - What it returns (if anything) indicated by @return.
  19.  *
  20.  * 2) CAREFULLY read through the class definition.
  21.  *
  22.  * 3) Define the methods that are not implemented. They are indicated by a
  23.  *    comment reading "Fill in this method", but for reference, they are:
  24.  *
  25.  *    - The Fraction(m,n) constructor.
  26.  *    - Convertors: toDouble(), toString()
  27.  *    - Operations with fractions: add(), divide() and multiply()
  28.  *
  29.  * 4) Add to the class two methods, which are private and accept no
  30.  * parameters:
  31.  *      
  32.  *    - gcd():    computes and returns the greatest common divisor of the
  33.  *                numerator and denominator for this fraction.
  34.  *    - reduce(): uses gcd() to reduce the fraction to irreducible form. e.g.
  35.  *                a fraction 10/20 is simplified to 1/2.
  36.  *
  37.  * 5) The BOSS system will perform specific tests with your class. It will
  38.  *    test: constructor, operators and simplification to irreducible form. So
  39.  *    even if you do not complete all of the methods you will obtain some
  40.  *    credit if others are completed satisfactorily.
  41.  *
  42.  * 6) Finally, fill in the following fields:
  43.  *
  44.  * NAME:
  45.  * UNIVERSITY ID:
  46.  * DEPARTMENT:
  47.  */
  48.  
  49. /**
  50.  * Classname: Fraction
  51.  * Description: This class implements a new type for fractions
  52.  *              and corresponding arithmetic.
  53.  *
  54.  * @author : Original: K.N. King, modified by D. Moxey and P. Plechac
  55.  *           for use in the course MA117
  56.  * @version: history: v1.1
  57.  */
  58.  
  59. public class Fraction {
  60.     // ============================================================
  61.     // Instance variables
  62.     // ============================================================
  63.  
  64.     /**
  65.      * The numerator of the fraction.
  66.      */
  67.     private int numerator;
  68.     /**
  69.      * The denominator of the fraction.
  70.      */
  71.     private int denominator;
  72.  
  73. // Methods:
  74. // GCD calculator
  75.     private int gcd() {
  76.         int a, b, hcf;
  77.         a = b = hcf= 1;
  78.         //Renames variables so that a > b
  79.         if (this.numerator > this.denominator) {
  80.             a = this.numerator;
  81.             b = this.denominator;
  82.         }
  83.         else {
  84.             b = this.numerator;
  85.             a = this.denominator;      
  86.         }
  87.         int i;
  88.         for (i = b; i > 0;i--) {
  89.             //finds the remainder on division by i of a, b.
  90.             int remainderbOveri = b%i;
  91.             int remainderaOveri = a%i;
  92.             //If i divides both b and a, then sets hcf to i and stops the loop.
  93.             if(remainderbOveri == 0 && remainderaOveri == 0){
  94.                 hcf = i;
  95.                 break;
  96.             }
  97.         }
  98.         return hcf;
  99.     }
  100.  
  101.  //Reducer
  102.     public void reduce() {
  103.         int gcd = this.gcd();
  104.         this.numerator = this.numerator/gcd;
  105.         this.denominator = this.denominator/gcd;
  106.     }
  107.    
  108.     // ============================================================
  109.     // Constructors
  110.     // ============================================================
  111.    
  112.    
  113.     /**
  114.      * Constructor which takes coefficients explicity.
  115.      *
  116.      * Behaviour: Constructs a fraction with the specified numerator and
  117.      *            denominator. Remember that your fraction should *always* be
  118.      *            stored in irreducible form.
  119.      *
  120.      * @param num   The numerator
  121.      * @param denom The denominator
  122.      */
  123.     public Fraction(int num, int denom) {
  124.     this.numerator = num;
  125.     this.denominator = denom;
  126.     this.reduce();
  127.     }
  128.  
  129.     /**
  130.      * Constructor which takes coefficients explicity.
  131.      *
  132.      * Behaviour: Constructs a fraction which represents an integer: set the
  133.      *            specified numerator and set denominator to 1.
  134.      *
  135.      * @param num The numerator
  136.      */
  137.     public Fraction(int num) {
  138.     // This method is complete.
  139.     this(num, 1);
  140.     }
  141.  
  142.     /**
  143.      * Default constructor.
  144.      *
  145.      * Behaviour: Constructs a fraction and set the default value to 0;
  146.      *            i.e. numerator = 0 and denominator = 1
  147.      */
  148.     public Fraction() {
  149.     // This method is complete.
  150.     this(0, 1);
  151.     }
  152.    
  153.    
  154.     // ==============================================================
  155.     // Convertors
  156.     //
  157.     // These functions will convert the Fraction to other data types.
  158.     // ==============================================================
  159.  
  160.     /**
  161.      * Convert the fraction to the floating point representation using double
  162.      * precision.
  163.      *
  164.      * Behaviour: Converts this fraction into a double.
  165.      *
  166.      * @return    A double value obtained by dividing the fraction's numerator
  167.      *            by its denominator.
  168.      */
  169.     public double toDouble() {
  170.     return (double) this.numerator/this.denominator;
  171.     }
  172.  
  173.  
  174.     /**
  175.      * Convert the fraction to the floating point representation using the
  176.      * single precision.
  177.      *
  178.      * Behaviour: Converts this fraction into a float value.
  179.      *
  180.      * @return    A float value obtained by dividing the fraction's numerator by
  181.      *            its denominator
  182.      */
  183.     public float toFloat() {
  184.     // This method is complete.
  185.     return (float) numerator / denominator;
  186.     }
  187.  
  188.     /**
  189.      * Convert the fraction to a string.
  190.      *
  191.      * Behaviour: Converts this fraction into a string
  192.      *
  193.      * @return    A string of the form "num/denom". If the denominator is 1,
  194.      *            returns a string containing only the numerator.
  195.      */
  196.     public String toString() {
  197.     return (String) "this.numerator/this.denominator";
  198.     }
  199.    
  200.    
  201.     // ============================================================
  202.     // Accessors and mutator methods (getters and setters)
  203.     // ============================================================
  204.  
  205.     /**
  206.      * Get denominator.
  207.      *
  208.      * Behaviour: Returns the denominator of this fraction.
  209.      *
  210.      * @return    The denominator of this fraction.
  211.      */
  212.     public int getDenominator() {
  213.     // This method is complete.
  214.     return denominator;
  215.     }
  216.  
  217.     /**
  218.      * Get numerator.
  219.      *
  220.      * Behaviour: Returns the numerator of this fraction.
  221.      *
  222.      * @return    The numerator of this fraction.
  223.      */
  224.     public int getNumerator() {
  225.     // This method is complete.
  226.     return numerator;
  227.     }
  228.  
  229.     //============================================================
  230.     // Operations with fractions - Core methods
  231.     //============================================================
  232.  
  233.     /**
  234.      * Addition operation.
  235.      *
  236.      * Behaviour: Adds this fraction to a supplied fraction.
  237.      *
  238.      * @param f  The fraction to be added.
  239.      * @return   The sum of this fraction and f.
  240.      */
  241.     public Fraction add(Fraction f) {
  242.     int num = this.numerator*f.denominator + this.denominator*f.numerator;
  243.     int denom = this.denominator*f.denominator;
  244.  
  245.     return new Fraction(num,denom);
  246.     }
  247.  
  248.     /**
  249.      * Subtraction operation.
  250.      *
  251.      * Behaviour: Subtracts a fraction from this fraction.
  252.      *
  253.      * @param f   The fraction to be subtracted.
  254.      * @return    The difference between this fraction and f.
  255.      *
  256.      */
  257.     public Fraction subtract(Fraction f) {
  258.     // This method is complete.
  259.     int num   = numerator * f.denominator - f.numerator * denominator;
  260.     int denom = denominator * f.denominator;
  261.    
  262.     return new Fraction(num, denom);
  263.     }
  264.  
  265.     /**
  266.      * Division.
  267.      *
  268.      * Behaviour: Divides this fraction by another fraction.
  269.      *
  270.      * @param f   The fraction to be used as a divisor.
  271.      * @return    The quotient of this fraction and f.
  272.      */
  273.     public Fraction divide(Fraction f) {
  274.     int num = this.numerator * f.denominator;
  275.     int denom = this.denominator * f.numerator;
  276.  
  277.     return new Fraction(num,denom);
  278.     }
  279.  
  280.     /**
  281.      * Multiplication.
  282.      *
  283.      * Behaviour: Multiplies this fraction and another fraction.
  284.      *
  285.      * @param f   The fraction to be multiplied.
  286.      * @return    The product of this fraction and f.
  287.      */
  288.     public Fraction multiply(Fraction f) {
  289.     Fraction fInverse = new Fraction(f.denominator,f.numerator);
  290.     return this.divide(fInverse);
  291.    
  292.     }
  293.    
  294.     // ======================================================================
  295.     // END OF USER MODIFIABLE CODE
  296.     //
  297.     // DO NOT CHANGE ANY FUNCTIONS IN THE SECTION BELOW; THEY ARE NEEDED FOR
  298.     // THE AUTOMATED MARKING SYSTEM. YOUR CODE CANNOT BE MARKED WITHOUT IT!
  299.     // ======================================================================
  300.  
  301.     /**
  302.      * Compare two fractions.
  303.      *
  304.      * @param  q  The fraction to compare with.
  305.      * @return    true if q is Fraction equal to this fraction .
  306.      */
  307.     public boolean equals(Fraction q) {
  308.     return (numerator   == q.getNumerator() &&
  309.         denominator == q.getDenominator());
  310.     }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement