HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Mathematical functions on fractions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
functionsfractionsmathematical

Problem

I have been challenged by @nhgrif to write a Fraction class to better my understanding of OOP. I have found an interesting challenge here (I apologize that the source is not mentioned, I was not able to find it either).


Problem statement: Create a class named Fraction having two integer
data members named for a fraction's numerator and denominator. The
class' default constructor should provide both data members with
default values of 1 if no explicit initialization is provided. The
constructor must also prohibit a 0 denominator value. Include member
functions for displaying an object's data values and mathematical
functions capable of adding, subtracting, multiplying, and dividing
two Fraction objects.

I have written this on Ideone.com hence explaining why everything is in one file. I have made a slight change to the default values given no parameters, as I have been told a 0/1 default fraction makes more sense than 1/1.

I have also added a simple but potentially useful method to cast a fraction into its decimal value.

I named my fraction variables using a pattern like fractionN_D where N is numerator and D is denominator. It seemed short enough to be practical (as compared to fractionThreeFourths) while still carrying meaning. Any suggestion on that is of course welcome.

```
class Fraction {
private final int numerator;
private final int denominator;

Fraction() {
numerator = 0;
denominator = 1;
}

Fraction (int numerator, int denominator) {
this.numerator = numerator;
if (denominator == 0) {
throw new IllegalArgumentException("Denominator cannot be zero.");
} else {
this.denominator = denominator;
}
}

public String getFraction() {
return numerator + "/" + denominator;
}
public double getFractionDecimal() {
return ((double) numerator) / denominator;
}

// Mathematical functions

public String addFrac

Solution

Public

class Fraction {


I get it that you're writing this in ideone so you cannot do this, but for the record, that class should be public class

Fraction() {


This constructor should be public

Fraction (int numerator, int denominator) {


This constructor should also be public

Constructor calls constructor

As your Fraction() constructor is essentially initializing it with specific values you can call one constructor from another

public Fraction() {
    this(0, 1);
}


Throw or else

if (denominator == 0) {
    throw new IllegalArgumentException("Denominator cannot be zero.");
} else {


If you throw an exception inside the if, there's no need to write else explicitly. Just put the code that is currently in the else after the if-statement. Like this:

public Fraction (int numerator, int denominator) {
    this.numerator = numerator;
    if (denominator == 0) {
        throw new IllegalArgumentException("Denominator cannot be zero.");
    }
    this.denominator = denominator;
}


(I do however agree about @Jeroen's answer about putting the denominator check first)

Returning something useful

public String addFraction(Fraction otherFraction) {


What do you get when you add a Fraction with another Fraction? (Mathematically speaking)

You get... A STRING!

No you don't. You get a Fraction

These mathematical methods you have written here should really return a new Fraction instance.

And you don't need to have Fraction in the name. Simply add, subtract, multiply, divide is enough.

MOAR METHODS!

I would recommend adding a toString method, in your case the implementation is simple:

@Override
public String toString() {
    return getFraction();
}


I would also like to see a normalize method that returns a fraction that has been reduced as much as possible, for example:

  • 18/4 --> 9/2



  • 24/4 --> 6/1



  • 4/8 --> 1/2



I am also missing public int getNumerator() and public int getDenominator(), which would make your Fraction class much more useful.

Code Snippets

class Fraction {
Fraction() {
Fraction (int numerator, int denominator) {
public Fraction() {
    this(0, 1);
}
if (denominator == 0) {
    throw new IllegalArgumentException("Denominator cannot be zero.");
} else {

Context

StackExchange Code Review Q#83245, answer score: 13

Revisions (0)

No revisions yet.