Java working with Fractions

Posted on 2009-05-11
Medium Priority
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;
    denominator == 1 )
     while denominator  == 0;
  public int getNumerator() {
     return  numerator;
  public int getDenominator() {
    return  denominator;
  public Fraction add(Fraction f) {
    return (numerator * denominator2) + ( denominator * numerator2)/ denominator*numerator2;

Open in new window

Question by:Valisha
  • 2
LVL 86

Accepted Solution

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

Open in new window

LVL 92

Assisted Solution

objects earned 600 total points
ID: 24359963
>     numerator2=0;
>    denominator2=0

you need to set these


>     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));

LVL 86

Expert Comment

ID: 24416866

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

627 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