Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Helps with class

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys. I am learning Java on my own. I have written a Fraction class. I would very much appreciate if you guys could offer some advices and inputs as to how I could improve it. Since I am learning on my own, chances are that I make many mistakes. Your comments will help me tremendously. Thanks.

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

. . . I am learning Java on my own. . . .

Please find somebody to help you, but looking at your code, you are doing quite well It is neatly laid out and you have all the right methods.

line 2 wrote:// Fractions must be in reduced form

In which case do the reduction when you create the object. You need a gcd() method, but don't put it in this class. Create a utility class with that method in:-Utility classes have several characteristics:-
  • 1: They exist to serve other code, and have no existence of their own.
  • 2: It is never necessary to make any instances, hence the private constructor (see JLS = Java┬« Language Specification).
  • 3: They only have static members.
  • 4: They need lots of documentation comments, which I haven't written, to be useful.
  • I wrote gcd() because I thought you would use it again in a different context. Two ways to use gcd:-Those imports only work if you have a package name.
    The old Sun style suggestions say not to write if (xyz) return a; else return b; but to use ?: instead. So I did.
    Be careful about names; temp #isn't usually a good name, and neither num nor deno is clear. Write those two words in full.
    Lots more to say, but I shall come back later.
     
    Campbell Ritchie
    Marshal
    Posts: 70625
    288
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    ken luong wrote:. . .
    // Fractions such as 1/3 must be displayed as 3.
    . . .

    1/3? Surely you mean 3/1?
    I firmly believe every class must have at least one constructor written, but also that multiple constructors simply constitute opportunities for errors to occur. Do you really need the no‑arguments constructor? I don't think you do.
    If somebody passes a 0 denominator, I don't like System.exit()) but I would be more aggressive than simply printing an error message:-The escape \u2019 is a posh apostrophe.
    I would also throw an ArithmeticException for large values, or use this sort of method instead of * (=multiply operator).But multiplyExact() is a lot better, isn't it.
    Set up the sign in the constructor; the toString() method isn't the place to calculate signs. Set up the sign in the constructor:-I think you should reserve // comments for something short at the end of the line; many of your comments would be better as /* comments */.
    You don't need to declare a temporary variable just to return it, and no need for no‑arguments constructor:-Note I have fiddled the indentation to get names to align vertically.
    Challenge 1: You have gone a long way towards making your class immutable. Immutability is usually a very good thing. (Sorry, a Very Good Thing.) Find out about immutability and its advantages and how to make your class completely immutable.
    Challenge 2: Find out how to write equals() and hashCode() methods. Some sources of information quoted in this old post of mine.
     
    ken luong
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you so very much for your insights Campbell Ritchie! I definite will keep your advices in mind as I learn more about Java.
     
    Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic