0

I am learning how to code and I am having trouble with my class methods and testing these methods. Here is the problem:

In this lab, we will create a Fraction class. This class is used to represent a ratio of two integers. The main method of the FractionDriver class will contain the code that tests your Fraction class. I recommend that you test your Fraction class incrementally.

Your Fraction class should have two private integer instance variables, numerator and denominator. Initially, the value of numerator should be 0 and the value of denominator should be 1.

Write two mutator methods, setNumerator() and setDenominator(), that allow the user to set the numerator and the denominator to an integer value. Your code should not allow the denominator to be set to 0. If the user tries to set the denominator to 0, the value should not be changed.

Also, include a method named getValue() that returns the value of the numerator divided by the denominator as a double.

Add a toString() method that returns a String representation of the fraction in the form numerator/denominator, for example 5/3.

Finally, add an equals method that determines whether two objects of type Fraction are equal. Note that 3/5 and 6/10 should be considered equal.

Here is my code for my Fraction class:

public class Fraction {

private int numerator = 0;
private int denominator = 1;
private double divide;
//setting numerator and denominator
public void setNumerator(int numerator) {
    this.numerator = numerator;
}
public void setDenominator(int denominator) {
    if (denominator == 0) {
        return;
    }
    this.denominator = denominator;
}

//returning value of the numerator divided by a denominator as a double
public void getValue() {
    divide = numerator / denominator;
    this.divide = divide;
    System.out.println("The value of this fraction in decimal form is: " + divide);
}
//returning the fraction as a string #/#
public String toString() {
    return "Your fraction is: " + numerator + "/" + denominator;
}
public boolean equals(Fraction other) {
    if(other.divide == divide) {
        return true;
    }
    return false;
}

}

Here is the code for my driver so far:

public class FractionDriver {

public static void main(String[] args) {
    Fraction fract1 = new Fraction();
    Fraction fract2 = new Fraction();

    //initialize variables
    fract1.setNumerator(1);
    fract1.setDenominator(2);
    fract2.setNumerator(5);
    fract2.setDenominator(10);

    for(int i = 0; i < 1; i++) {
        //testing toString method
        System.out.println(fract1.toString());
        System.out.println(fract2.toString());
        fract1.getValue();
        fract2.getValue();

    }
}

}

When I test my getValue() method for both fractions, each have the result of 0.0 and I am not sure what I am doing wrong in my class method. Also, how do I test my equals method?

Russiancold
  • 952
  • 1
  • 7
  • 20
  • 3
    HINT: you are dividing ints. what is 1/2 in an integer? – crumbug Oct 18 '17 at 20:24
  • Samantha, I just wanted to say great job testing your code. Testing is very important, keep up the good work. Make sure you test the so-called "edge cases" as well like negative values, zeroes, etc. Also, consider creating tests that you can keep and run over and over. If you're ambitious you could take a look at [Junit](http://junit.org), but if that's too advanced for you perhaps just create a method for each test and each method throws an exception of some kind when the test fails. Then you simply run all your methods. – D.B. Oct 19 '17 at 01:50

2 Answers2

2

Dividing an int by an int gives another int. You don't get halves etc.

See Why is the result of 1/3 == 0?

user1675642
  • 717
  • 1
  • 5
  • 13
-2

Your method to get value is VOID... so that's your problem. It should be

//returning value of the numerator divided by a denominator as a double
public  double getValue() {
    divide = numerator / denominator;
    this.divide = divide;
    return this.divide; 
}

But better to make it

public  double getValue() {
    return numerator / denominator;
}
olexity
  • 95
  • 4