//===========================================
// rational  number description
// Dr. Turhan Coban
//===========================================
import javax.swing.JOptionPane; // giriş çıkış


// class rational numbers

public class rational {
    // sınıf degişkenleri
    protected int   numerator;
    protected int   denominator;

    // kurucu metodlar

public rational()
{
numerator=0;
denominator=1;
}

public rational(int nnumerator,int ndenominator) throws dividebyzeroException
{
numerator=nnumerator;
if(ndenominator==0) throw new dividebyzeroException();
denominator=ndenominator;
}

public rational(int numer)
{
numerator=numer;
denominator=1;
}

public rational(rational c ) throws dividebyzeroException
{
numerator=c.numerator();
if(c.denominator()==0) throw new dividebyzeroException();
denominator=c.denominator();
}

//calculation methods

public int leastcommondivisor()
{
// finds least common dividor(en küçük ortak bölen)
int n=numerator;
int m=denominator;
if(n==0)
   return m;
if(m==0)
   return n;
while(m != n)
{
  if(n>m)
     n=n-m;
  else
     m=m-n;
}
return n;
}

public void simplify()
{
//simplify the rational number
int isaret=1;
if(numerator<0)
{
 isaret=-isaret;
 numerator=-numerator;
}
if(denominator<0)
{
 isaret=-isaret;
 denominator=-denominator;
}
if(denominator==0) throw new dividebyzeroException();
int ebob=leastcommondivisor();
ebob=Math.abs(ebob);
numerator=isaret*numerator/ebob;
denominator=denominator/ebob;
}


// input output methods

public int numerator()
{
return numerator;
}

public int denominator()
{
return denominator;
}

public void input_numerator(int r)
{
numerator=r;
}

public void input_denominator(int i) throws dividebyzeroException
{
if(i==0) throw new dividebyzeroException();
denominator=i;
}

public void input_rational(rational right)
{
numerator=right.numerator();
denominator=right.denominator();
}

public void input_rational(int nr,int ni)
{
numerator=nr;
denominator=ni;
}

public void input_rational(int nr)
{
numerator=nr;
denominator=1;
}

public void input_rational(double d)
{
// load rational equivalent of real number
// there area a little approximation of course
input_rational((int)d*100000,100000);
simplify();
}

public double toDouble()
{
//real number equivalent of rational number
return ((double)numerator/(double)denominator);
}

public static rational convert_to_rational(double d)
{
rational b=new rational();
b.input_rational((int)d*100000,100000);
b.simplify();
return b;
}


public void add(rational  right)
{
numerator = numerator*right.denominator() + right.numerator()*denominator;
denominator = denominator*right.denominator();
simplify();
}

public void add(int right)  
{
numerator = numerator + right*denominator;
simplify();
}

public void substract(rational  right)  
{
numerator = numerator*right.denominator() - right.numerator()*denominator;
denominator = denominator*right.denominator();
simplify();
}

public void substract(int right)  
{
numerator = numerator - right*denominator;
simplify();
}

public void multiply(rational right )  
{
numerator = numerator*right.numerator();
denominator = denominator*right.denominator();
simplify();
}

public void multiply(int right )  
{
numerator = numerator*right;
simplify();
}

public void divide(rational right )  
{
numerator = numerator*right.denominator();
denominator = denominator*right.numerator();
simplify();
}

public void divide(int right )  
{
denominator = denominator*right;
simplify();
}

public static rational add(rational left, rational right)  
{
 int r1= left.numerator() * right.denominator() + right.numerator() * left.denominator();
 int i1= left.denominator()* right.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

public static rational add(rational left, int right)  
{
 int r1=left.numerator() + right*left.denominator();
 int i1=left.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

public static rational add(int left, rational right)  
{
 int r1=right.numerator() + left*right.denominator();
 int i1=right.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

//===================================
public static rational substract(rational left, rational right)  
{
 int r1= left.numerator() * right.denominator() - right.numerator() * left.denominator();
 int i1= left.denominator()* right.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

public static rational substract(rational left, int right)  
{
 int r1=left.numerator() - right*left.denominator();
 int i1=left.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

public static rational substract(int left, rational right)  
{
 int r1=right.numerator() - left*right.denominator();
 int i1=right.denominator();
 rational sonuc;
 sonuc=new rational(r1,i1);
 sonuc.simplify();
 return sonuc;
}

//===================================

public static rational multiply(rational left, rational right)  
{ 
 rational sonuc;
 sonuc=new rational(left.numerator()*right.numerator(),left.denominator()*right.denominator());
 sonuc.simplify();
 return sonuc;
}

public static rational multiply(rational left, int right)  
{ 
 rational sonuc;
 sonuc=new rational(left.numerator()*right,left.denominator());
 sonuc.simplify();
 return sonuc;
}

public static rational multiply(int left, rational right)  
{ 
 rational sonuc;
 sonuc=new rational(right.numerator()*left,right.denominator());
 sonuc.simplify();
 return sonuc;
}

public static rational divide(rational left, rational  right)  
{ 
 double a=right.numerator()*right.numerator()+right.denominator()*right.denominator();
 rational sonuc;
 sonuc=new rational((int)(left.numerator()*right.denominator()),(int)(left.denominator()*right.numerator()));
 sonuc.simplify();
 return sonuc;
}

public static rational divide(rational left, int  right)  
{ 
 rational sonuc;
 sonuc=new rational(left.numerator(),(left.denominator()*right));
 sonuc.simplify();
 return sonuc;
}

public boolean smaller_than(rational left,rational right)
{
// less then comparison of two rational numbers
return (left.toDouble() < right.toDouble());
}

public boolean smaller_than_equals(rational left,rational right)
{
// less then and equals comparison of two rational numbers
return (left.toDouble() <= right.toDouble());
}

public boolean greater_than(rational left,rational right)
{
// greater_than then comparison of two rational numbers
return left.toDouble() > right.toDouble();
}

public boolean greater_than_equals(rational left,rational  right)
{
// greater_than then and equals comparison of two rational numbers
return left.toDouble() >= right.toDouble();
}


public boolean equals(rational left,rational right)
{
// equals comparison of two rational numbers
return left.toDouble() == right.toDouble();
}

public boolean not_equals(rational left,rational  right)
{
// not equals comparison of two rational numbers
return left.toDouble() != right.toDouble();
}

public static String toString(rational  value)
{ //formatted output
String b="";
if(Math.abs(value.denominator())!=1)
  {
    b=b+"( "+value.numerator()+" / "+value.denominator()+" )";
  }
else
  {
    b=b+value.numerator()+" ";
  }
return b;
}

public String toString()
{
// output method
String b="";
if(Math.abs(denominator())!=1)
  {
    b=b+"( "+numerator+" / "+denominator()+" )";
  }
else
  {
    b=b+numerator()+" ";
  }
return b;
}
                };

//rational sınıfının sonu


