I have transformed the methods in the Rational class from class methods to object methods. I am asked to make the necessary changes in main method. How do I go about doing so? I already have a program that works but the methods are class methods. The problem with this program is that since many of the methods lose the parameters when changed to object methods, I do not know how I can call them in the main method.
jason escano wrote:I have transformed the methods in the Rational class from class methods to object methods. . . . I already have a program that works but the methods are class methods. . . . I do not know how I can call them in the main method.
Have a look at our FAQ. Don't give the rational number class a main() method. I think you want a RationalNumberDemo class with the main() method and a run() method:-There is lots more to say about your code. All these things will improve it and get you better marks:-
Lines 4‑5. Call the fields numerator and denominator.
Line 16: Expose the names numerator and denominator as the parameters for that constructor.
Lines 17‑18: Consider reducing the fraction ot its simplest form in the constructor. Make the numerator signed and the denominator always positive.
Lines 26‑30: Write a toString() method. You can then write System.out.println(number); The comment in line 26 is out of date.
Lines 36‑42: If you normalised the fraction in the constructor, you wouldn't have that very complicated method to negate the number. Consider creating a new fraction object representing the negation.
Line 58: That method isn't a function because it takes no input parameters. It simply performs a calculation on the object.
Line 66: That comment doesn't descrube what you are actually doing. You have to think precisely.
Lines 68‑83: That is the right way to calculate GCD, but you shouldn't use Capital Letters for a variabe; gcd is probably better. Note the repeated code (lines 108‑117). That tells me that you should have a gcd(int, int) method, which should probably be static since it is a pure function. It probably merits a utility class to contain it. Then you can simply call MyUtilities.gcd(23456, 78901). This method doesn't do what it says. It doesn't reduce anything, but creates a new object which is printed out and then discarded. You have the correct way to create a new object by adding in the next method
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java