Advertisement
H0_0H

Untitled

Feb 7th, 2023
1,195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.18 KB | None | 0 0
  1. /*
  2.  * FRACTION
  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: constructors, 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.     // ============================================================
  74.     // Constructors
  75.     // ============================================================
  76.  
  77.     /**
  78.      * Constructor which takes coefficients explicity.
  79.      *
  80.      * Behaviour: Constructs a fraction with the specified numerator and
  81.      *            denominator. Remember that your fraction should *always* be
  82.      *            stored in irreducible form.
  83.      *
  84.      * @param num   The numerator
  85.      * @param denom The denominator
  86.      */
  87.     public Fraction(int num, int denom) {
  88.         this.numerator = num;
  89.         this.denominator = denom;
  90.     }
  91.  
  92.     /**
  93.      * Constructor which takes coefficients explicity.
  94.      *
  95.      * Behaviour: Constructs a fraction which represents an integer: set the
  96.      *            specified numerator and set denominator to 1.
  97.      *
  98.      * @param num The numerator
  99.      */
  100.     public Fraction(int num) {
  101.         // This method is complete.
  102.         this(num, 1);
  103.     }
  104.  
  105.     /**
  106.      * Default constructor.
  107.      *
  108.      * Behaviour: Constructs a fraction and set the default value to 0;
  109.      *            i.e. numerator = 0 and denominator = 1
  110.      */
  111.     public Fraction() {
  112.         // This method is complete.
  113.         this(0, 1);
  114.     }
  115.  
  116.  
  117.     // ==============================================================
  118.     // Convertors
  119.     //
  120.     // These functions will convert the Fraction to other data types.
  121.     // ==============================================================
  122.  
  123.     /**
  124.      * Convert the fraction to the floating point representation using double
  125.      * precision.
  126.      *
  127.      * Behaviour: Converts this fraction into a double.
  128.      *
  129.      * @return    A double value obtained by dividing the fraction's numerator
  130.      *            by its denominator.
  131.      */
  132.     public double toDouble() {
  133.         double res = (double) this.numerator / this.denominator;
  134.         return res;
  135.     }
  136.  
  137.  
  138.     /**
  139.      * Convert the fraction to the floating point representation using the
  140.      * single precision.
  141.      *
  142.      * Behaviour: Converts this fraction into a float value.
  143.      *
  144.      * @return    A float value obtained by dividing the fraction's numerator by
  145.      *            its denominator
  146.      */
  147.     public float toFloat() {
  148.         // This method is complete.
  149.         return (float) numerator / denominator;
  150.     }
  151.  
  152.     /**
  153.      * Convert the fraction to a string.
  154.      *
  155.      * Behaviour: Converts this fraction into a string
  156.      *
  157.      * @return    A string of the form "num/denom". If the denominator is 1,
  158.      *            returns a string containing *only* the numerator.
  159.      */
  160.     public String toString() {
  161.         // Fill in this method to format the correct string to return.
  162.         return this.numerator + "/" + this.denominator;
  163.     }
  164.  
  165.  
  166.     // ============================================================
  167.     // Accessors and mutator methods (getters and setters)
  168.     // ============================================================
  169.  
  170.     /**
  171.      * Get denominator.
  172.      *
  173.      * Behaviour: Returns the denominator of this fraction.
  174.      *
  175.      * @return    The denominator of this fraction.
  176.      */
  177.     public int getDenominator() {
  178.         // This method is complete.
  179.         return denominator;
  180.     }
  181.  
  182.     /**
  183.      * Get numerator.
  184.      *
  185.      * Behaviour: Returns the numerator of this fraction.
  186.      *
  187.      * @return    The numerator of this fraction.
  188.      */
  189.     public int getNumerator() {
  190.         // This method is complete.
  191.         return numerator;
  192.     }
  193.  
  194.     //============================================================
  195.     // Operations with fractions - Core methods
  196.     //============================================================
  197.  
  198.     /**
  199.      * Addition operation.
  200.      *
  201.      * Behaviour: Adds this fraction to a supplied fraction.
  202.      *
  203.      * @param f  The fraction to be added.
  204.      * @return   The sum of this fraction and f.
  205.      */
  206.     public Fraction add(Fraction f) {
  207.         int new_num = this.numerator * f.denominator + f.numerator*this.denominator;
  208.         int new_denom = this.denominator * f.denominator;
  209.         Fraction res = new Fraction(new_num, new_denom);
  210.         res.reduce();
  211.         return res;
  212.     }
  213.  
  214.     /**
  215.      * Subtraction operation.
  216.      *
  217.      * Behaviour: Subtracts a fraction from this fraction.
  218.      *
  219.      * @param f   The fraction to be subtracted.
  220.      * @return    The difference between this fraction and f.
  221.      *
  222.      */
  223.     public Fraction subtract(Fraction f) {
  224.         // This method is complete.
  225.         int num   = numerator * f.denominator - f.numerator * denominator;
  226.         int denom = denominator * f.denominator;
  227.         Fraction res = new Fraction(num, denom);
  228.         res.reduce();
  229.         return res;
  230.     }
  231.  
  232.     /**
  233.      * Division.
  234.      *
  235.      * Behaviour: Divides this fraction by another fraction.
  236.      *
  237.      * @param f   The fraction to be used as a divisor.
  238.      * @return    The quotient of this fraction and f.
  239.      */
  240.     public Fraction divide(Fraction f) {
  241.         int new_num =this.numerator *f.denominator;
  242.         int new_denom = this.denominator * f.numerator;
  243.         Fraction res = new Fraction(new_num, new_denom);
  244.         res.reduce();
  245.         return res;
  246.     }
  247.  
  248.     /**
  249.      * Multiplication.
  250.      *
  251.      * Behaviour: Multiplies this fraction and another fraction.
  252.      *
  253.      * @param f   The fraction to be multiplied.
  254.      * @return    The product of this fraction and f.
  255.      */
  256.     public Fraction multiply(Fraction f) {
  257.         int new_num = this.numerator * f.numerator;
  258.         int new_denom = this.denominator * f.denominator;
  259.         Fraction res = new Fraction(new_num, new_denom);
  260.         res.reduce();
  261.         return res;
  262.     }
  263.  
  264.     // ======================================================================
  265.     // END OF USER MODIFIABLE CODE
  266.     //
  267.     // DO NOT CHANGE ANY FUNCTIONS IN THE SECTION BELOW; THEY ARE NEEDED FOR
  268.     // THE AUTOMATED MARKING SYSTEM. YOUR CODE CANNOT BE MARKED WITHOUT IT!
  269.     // ======================================================================
  270.  
  271.     /**
  272.      * Compare two fractions.
  273.      *
  274.      * @param  q  The fraction to compare with.
  275.      * @return    true if q is Fraction equal to this fraction .
  276.      */
  277.     public boolean sameAs(Fraction q) {
  278.         return (numerator   == q.getNumerator() &&
  279.                 denominator == q.getDenominator());
  280.     }
  281.  
  282.     private int gcd(int a, int b){
  283.         while (b != 0){
  284.             int t = b;
  285.             b = a % b;
  286.             a = t;
  287.         }
  288.         return a;
  289.     }
  290.  
  291.     private void reduce(){
  292.         int g = gcd(this.denominator, this.numerator);
  293.         this.denominator /= g;
  294.         this.numerator /= g;
  295.     }
  296. }
  297.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement