About this question
What is a fraction calculator in Java?
What is a fraction calculator in Java?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Java Expert
Answered on Oct 13, 2022
Regarding the fraction calculator java, OOP is not only about Polymorphism and Inheritance towards which you intuitively designed your operation classes, although incompletely and it was pointed out by Simon André Forsberg. OOP is also about Encapsulation which is often neglected by many. Encapsulation simply says that you have to hide the details of your implementation behind the public interface. This has a good side effect which is one of the most important OOP features: it makes you keep your data and routines that operate on that data together in one place. Encapsulation is what differentiates OOP from Procedural programming. A related principle is called Tell, Don't Ask.
This said, a proper OOP-style design would be to convert the operation classes to methods of the Fraction class
public class Fraction {
public void add(Fraction fraction) {}
public void subtract(Fraction fraction) {}
public void multiply(Fraction fraction) {}
public void divide(Fraction fraction) {}
}
Another thing is that you should make use of a constructor other than the default one. The constructor will ensure that the newly created fraction is complete and good to go. What happens if someone tries to perform an operation on a fraction without setting the numerator and denominator first? This will force you to validate fractions before each operation. Constructor also is a good way to specify the required object properties
public class Fraction {
public Fraction(int numerator, int denominator) {}
public void add(Fraction fraction) {}
public void subtract(Fraction fraction) {}
public void multiply(Fraction fraction) {}
public void divide(Fraction fraction) {}
}
Also, Fraction class should be immutable
public class Fraction {
public Fraction(int numerator, int denominator) {}
public Fraction add(Fraction fraction) {}
public Fraction subtract(Fraction fraction) {}
public Fraction multiply(Fraction fraction) {}
public Fraction divide(Fraction fraction) {}
}
PS. If you want to have a set of operations, Enum would be a better choice.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Java interview questions, answered
Guides, tips and career advice on Java from JanBask experts.
Java Frequently Asked J2EE Interview Questions and Answers You Must Read
Top 45 frequently asked Java J2EE interview questions to ace your interview. These J2EE questions are useful for freshers and…
Java Top 100+ Angular Interview Questions and Answers
Are you preparing for your next Angular interview? We have compiled the list of the top 100+ Angular Interview Questions &…
Java Top 51+ Simple Java Project Ideas for Beginners In 2025
Enhance your Java skills with our top 51+ project ideas! From beginner-friendly exercises to advanced applications, fuel your…
Java How Long Does It Takes To Learn Java?
Confused about How long does it take to learn Java ? Well, No worries! This blog will guide you with valuable information and…