• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

Java working with Fractions

I need help with the method Fraction add. The idea of the program is to add two fractions together. how do I create and return a new fraction plus add the  newly created object fraction to the existing fraction.
public class Fraction 
{
  private int numerator;
  private int denominator;
 
  public Fraction(int numerator, int denominator) 
  {
    
    numerator = 0; 
    denominator = 0;
    
    do 
    
    (
    denominator == 1 )
       
     } 
     while denominator  == 0;
        
     
   
  }
 
  public int getNumerator() {
     return  numerator;
  }
 
  public int getDenominator() {
    return  denominator;
  }
 
  public Fraction add(Fraction f) {
 
    
    numerator2=0;
    denominator2=0
    
    return (numerator * denominator2) + ( denominator * numerator2)/ denominator*numerator2;
  }

Open in new window

0
Valisha
Asked:
Valisha
  • 2
2 Solutions
 
CEHJCommented:
Try
public Fraction add(Fraction f) { 
	return new Fraction(denominator * f.getNumerator() + f.getDenominator() * numerator, denominator*f.getDenominator());
}

Open in new window

0
 
objectsCommented:
>     numerator2=0;
>    denominator2=0

you need to set these

    numerator2=f.numerator;
    denominator2=f.denominator;


>     return (numerator * denominator2) + ( denominator * numerator2)/ denominator*numerator2;

you're close, but you need to return a fraction so instead of dividing you should be using the values to create a new fraction. Your maths is also a little wrong

    return new Fraction((numerator * denominator2) + ( denominator * numerator2), ( denominator* denominator2));

0
 
CEHJCommented:
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now