• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Calling on object-oriented methods in the main  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

We have deleted another post which was identical to this one. Please always use the code button. I have edited your post with the code button, and doesn't it look better But it shows that your lines with comments are too long. The worst offender is line 58.

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
    https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!