I'm working on a project in Java. I've been given a program with classes and I just have to write the methods. I think I'm doing something wrong, whenever I run it, it doesn't assign the numerator and the denominator to a fraction. Please help! http://bpaste.net/show/3wURv8xijTw5gXAhcY6b/ I can provide the rest of the program, but I'm pretty sure the problem is in here.
Here's all the methods I have to fill out, I figure I'll just show everything. http://bpaste.net/show/B6pXsStbQIWgcfb3XtbH/
I know my problem is in line 23, but for the life of me, I cannot figure out how to fix it.
So what is going on at line 23? Looks like you've got: setFraction = (num, den) But what is that? You're already in the setFraction method, so you can't just call itself now you actually have to set the fraction with this method. I'm not entirely sure how your thing works, but I'm guessing you have a fraction object that has two fields called num and den to hold the two values. So when you called the constructor, Fraction(), then that means you've instantiated a new object. Then you need to take this object's num and den fields and set them to 0 and 1 respectively. num = 0; den = 1; But this is wrong, although almost right. It's wrong because what if we want to setFraction(2,3) ? We can't since it will just set it to 0/1 every time. But remember, the setFraction() method takes the arguments and calls them numerator and denominator. So we can then set: num = numerator; den = denominator; and you should be good and ready to use the method to set other things as well.
This worked! Thanks a ton!
Awesome. :)
Join our real-time social learning platform and learn together with your friends!