Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Java working with Fractions

Posted on 2009-05-11
Medium Priority
416 Views
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;
}

numerator2=0;
denominator2=0

return (numerator * denominator2) + ( denominator * numerator2)/ denominator*numerator2;
}
``````
0
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
• 2

LVL 86

Accepted Solution

CEHJ earned 900 total points
ID: 24358680
Try
``````public Fraction add(Fraction f) {
return new Fraction(denominator * f.getNumerator() + f.getDenominator() * numerator, denominator*f.getDenominator());
}
``````
0

LVL 92

Assisted Solution

objects earned 600 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

ID: 24416866
:-)
0

## Featured Post

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
###### Suggested Courses
Course of the Month11 days, 8 hours left to enroll