Java Training Course/JT05
Motivation
The goal of the next few sessions is the development of a real, useful Java class that is missing from the JDK.
This class will be named Rational
. It will represent a fractional number consisting of:
- an integer numerator displayed above a line (or before a slash), and
- a non-zero integer denominator, displayed below that line (or after the slash),
- methods for the addition, subtraction, multiplication and division of 2 such
Rational
s, - some additional, helper methods which are used internally in the class.
If you are not familiar with such rational numbers, you may read the Wikipedia article on fractions. A citation from there:
- Fractional numbers can also be written without using explicit numerators or denominators, by using decimals, percent signs, or negative exponents (as in 0.01, 1%, and 10−2 respectively, all of which are equivalent to 1/100). An integer such as the number 7 can be thought of as having an implicit denominator of one: 7 equals 7/1.
We make a little distinction between fractions (in general; sometimes imprecise 0.33333...) and rationals (with the representation by a numerator and a denominator; 1/3 is always exact).
- Subtask 1: Explain how the 4 arithmetic operations for fractions were done in school.
Least Common Multiple
Multiplication and division of rationals is very simple. But for addition and subtraction, the denominators must first be aligned to their so-called least common multiple (LCM).
- Subtask 2: Explain how you would compute the LCM.