Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Using Euclid's algorithm in a Fraction Class

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Using Euclid's algorithm in a Fraction Class

    I am trying to use Euclid's Algorithm in a fraction class. I am not finished with the class yet so there are parts that are incomplete. I am unsure of how I would use the method. Would I put in the setter methods? or Public fraction (int n, int d)?

    public class Fraction{
     
        private int numerator;
        private int denominator; 
        int gcd;
     
        public Fraction(){
     
        }
     
        public Fraction(int n, int d){
           setNumerator(n);
           setDenominator(d);
     
        }
     
        public static int gcd(int p, int q) {
            if (q == 0) return p;
            else return gcd(q, p % q);
        }
     
        public String toString(){
            return String.format("%d/%d", getNumerator(), getDenominator());
        }
     
        public boolean equals(Object otherObject){
            if(otherObject == null){
                return false;
            }else{
                if(!getClass().equals(otherObject.getClass())){
                    return false;
                }else{
                    Fraction otherFraction = (Fraction) otherObject; 
                    return getNumerator()== otherFraction.getNumerator() && 
                           getDenominator()== otherFraction.getDenominator();
     
                }
            }
        }
     
        public int getNumerator(){
            return numerator;
        }
     
        public void setNumerator(int newNumerator){
            numerator = newNumerator/gcd;
        }
     
        public int getDenominator(){
            return denominator ;
        }
     
        public void setDenominator(int newDenominator){
            if(newDenominator == 0 ){
                throw new IllegalStateException(String.format("Illegal denominator: %d", newDenominator));  
            } else{
                denominator = newDenominator;
            }
        }
     
    }


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Using Euclid's algorithm in a Fraction Class

    I'm not sure where you're going with this. It seems like you have a good start on it in your gcd method. You mention putting it in the setter methods or the constructor, what are you really trying to do with the algorithm? If you could give us a flow of what you're trying to accomplish it would be easier to help.

    Example:
    1. Create fraction
    2. Call Euclid's Algorithm
    3. Display answer
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. Slight problem with fraction calculator code
    By Jampolo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2013, 05:01 PM
  2. Euclid Calculator GCD and LCM using BigInteger
    By Exiled in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 18th, 2012, 11:00 PM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  4. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  5. Mixed Fraction JAVA
    By x3iancute in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2010, 09:18 PM