Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. public class Rational extends Number implements Comparable<Rational> {
  2.  
  3.        public double doubleValue(){
  4.                return (double)num1 / (double)den1;
  5.        }
  6.        public float floatValue(){
  7.                return (float)num1 / (float)den1;
  8.        }
  9.        public int intValue(){
  10.                return num1 / den1;
  11.        }
  12.        public long longValue(){
  13.                return (long)num1 / (long)den1;
  14.        }
  15.        public int compareTo(Rational o){
  16.                if (num1 * o.den1 < o.num1 * den1){
  17.                        return 1;
  18.                }
  19.                else if (num1 * o.den1 > o.num1 * den1){
  20.                        return -1;
  21.                }
  22.                else{
  23.                        return 0;
  24.                }
  25.        }
  26.  
  27.  
  28.        public Rational(int n1, int d1) {
  29.                num1 = n1;
  30.                den1 = d1;
  31.        }
  32.  
  33.        public Rational add(Rational o) {
  34.                int n1 = num1 * o.den1+ o.num1 * den1;
  35.                int d1 = den1 * o.den1;
  36.  
  37.                return new Rational(n1 / gcd (n1, d1), d1/ gcd (n1,d1));
  38.        }
  39.  
  40.        public Rational sub(Rational o) {
  41.  
  42.                int n1 = num1 * o.den1 - o.num1 * den1;
  43.                int d1 = den1 * o.den1;
  44.                return new Rational( n1 / gcd (n1, d1), d1 / gcd (n1, d1) );
  45.        }
  46.  
  47.        public Rational mul(Rational o) {
  48.  
  49.                int n1 = num1 * o.num1;
  50.                int d1 =  den1 * o.den1;
  51.  
  52.                return new Rational( n1/ gcd (n1,d1), d1 / gcd (n1, d1));
  53.        }
  54.  
  55.        public Rational div(Rational o) {
  56.                int totalx = (num1 * o.den1);
  57.                int totaly = (den1 * o.num1);
  58.  
  59.                return new Rational (totalx / gcd (totalx , totaly), totaly / gcd
  60. (totalx, totaly));
  61.        }
  62.  
  63.        int gcd(int num1, int den1){
  64.                int r, x;
  65.                while (den1 != 0) {
  66.                                x = den1;
  67.                                r = (num1 % den1);
  68.                                den1 = r;
  69.                                num1 = x;
  70.                }
  71.                return num1;
  72.        }
  73.  
  74.        public String toString() {
  75.                if (den1 != 0)
  76.                        return "(" + num1 + " / " + den1 + ")";
  77.                else
  78.                        return ("Undefined");
  79.          }
  80.  
  81.        private int num1;
  82.        private int den1;
  83.  
  84. /*public static void main(String[] args) {
  85.  
  86.    Rational a = new Rational(1, 14);
  87.    Rational b = new Rational(1, 30);
  88.  
  89.    System.out.println(a + " + " + b + " = " + a.add(b));
  90.    System.out.println(a + " - " + b + " = " + a.sub(b));
  91.    System.out.println(a + " * " + b + " = " + a.mul(b));
  92.    System.out.println(a + " / " + b + " = " + a.div(b));
  93.        }*/
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement