Tate Hsumsor

Greenhorn

Posts: 10

posted 3 years ago

Hi guys

I've been writing a fraction class code below that does a number of arithmetic calcs and when I run it these are the results I get. My gcd doesn't work when it comes to negative fractions and I'm not quite sure how to print.out the boolean methods ((greaterthan)), ((equals))and ((negative)). I'm also not sure if I have implemented those 3 methods properly. I'm still learning how to do unit testing. . Thanks in advance.

Enter numerator; then denominator.

-5

10

-5/10

Enter numerator; then denominator.

3

9

1/3

Sum:

-5/30

-0.16666666666666666

Product:

-5/30

-0.16666666666666666

Devide:

-15/30

-0.5

subtract:

-45/90

-0.5

negative:

1/6

0.16666666666666666

Lessthan:

1/6

0.16666666666666666

greaterthan:

1/6

0.16666666666666666

FRACTION CLASS

I've been writing a fraction class code below that does a number of arithmetic calcs and when I run it these are the results I get. My gcd doesn't work when it comes to negative fractions and I'm not quite sure how to print.out the boolean methods ((greaterthan)), ((equals))and ((negative)). I'm also not sure if I have implemented those 3 methods properly. I'm still learning how to do unit testing. . Thanks in advance.

Enter numerator; then denominator.

-5

10

-5/10

Enter numerator; then denominator.

3

9

1/3

Sum:

-5/30

-0.16666666666666666

Product:

-5/30

-0.16666666666666666

Devide:

-15/30

-0.5

subtract:

-45/90

-0.5

negative:

1/6

0.16666666666666666

Lessthan:

1/6

0.16666666666666666

greaterthan:

1/6

0.16666666666666666

FRACTION CLASS

Campbell Ritchie

Marshal

Posts: 56570

172

posted 3 years ago

You have fallen into the trap of writing lots of code and then wondering why it doesn't work.

Start by removing most of the //0000000000 comments which make the code harder to read. Then turn your computer off.

Write down an algorithm for GCD. Convert that to pseudocode, then consider turning your computer back on.

Then create a class with a gcd method (probably static) using Euclid's algorithm or something else. Get that to work.

Then you can consider the other methods.

Start by removing most of the //0000000000 comments which make the code harder to read. Then turn your computer off.

Write down an algorithm for GCD. Convert that to pseudocode, then consider turning your computer back on.

Then create a class with a gcd method (probably static) using Euclid's algorithm or something else. Get that to work.

Then you can consider the other methods.

posted 3 years ago

Oddly enough, I did exactly the same thing a few years back, except with BigInteger's, so here's a few suggestions for you:

1. Consider implementing

2. Your

3. Normalizing (also called 'reducing') can be done "in arrears" - ie, when the Fraction is

4. Consider adding an

5. You never check for overflow, despite the fact that pretty much

6. Fractions have a lot in common with floating-point numbers, so you might want to consider whether you want to support "infinites" (n/0) and/or "NaN" (0/0).

7. You keep the sign in the numerator, which is fine (and probably simplest); but an alternative arrangement is to store the sign separately and make your numerator and divisor positive (sometimes called "sign-magnitude"). One thing this does is allow you to store ±0 as distinct values if you want to; but it's not wildly important.

8. Consider making the class immutable and have your arithmetic methods return

and a few "efficiency" things for you:

1. When adding or subtracting, you don't need to normalize if the divisors are equal.

2. When muliplying or dividing, you don't have to normalize if either operand is 1 or -1.

3. When comparing, check the signs first, and then whether one operand is "vulgar" and the other isn't. You only need to compare products if

HIH

Winston

Tate Hsumsor wrote:I've been writing a fraction class...

Oddly enough, I did exactly the same thing a few years back, except with BigInteger's, so here's a few suggestions for you:

1. Consider implementing

`Comparable<Fraction>`, rather than separate

`lessThan()`and

`greaterThan()`methods.

2. Your

`lessThan()`method doesn't look right (haven't checked the other one).

3. Normalizing (also called 'reducing') can be done "in arrears" - ie, when the Fraction is

*used*, rather than every time you get a result. You may also find it useful to have

`normalize()`return a

`Fraction`- ie,

`this`, after you've reduced it.

4. Consider adding an

`invert()`method.

5. You never check for overflow, despite the fact that pretty much

*any*arithmetic on a Fraction will tend to increase the sizes of the numerator and/or divisor.

6. Fractions have a lot in common with floating-point numbers, so you might want to consider whether you want to support "infinites" (n/0) and/or "NaN" (0/0).

7. You keep the sign in the numerator, which is fine (and probably simplest); but an alternative arrangement is to store the sign separately and make your numerator and divisor positive (sometimes called "sign-magnitude"). One thing this does is allow you to store ±0 as distinct values if you want to; but it's not wildly important.

8. Consider making the class immutable and have your arithmetic methods return

*new*Fractions, rather than updating

`this`one. I think you'll find the API becomes a lot simpler.

and a few "efficiency" things for you:

1. When adding or subtracting, you don't need to normalize if the divisors are equal.

2. When muliplying or dividing, you don't have to normalize if either operand is 1 or -1.

3. When comparing, check the signs first, and then whether one operand is "vulgar" and the other isn't. You only need to compare products if

*both*of the those cases are false.

HIH

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Tate Hsumsor

Greenhorn

Posts: 10

posted 3 years ago

You're most welcome. Glad to see someone else ploughing the same furrow. I always thought it might be

Winston

Tate Hsumsor wrote:Winston thank you so much that was seriously helpful. Much appreciated...

You're most welcome. Glad to see someone else ploughing the same furrow. I always thought it might be

*kinda*useful.

Winston

Articles by Winston can be found here