Java program for rational numbers
import java.util.Scanner;
public class RationalNumbers
{
public int getNumerator ()
{
return numerator;
}
public int getDenominator ()
{
return denominator;
}
public boolean IsEqual(RationalNumbers r1)
{
if(this.numerator == r1.numerator && denominator == r1.denominator)
{
return true;
}
return false;
}
public boolean IsNotEqual(RationalNumbers r1)
{
return !(this.IsEqual(r1));
}
public boolean IsGreater(RationalNumbers r1)
{
if(this.ToDouble() > r1.ToDouble())
{
return true;
}
return false;
}
public boolean IsGreaterEqual(RationalNumbers r1)
{
return !(this.IsLess(r1));
}
public boolean IsLessEqual(RationalNumbers r1)
{
return !(this.IsGreater(r1));
}
public boolean IsLess(RationalNumbers r1)
{
if(this.ToDouble() < r1.ToDouble())
{
return true;
}
return false;
}
public double ToDouble()
{
return ((double)this.numerator/this.denominator);
}
public int ToInt()
{
return (this.numerator/this.denominator);
}
public RationalNumbers negate()
{
return new RationalNumbers(numerator*-1, denominator);
}
public void get_input()
{
Scanner scan = new Scanner(System.in);
String s1 = scan.nextLine();
char s2[];
if(s1.charAt(0) == '/')
{
System.out.println("Error: Invalid Value");
return ;
}
s2 = s1.toCharArray();
int i = 0 ;
int num;
for (;s2[i] != '\0' && s2[i]!= '/'; i++)
{
if(s2[i] >= '0' && s2[i] <= '9')
{
num = (int) s2[i] - (int)'0';
this.numerator = this.numerator*10 + num;
}
else
{
System.out.println("Error : Invalid Input");
return;
}
if(i+1<s2.length)
{
if(s2[i+1] == '/')
{
break;
}
}
else if(i+1>=s2.length)
{
this.denominator = 1;
return;
}
}
i++;
i++;
if(s2[i]=='0')
{
System.out.println("Error : The Denominator can't be zero.");
}
else if(s2[i] >= '0' && s2[i] <= '9')
{
this.denominator =0;
for (;s2.length > i; i++)
{
if(s2[i] >= '0' && s2[i] <= '9')
{
num = (int) s2[i] - (int)'0';
this.denominator = this.denominator*10 + num;
}
else
{
System.out.println("Error : Invalid Input");
return;
}
}
}
}
public RationalNumbers reciprocal()
{
return new RationalNumbers(denominator, numerator);
}
public RationalNumbers(int num,int den)
{
numerator = num;
denominator = den;
}
public RationalNumbers addition(RationalNumbers rn2)
{
int den = this.denominator*rn2.denominator;
int num = this.numerator*(den/this.denominator)+ rn2.numerator*(den/rn2.denominator);
return new RationalNumbers(num,den);
}
public RationalNumbers subtract( RationalNumbers rn2)
{
int den = this.denominator*rn2.denominator;
int num = this.numerator*(den/this.denominator)- rn2.numerator*(den/rn2.denominator);
return new RationalNumbers(num,den);
}
public RationalNumbers multiply( RationalNumbers rn2)
{
int den = this.denominator*rn2.denominator;
int num = this.numerator * rn2.numerator;
return new RationalNumbers(num,den);
}
public RationalNumbers divide( RationalNumbers rn2)
{
int den = this.denominator * rn2.numerator;
int num = this.numerator * rn2.denominator;
return new RationalNumbers(num,den);
}
public RationalNumbers simplyfy()
{
int num,den;
int gcd1 = gcd(this.numerator,this.denominator);
num = this.numerator / gcd1;
den =this.denominator /gcd1;
return new RationalNumbers(num, den);
}
public int gcd(int num,int den)
{
if(den == 0)
return num;
else
return gcd(den , num % den);
}
public void display()
{
System.out.println(this.numerator + "/" + this.denominator);
}
public RationalNumbers()
{
this.numerator = 0 ;
this.denominator = 1;
}
private int numerator,denominator;
public static void main(String[] args)
{
int choice;
boolean flag;
Scanner scan = new Scanner(System.in);
RationalNumbers rn = new RationalNumbers();
System.out.println("The Values are not changed after performing operations.\n to change values use option 9");
System.out.println("Enter first rational Number : ");
RationalNumbers r1 = new RationalNumbers();
r1.get_input();
System.out.println("Enter second rational Number : ");
RationalNumbers r2 = new RationalNumbers();
r2.get_input();
System.out.println("What Do You Want to do ");
System.out.println("1) Take Reciprocal");
System.out.println("2) Addition");
System.out.println("3) Subtraction");
System.out.println("4) Multiplication");
System.out.println("5) Division");
System.out.println("6) Negation");
System.out.println("7) Simplify");
System.out.println("8) Print Number");
System.out.println("9) Change Value");
System.out.println("10) Logical Operations");
System.out.println("0) Exit");
System.out.println("\nEnter Choice :: ");
String s = scan.nextLine();
choice = Integer.parseInt(s);
while(choice != 0)
{
switch(choice)
{
case 1:
System.out.println("Do Reciprocal of.");
System.out.println("1) First.");
System.out.println("2) Second.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
rn = r1.reciprocal();
else if(choice == 2)
rn = r2.reciprocal();
break;
case 2:
rn =r1.addition(r2);
break;
case 3:
System.out.println("1) Subtract First num from Second.");
System.out.println("2) Subtract Second num from First.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
rn = r2.subtract(r1);
else if(choice == 2)
rn = r1.subtract(r2);
break;
case 4:
rn = r1.multiply(r2);
break;
case 5:
System.out.println("1) Divide First num By Second.");
System.out.println("2) Divide Second num By First.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
rn = r1.divide(r2);
else if(choice == 2)
rn = r2.divide(r1);
break;
case 6:
System.out.println("Do negation of.");
System.out.println("1) First.");
System.out.println("2) Second.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
rn = r1.negate();
else if(choice == 2)
rn = r2.negate();
break;
case 7:
System.out.println("Do Simplification of.");
System.out.println("1) First Number.");
System.out.println("2) Second Number.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
rn = r1.simplyfy();
else if(choice == 2)
rn = r2.simplyfy();
break;
case 8:
System.out.println("Display.");
System.out.println("1) First Number.");
System.out.println("2) Second Number.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
r1.display();
else if(choice == 2)
r2.display();
break;
case 9:
System.out.println("Change Value Of.");
System.out.println("1) First Number.");
System.out.println("2) Second Number.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
r1.get_input();
else if(choice == 2)
r2.get_input();
break;
case 10:
System.out.println("Check.");
System.out.println("1) Is First Number Greater.");
System.out.println("2) Is First Number Greater or Equal.");
System.out.println("3) Is First Number Equal to Second.");
System.out.println("4) Is First Number Not Equal to Second.");
System.out.println("5) Is First Number Lesser or Equal.");
System.out.println("6) Is First Number Lesser.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice <=0 && choice >= 7)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
switch(choice)
{
case 1:
if(r1.IsGreater(r2))
{
System.out.println("First number is greater than second number");
}
else
System.out.println("First number is Not greater than second number");
break;
case 2:
if(r1.IsGreaterEqual(r2))
{
System.out.println("First number is greater or equal to second number");
}
else
System.out.println("First number is Not greater or equal to second number");
break;
case 3:
if(r1.IsEqual(r2))
{
System.out.println("First number is equal to second number");
}
else
System.out.println("First number is Not equal to second number");
break;
case 4:
if(r1.IsNotEqual(r2))
{
System.out.println("First number is Not Equal to second number");
}
else
System.out.println("First number is Equal to second number");
break;
case 5:
if(r1.IsLess(r2))
{
System.out.println("First number is Less Than second number");
}
else
System.out.println("First number is Not Less Than second number");
break;
case 6:
if(r1.IsLessEqual(r2))
{
System.out.println("First number is Less Than or Equal to second number");
}
else
System.out.println("First number is Not Less Than or Equal to second number");
break;
}
break;
case 0:
choice = 0 ;
break;
default:
System.out.println("ERROR!! Invalid Choice.\n");
}
if(choice!=0)
{
rn.display();
System.out.println("What Do You Want to do ");
System.out.println("1) Take Reciprocal");
System.out.println("2) Addition");
System.out.println("3) Subtraction");
System.out.println("4) Multiplication");
System.out.println("5) Division");
System.out.println("6) Negation");
System.out.println("7) Simplify");
System.out.println("8) Print Number");
System.out.println("9) Change Value");
System.out.println("10) Logical Operations");
System.out.println("0) Exit");
System.out.println("\nEnter Choice :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
}
}
};
public class RationalNumbers
{
public int getNumerator ()
{
return numerator;
}
public int getDenominator ()
{
return denominator;
}
public boolean IsEqual(RationalNumbers r1)
{
if(this.numerator == r1.numerator && denominator == r1.denominator)
{
return true;
}
return false;
}
public boolean IsNotEqual(RationalNumbers r1)
{
return !(this.IsEqual(r1));
}
public boolean IsGreater(RationalNumbers r1)
{
if(this.ToDouble() > r1.ToDouble())
{
return true;
}
return false;
}
public boolean IsGreaterEqual(RationalNumbers r1)
{
return !(this.IsLess(r1));
}
public boolean IsLessEqual(RationalNumbers r1)
{
return !(this.IsGreater(r1));
}
public boolean IsLess(RationalNumbers r1)
{
if(this.ToDouble() < r1.ToDouble())
{
return true;
}
return false;
}
public double ToDouble()
{
return ((double)this.numerator/this.denominator);
}
public int ToInt()
{
return (this.numerator/this.denominator);
}
public RationalNumbers negate()
{
return new RationalNumbers(numerator*-1, denominator);
}
public void get_input()
{
Scanner scan = new Scanner(System.in);
String s1 = scan.nextLine();
char s2[];
if(s1.charAt(0) == '/')
{
System.out.println("Error: Invalid Value");
return ;
}
s2 = s1.toCharArray();
int i = 0 ;
int num;
for (;s2[i] != '\0' && s2[i]!= '/'; i++)
{
if(s2[i] >= '0' && s2[i] <= '9')
{
num = (int) s2[i] - (int)'0';
this.numerator = this.numerator*10 + num;
}
else
{
System.out.println("Error : Invalid Input");
return;
}
if(i+1<s2.length)
{
if(s2[i+1] == '/')
{
break;
}
}
else if(i+1>=s2.length)
{
this.denominator = 1;
return;
}
}
i++;
i++;
if(s2[i]=='0')
{
System.out.println("Error : The Denominator can't be zero.");
}
else if(s2[i] >= '0' && s2[i] <= '9')
{
this.denominator =0;
for (;s2.length > i; i++)
{
if(s2[i] >= '0' && s2[i] <= '9')
{
num = (int) s2[i] - (int)'0';
this.denominator = this.denominator*10 + num;
}
else
{
System.out.println("Error : Invalid Input");
return;
}
}
}
}
public RationalNumbers reciprocal()
{
return new RationalNumbers(denominator, numerator);
}
public RationalNumbers(int num,int den)
{
numerator = num;
denominator = den;
}
public RationalNumbers addition(RationalNumbers rn2)
{
int den = this.denominator*rn2.denominator;
int num = this.numerator*(den/this.denominator)+ rn2.numerator*(den/rn2.denominator);
return new RationalNumbers(num,den);
}
public RationalNumbers subtract( RationalNumbers rn2)
{
int den = this.denominator*rn2.denominator;
int num = this.numerator*(den/this.denominator)- rn2.numerator*(den/rn2.denominator);
return new RationalNumbers(num,den);
}
public RationalNumbers multiply( RationalNumbers rn2)
{
int den = this.denominator*rn2.denominator;
int num = this.numerator * rn2.numerator;
return new RationalNumbers(num,den);
}
public RationalNumbers divide( RationalNumbers rn2)
{
int den = this.denominator * rn2.numerator;
int num = this.numerator * rn2.denominator;
return new RationalNumbers(num,den);
}
public RationalNumbers simplyfy()
{
int num,den;
int gcd1 = gcd(this.numerator,this.denominator);
num = this.numerator / gcd1;
den =this.denominator /gcd1;
return new RationalNumbers(num, den);
}
public int gcd(int num,int den)
{
if(den == 0)
return num;
else
return gcd(den , num % den);
}
public void display()
{
System.out.println(this.numerator + "/" + this.denominator);
}
public RationalNumbers()
{
this.numerator = 0 ;
this.denominator = 1;
}
private int numerator,denominator;
public static void main(String[] args)
{
int choice;
boolean flag;
Scanner scan = new Scanner(System.in);
RationalNumbers rn = new RationalNumbers();
System.out.println("The Values are not changed after performing operations.\n to change values use option 9");
System.out.println("Enter first rational Number : ");
RationalNumbers r1 = new RationalNumbers();
r1.get_input();
System.out.println("Enter second rational Number : ");
RationalNumbers r2 = new RationalNumbers();
r2.get_input();
System.out.println("What Do You Want to do ");
System.out.println("1) Take Reciprocal");
System.out.println("2) Addition");
System.out.println("3) Subtraction");
System.out.println("4) Multiplication");
System.out.println("5) Division");
System.out.println("6) Negation");
System.out.println("7) Simplify");
System.out.println("8) Print Number");
System.out.println("9) Change Value");
System.out.println("10) Logical Operations");
System.out.println("0) Exit");
System.out.println("\nEnter Choice :: ");
String s = scan.nextLine();
choice = Integer.parseInt(s);
while(choice != 0)
{
switch(choice)
{
case 1:
System.out.println("Do Reciprocal of.");
System.out.println("1) First.");
System.out.println("2) Second.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
rn = r1.reciprocal();
else if(choice == 2)
rn = r2.reciprocal();
break;
case 2:
rn =r1.addition(r2);
break;
case 3:
System.out.println("1) Subtract First num from Second.");
System.out.println("2) Subtract Second num from First.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
rn = r2.subtract(r1);
else if(choice == 2)
rn = r1.subtract(r2);
break;
case 4:
rn = r1.multiply(r2);
break;
case 5:
System.out.println("1) Divide First num By Second.");
System.out.println("2) Divide Second num By First.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
rn = r1.divide(r2);
else if(choice == 2)
rn = r2.divide(r1);
break;
case 6:
System.out.println("Do negation of.");
System.out.println("1) First.");
System.out.println("2) Second.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
rn = r1.negate();
else if(choice == 2)
rn = r2.negate();
break;
case 7:
System.out.println("Do Simplification of.");
System.out.println("1) First Number.");
System.out.println("2) Second Number.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
rn = r1.simplyfy();
else if(choice == 2)
rn = r2.simplyfy();
break;
case 8:
System.out.println("Display.");
System.out.println("1) First Number.");
System.out.println("2) Second Number.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
r1.display();
else if(choice == 2)
r2.display();
break;
case 9:
System.out.println("Change Value Of.");
System.out.println("1) First Number.");
System.out.println("2) Second Number.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice != 1 && choice != 2)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
if(choice == 1)
r1.get_input();
else if(choice == 2)
r2.get_input();
break;
case 10:
System.out.println("Check.");
System.out.println("1) Is First Number Greater.");
System.out.println("2) Is First Number Greater or Equal.");
System.out.println("3) Is First Number Equal to Second.");
System.out.println("4) Is First Number Not Equal to Second.");
System.out.println("5) Is First Number Lesser or Equal.");
System.out.println("6) Is First Number Lesser.");
s = scan.nextLine();
choice = Integer.parseInt(s);
while (choice <=0 && choice >= 7)
{
System.out.println("Invalid choice.\nEnter Again :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
switch(choice)
{
case 1:
if(r1.IsGreater(r2))
{
System.out.println("First number is greater than second number");
}
else
System.out.println("First number is Not greater than second number");
break;
case 2:
if(r1.IsGreaterEqual(r2))
{
System.out.println("First number is greater or equal to second number");
}
else
System.out.println("First number is Not greater or equal to second number");
break;
case 3:
if(r1.IsEqual(r2))
{
System.out.println("First number is equal to second number");
}
else
System.out.println("First number is Not equal to second number");
break;
case 4:
if(r1.IsNotEqual(r2))
{
System.out.println("First number is Not Equal to second number");
}
else
System.out.println("First number is Equal to second number");
break;
case 5:
if(r1.IsLess(r2))
{
System.out.println("First number is Less Than second number");
}
else
System.out.println("First number is Not Less Than second number");
break;
case 6:
if(r1.IsLessEqual(r2))
{
System.out.println("First number is Less Than or Equal to second number");
}
else
System.out.println("First number is Not Less Than or Equal to second number");
break;
}
break;
case 0:
choice = 0 ;
break;
default:
System.out.println("ERROR!! Invalid Choice.\n");
}
if(choice!=0)
{
rn.display();
System.out.println("What Do You Want to do ");
System.out.println("1) Take Reciprocal");
System.out.println("2) Addition");
System.out.println("3) Subtraction");
System.out.println("4) Multiplication");
System.out.println("5) Division");
System.out.println("6) Negation");
System.out.println("7) Simplify");
System.out.println("8) Print Number");
System.out.println("9) Change Value");
System.out.println("10) Logical Operations");
System.out.println("0) Exit");
System.out.println("\nEnter Choice :: ");
s = scan.nextLine();
choice = Integer.parseInt(s);
}
}
}
};
Comments
Post a Comment