Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Fraction re-visited

Implements the Fraction2 Class which illustrates the use of accessor, mutator and helper methods.

 

 

 

 

 

 

 

 

 

 

 

 

Use set (mutator) methods instead.

 

 

 

 

 

 

 

 

 

use the new simplify helper method.

 

 

 

mutator methods

 

 

 

 

 

 

A new simplify method.

 

 

 

 

 

 

 

 

 

Every Java Class descends fron Class Object, therefore every Java Class has a toString() method, whether you can see it or not.

See Inheritance in Topic 5.


/**
* A Class for implementing the data type fraction
* Modified with accessor, mutator and helper methods
*
* @author Mr J
* @version 20040217
*/

public class Fraction2
{
   private int numerator;    // top part
   private int denominator;  // bottom part

  /**
   * Constructor for objects of class Fraction
   */

   public Fraction2()
   {
     // initialise instance variables
     setNumerator(0);
     setDenominator(1);
   }
   /**
   * Construct a fraction given two ints (numerator first)
   *
   * @param n the numerator
   * @param d the denominator
   */

   public Fraction2(int n, int d)
   {
     setNumerator(n);
     setDenominator(d);
   }

  /**
   * A method to add two fractions
   *
   * @param a a Fraction
   * @param b another Fraction
   * @return the sum of fractions a and b
   */

   public static Fraction2 add(Fraction2 a, Fraction2 b)
   {
    
 // find the common denominator, multiply the numerators
     int cd = (a.denominator * b.denominator);
     a.numerator = a.numerator * b.denominator;
     b.numerator = b.numerator * a.denominator;
    
 // now add them
     int nm = a.numerator + b.numerator;

    
// return a new (simplified) fraction instance
     Fraction2 newOne = new Fraction2(nm, cd);
     newOne.simplify();
     return newOne;
   }
  
/**
   * sets the numerator field
   *
   * @param n top part of fraction
   */

   public void setNumerator(int n)
   {
     this.numerator = n;
   }
   /**
   * sets the denominator field
   *
   * @param d bottom part of fraction
   */

   public void setDenominator(int d)
   {
     this.denominator = d;
   }
   /**
   * reduces fraction to lowest common denominator
   */

   private void simplify()
   {
     int start = numerator;
     boolean done = false;
     while ( (!done) && (start > 1) )
     {
       // check that the numerator can be divided exactly by start
       if (numerator%start == 0)
       {
         // see if the denominator can be divided exactly by the same number
         if (denominator%start == 0)
         {
           // got a result
           numerator = numerator / start;
           denominator = denominator / start;
           done = true;
         }
       }
       start = start - 1;
     }
   }
   /**
   * Override toString() method of Class Object
   *
   * @return a String representation of the Fraction
   */

   public String toString()
   {
     return("" + numerator + "/" + denominator);
   }
}

Related: [ Java Home | classes page ]

Modified from Fraction


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009