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 4 of 4

Thread: Fractions Code help

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Fractions Code help

    Hey All I have three classes. FractionGenerator, Fraction and MyProgram which is the main.

    I have FractionGenerator working, it is generating random numbers for numerator and denominator. My code has one error with the return type in my public boolean equals. I am not sure what to return. Also I am not sure if my add, subtract, multiply and divide methods are correct.


     
    //Program Summary: Fraction class represents a ratio of two integers. 
     
    public class Fraction
    {
        private int fNumerator;
        private int fDenominator;
     
     
        public Fraction(int numerator, int denominator)
        {
        this.fNumerator = fNumerator;
        this.fDenominator = fDenominator;
        }
        //
        public Fraction()
        {
        this.fNumerator = 0;
        this.fDenominator = 1;
    	}
    	public Fraction(int numerator)
    	{
    	this.fNumerator = numerator;
    	fDenominator = 1;
    	}
     
     
        // Mutator for numerator
       public void setNumerator(int numer)
       {
         this.fNumerator = numer;
       }
     
       // Mutator for denominator
       public void setDenominator(int denom)
       {
         this.fDenominator = denom;
       }
     
        public void writeFraction()
        {
        System.out.println(fNumerator + "/" + fDenominator);
        }
     
        public void reduceFraction()
    	{
    	int reduce;
     
    		if(this.fNumerator > this.fDenominator)
    		{
    			reduce = this.fDenominator;
    		}
    		else
    		{	
    			reduce = this.fNumerator;
    		}
     
    		while (reduce > 1)
    		{
    			if(this.fNumerator / reduce == 0 && this.fDenominator / reduce == 0)
    		{
    			this.fNumerator = this.fNumerator / reduce;
     
    		}
    			reduce --;
     
    		}
    	}
        public boolean equals(Fraction other)
        {
             if (this.fNumerator == other.fNumerator && this.fDenominator == other.fDenominator)
             {
                System.out.println("Equal");
             }
     
              else 
     
    			 System.out.println("Not Equal");
     
    			return a;
     
        } 
     
        public void add(Fraction other)
        {
          this.fNumerator = this.fNumerator * this.fDenominator + this.fDenominator + fNumerator;
          this.fDenominator = this.fNumerator * this.fDenominator;
        }
     
        public void subtract()
        {
          this.fNumerator = this.fNumerator * fDenominator - this.fDenominator - fNumerator;
          this.fDenominator = this.fNumerator * this.fDenominator;
        }
     
        public void multiply()
        {
        this.fNumerator = this.fNumerator * fDenominator * this.fDenominator * fNumerator;
        this.fDenominator = this.fNumerator * this.fDenominator;
        }
     
        public void divide()
        {
        this.fNumerator = this.fNumerator * fDenominator / this.fDenominator / fNumerator;
        this.fDenominator = this.fNumerator * this.fDenominator;
        }
     
    }


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Fractions Code help

    You need to return true if they are equal and false otherwise

  3. The Following User Says Thank You to DavidFongs For This Useful Post:

    SOK (November 8th, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Fractions Code help

    Thanks David,

    That takes care of that. Since this is my first program actually using more than one class, I am not sure how to call from the fraction class into my main program called MyProgram. It appears to be printing 0/0 for some reason.


     
    public class MyProgram
    {
    	public static void main(String[] args)
    	{
    		FractionGenerator a = new FractionGenerator();
    		FractionGenerator b = new FractionGenerator();
    		Fraction f1 = new Fraction(a.getNumerator(),a.getDenominator());
    		Fraction f2 = new Fraction(b.getNumerator(),b.getDenominator());
     
     
    		System.out.println("**************");
    		f1.writeFraction();
    		f2.writeFraction();
    		f1.equals(f2);
    		System.out.println("**************");
     
    		System.out.println("Multiply:     ");
    		f1.multiply(f2);
     
    		System.out.println("     Add:     ");
    		f1.add(f2);
     
    		System.out.println("  Divide:     ");
    		f1.divide(f2);
     
    		System.out.println("Subtract:     ");
    		f1.subtract(f2);
     
    		System.out.println("**************");
     
     
     
     
     
    		}
     
    }
    Last edited by SOK; November 8th, 2010 at 10:54 PM.

  5. #4
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Fractions Code help

    I have to assume you mean it is printing 0/0 when you are calling f1.writeFraction() and f2.writeFraction()...... The only way this could happen is if you are constructing the fraction with zeros.

    Put a print statement before these lines:

    Fraction f1 = new Fraction(a.getNumerator(),a.getDenominator());
    Fraction f2 = new Fraction(b.getNumerator(),b.getDenominator());

    That prints out the values of a & b: getNumerator() & getDenominator()

    The error is probably in your FractionGenerator class, but you haven't posted that code, so no help there yet.

  6. The Following User Says Thank You to DavidFongs For This Useful Post:

    javapenguin (November 9th, 2010)

Similar Threads

  1. Fractions
    By debug in forum Java Theory & Questions
    Replies: 1
    Last Post: March 8th, 2010, 10:29 PM