Solved

Java working with Fractions

Posted on 2009-05-11
3
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 24358680
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
ID: 24359963
>     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
ID: 24416866
:-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 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