Solved

Java working with Fractions

Posted on 2009-05-11
3
400 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 82
how do i compare an object based on two fields 6 53
Cipher Configuration on Apache HTTPD 4 62
servlet example issue 6 38
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

786 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