Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ProperFraction.java
- //Shashank Raghavachari
- //This simple class extends the Fraction class.
- import TerminalIO.KeyboardReader;
- public class ProperFraction extends Fraction
- {
- //non-default constructor
- public ProperFraction(int n, int d)
- {
- super(n, d);//Calls Parent's constructor (Fraction constructor method)
- }
- //Public method to return integer portion of the fraction
- public int getWhole()
- {
- return getNumerator()/getDenominator();
- }
- //Public method to return remainder portion of the fraction
- public int getRemainder()
- {
- int remainder;
- if (Math.abs(getNumerator())>Math.abs(getDenominator()))
- {
- remainder = Math.abs(getNumerator())%Math.abs(getDenominator());
- }
- else
- {
- remainder = getNumerator();
- }
- return remainder;
- }
- //Method to attractively display proper fraction
- public void print()
- {
- //System.out.println(getNumerator() + " " + getDenominator());
- System.out.println(toString());
- }
- //Method overwrites the automatic toString() method
- public String toString()
- {
- String s1;
- int whole = getWhole();
- int remainder = getRemainder();
- int denominator = getDenominator();
- if (getDenominator() == 1)
- {
- s1 = getNumerator() + "";
- }
- else
- {
- if (Math.abs(getNumerator()) > Math.abs(getDenominator()))
- {
- s1 = whole + " " + remainder + "/" + denominator;
- }
- else
- {
- s1 = getNumerator() + "/" + getDenominator();
- }
- }
- return s1;
- }
- }
Add Comment
Please, Sign In to add comment