Solved

Java working with Fractions

Posted on 2009-05-11
3
395 Views
Last Modified: 2012-05-06
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
Comment
Question by:Valisha
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
Comment Utility
Try
public Fraction add(Fraction f) { 

	return new Fraction(denominator * f.getNumerator() + f.getDenominator() * numerator, denominator*f.getDenominator());

}

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
Comment Utility
>     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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now