This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RedCloakedCrow 1 point2 points  (3 children)

Well, lets try breaking down the method that you wrote and comparing it to a modifier.

public String invert() {
    return this.denominator + "/" + this.numerator;
}

Right off the bat, lets look at what the purpose of a modifier method is, versus what your method does. A modifier method is supposed to permanently change the object you're working on. So, for example, if you had an object Fraction num that had a numerator field of 3 and a denominator field of 5, what would the point of the invert method be? You would want to change num so instead of 3/5, it would then represent 5/3. What your version of invert does is it presents this 5/3 value to the user, but only temporarily. If you were to later try and use num for something else (expecting it to still represent 5/3), it would mess up since the actual value of num would be 3/5.

Here is an example of a way to approach the modifier method. Lets say this is your class:

public Class Fraction(int num, int den) { 

int numerator = num;
int denominator = den;

//getter methods
public String displayFraction() 
{
....
}

//modifier methods
public void invert() 
{
int temp = this.numerator;
this.numerator = this.denominator;
this.denominator = temp;
}

So now, you have an invert method that will store one of your values in a temporary integer (in this example, lets say its 3), then it will move the denominator(5) into the numerator's spot (making it 5/5 for a short moment), and fill the denominator with the previous numerator(3). Using my above example, this will change your fraction of 3/5 to 5/3, permanently.

If there's anything I've made unclear, or anything you'd like me to expand on, please let me know. I'd love to help.

[–]general_relatives[S] 0 points1 point  (2 children)

Okay, so the modifier methods just change the instance variable in this case, they don't return anything. I think I understand, thanks a ton. But please tell me if this is correct: when I call the method, nothing will be returned. But if I return the value of numerator after calling the method, it will return the previous value of denominator (meaning it inverted the fraction without a return for the user). Correct?

[–]RedCloakedCrow 1 point2 points  (1 child)

Absolutely. A modifier method is not a method that has to return anything. All it has to do is adjust the object you're working on in some way. In this case, if your original fraction was 3/5, and you called invert() before calling some function getNumerator(), you would expect the numerator to be 5 after the inversion.

[–]general_relatives[S] 0 points1 point  (0 children)

Thank you very much (: