Jump to content

how do i add fractions?

airfrog19

I'm trying to create a method to add two fractions together, but i''m not sure on how it would look like. 

This is how the method looks like:

public class Fraction implements FractionInterface, Comparable<Fraction>{private int num; // Numerator private int den; // Denominator  public Fraction(){// set fraction to default = 0/1num = 0;                den = 1;} // end default constructor public FractionInterface add(FractionInterface secondFraction)//blahblah

i'm suppose to use secondFraction, but not sure how though. 

Link to comment
Share on other sites

Link to post
Share on other sites

Make sure they have the same denominator and then add the numerators together

I done been through a whole lot. Trial, tribulations, but I know God - Kendrick Lamar


I question your mother's upbringing if you don't like me - Action Bronson


You apocalyptic dingleberry - James 'Captain Slow' May

Link to comment
Share on other sites

Link to post
Share on other sites

create a common denominator by multiplying their denominators,

 

the top half will be the the product of the numerators with the opposite denominators added. 

 

@airfrog19

ex. 2/3 + 4/7 = ?

 

denominator = 3*7 = 21

 

numerator = 2*7 + 4*3 = 26

 

final fraction = 26/21

Aesthetics of rigs matter

42

If you're interested, participate in LTT Build Offs

Link to comment
Share on other sites

Link to post
Share on other sites

create a common denominator by multiplying their denominators,

 

the top half will be the the product of the numerators with the opposite denominators added. 

 

@airfrog19

ex. 2/3 + 4/7 = ?

 

denominator = 3*7 = 21

 

numerator = 2*7 + 4*3 = 26

 

final fraction = 26/21

 

Exactly this. If your program requires you to simplify then you have to write some more code for that, but this is the way I'd approach this problem.

Turnip OC'd to 3Hz on air

Link to comment
Share on other sites

Link to post
Share on other sites

a/b + c/d = ad + cb / bd

 

Have your fraction method take two integers as input and return a pair. Then make an addition method that takes two fractions as parameters, manipulates them as per the above formula (which is the definition of addition of fractions, btw), as returns the result.

 

Naturally, if you want to simplify the fractions, that's going to add a bit extra.

 

Here's a quick and ugly implementation on Python without simplifying. Note that if I wanted simplifying, I could just implement it into the fraction method, since fraction_addition returns a fraction. However, implementing proper simplifying means adding an equivalence relation.

def fraction(numerator, denominator):    return [numerator, denominator]def fraction_addition(first_fraction, second_fraction):    first_fraction = fraction(first_fraction[0], first_fraction[1])    second_fraction = fraction(second_fraction[0], second_fraction[1])    numerator = first_fraction[1] * second_fraction[0] + second_fraction[1] * first_fraction[0]    denominator = first_fraction[1] * second_fraction[1]    return fraction(numerator, denominator)

And here's an example of how it can be used:

first = fraction(1, 2)second = fraction(2, 3)print fraction_addition(first, second)

I own and use, sorted from newest to oldest: SteelSeries 6Gv2. Microsoft SideWinder X4. Mionix Naos 7000. Zowie EC1 Evo. Microsoft SideWinder X8. Microsoft IntelliMouse Explorer 3.0. Dell U2414H. Samsung P2270H. AKG K273 Pro. Sennheiser HD555. Razer Goliathus Speed Medium. Func 1030 L. Qpad CT Medium.

I used to own: Razer DeathAdder 3G. Razer Krait. IntelliMouse Optical 1.1. SteelSeries QcK.

Link to comment
Share on other sites

Link to post
Share on other sites

 

a/b + c/d = ad + cb / bd

 

Have your fraction method take two integers as input and return a pair. Then make an addition method that takes two fractions as parameters, manipulates them as per the above formula (which is the definition of addition of fractions, btw), as returns the result.

 

Naturally, if you want to simplify the fractions, that's going to add a bit extra.

 

Here's a quick and ugly implementation on Python without simplifying. Note that if I wanted simplifying, I could just implement it into the fraction method, since fraction_addition returns a fraction. However, implementing proper simplifying means adding an equivalence relation.

def fraction(numerator, denominator):    return [numerator, denominator]def fraction_addition(first_fraction, second_fraction):    first_fraction = fraction(first_fraction[0], first_fraction[1])    second_fraction = fraction(second_fraction[0], second_fraction[1])    numerator = first_fraction[1] * second_fraction[0] + second_fraction[1] * first_fraction[0]    denominator = first_fraction[1] * second_fraction[1]    return fraction(numerator, denominator)

And here's an example of how it can be used:

first = fraction(1, 2)second = fraction(2, 3)print fraction_addition(first, second)

okay i'm not sure if this works so this is what i got:

Fraction add = new Fraction();        int newden = den * secondFraction.den;        add.den = newden;        add.num = num * secondFraction.den + secondFraction.num * den;
Link to comment
Share on other sites

Link to post
Share on other sites

 

@olbaze okay i'm not sure if this works so this is what i got:

Fraction add = new Fraction();        int newden = den * secondFraction.den;        add.den = newden;        add.num = num * secondFraction.den + secondFraction.num * den;

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

okay i'm not sure if this works

 

Did you try running it? If it gives you a wrong answer then it doesn't work, if it gives you the right answer in multiple cases it (probably) does.

Link to comment
Share on other sites

Link to post
Share on other sites

Did you try running it? If it gives you a wrong answer then it doesn't work, if it gives you the right answer in multiple cases it (probably) does.

yea. doesnt work. need to fix it 

Link to comment
Share on other sites

Link to post
Share on other sites

 

okay i'm not sure if this works so this is what i got:

Fraction add = new Fraction();        int newden = den * secondFraction.den;        add.den = newden;        add.num = num * secondFraction.den + secondFraction.num * den;

 

 

yea. doesnt work. need to fix it 

 

Just curious, what results did you get vs what was expected? It looks right to me, although you may want to modify it to not return and instead add to current instance.

public void add(Fraction secondFraction){    this.den *= secondFraction.den;    this.num = this.num * secondFraction.den + secondFraction.num * this.den;    // Reduce fraction if needed}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×