Guest User

Untitled

a guest
Dec 10th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. //ProperFraction.java
  2. //Shashank Raghavachari
  3. //This simple class extends the Fraction class.
  4.  
  5. import TerminalIO.KeyboardReader;
  6. public class ProperFraction extends Fraction
  7. {
  8.     //non-default constructor
  9.     public ProperFraction(int n, int d)
  10.     {
  11.         super(n, d);//Calls Parent's constructor (Fraction constructor method)     
  12.     }
  13.    
  14.     //Public method to return integer portion of the fraction
  15.     public int getWhole()
  16.     {
  17.         return getNumerator()/getDenominator();    
  18.     }
  19.    
  20.     //Public method to return remainder portion of the fraction
  21.     public int getRemainder()
  22.     {
  23.         int remainder;
  24.        
  25.         if (Math.abs(getNumerator())>Math.abs(getDenominator()))
  26.         {
  27.             remainder = Math.abs(getNumerator())%Math.abs(getDenominator());
  28.         }
  29.         else
  30.         {
  31.             remainder = getNumerator();
  32.         }
  33.        
  34.         return remainder;
  35.     }
  36.    
  37.     //Method to attractively display proper fraction
  38.     public void print()
  39.     {
  40.         //System.out.println(getNumerator() + "  " + getDenominator());
  41.         System.out.println(toString());
  42.     }
  43.    
  44.     //Method overwrites the automatic toString() method
  45.     public String toString()
  46.     {
  47.         String s1;
  48.        
  49.         int whole = getWhole();
  50.         int remainder = getRemainder();
  51.         int denominator = getDenominator();
  52.        
  53.         if (getDenominator() == 1)
  54.         {
  55.             s1 = getNumerator() + "";  
  56.         }
  57.         else
  58.         {
  59.             if (Math.abs(getNumerator()) > Math.abs(getDenominator())) 
  60.             {
  61.                 s1 = whole + " " + remainder + "/" + denominator;
  62.                
  63.             }
  64.             else
  65.             {
  66.                 s1 = getNumerator() + "/" + getDenominator();
  67.             }
  68.         }
  69.                
  70.         return s1;
  71.     }
  72. }
Add Comment
Please, Sign In to add comment