Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // By: Sammy Samkough
- // Rational
- // Spec: to create a menu driven client program that utilizes the Rational Class
- public class Rational
- {
- private int numerator, denominator;
- // Sets up the rational number by ensuring a nonzero denominator and making only the number signed
- public Rational(int n, int d)
- {
- if (d == 0)
- {
- d = 1;
- }
- // Make the numerator "store" the sign
- if (d < 0)
- {
- n = n * -1;
- d = d * -1;
- }
- numerator = n;
- denominator = d;
- reduce();
- }
- // sets the fraction to 1/1
- public Rational()
- {
- numerator = 1;
- denominator = 1;
- }
- // returns the numerator of this rational number
- public int getNumerator()
- {
- return numerator;
- }
- // sets the numerator of this rational number
- public void setNumerator(int n)
- {
- numerator = n;
- }
- // returns the denominator of this rational number
- public int getDenominator()
- {
- return denominator;
- }
- // sets the denominator of this rational number
- public void setDenominator(int d)
- {
- denominator = d;
- }
- // sets the rational number
- public void setRational(int n, int d)
- {
- numerator = n;
- denominator = d;
- }
- // returns the reciprocal
- public Rational reciprocal()
- {
- return new Rational (denominator, numerator);
- }
- // adds the rational number to the one passed as a parameter. A common denominator is found by multiplying the individual
- // denominators
- public Rational add (Rational op2)
- {
- Rational result;
- int cd = denominator * op2.getDenominator();
- int n1 = numerator * op2.getDenominator();
- int n2 = op2.getNumerator() * denominator;
- int sum = n1 + n2;
- result = new Rational (sum, cd);
- result.reduce();
- return result;
- }
- // subtacts the rational number passed as a parameter from this rational number
- public Rational subtract (Rational op2)
- {
- Rational result;
- int cd = denominator * op2.getDenominator();
- int n1 = numerator * op2.getDenominator();
- int n2 = op2.getNumerator() * denominator;
- int difference = n1 - n2;
- result = new Rational (difference, cd);
- result.reduce();
- return result;
- }
- // multiplies this rational number by the one assed as a parameter
- public Rational multiply (Rational op2)
- {
- Rational result;
- int n = numerator * op2.getNumerator();
- int d = denominator * op2.getDenominator();
- result = new Rational (n, d);
- result.reduce();
- return result;
- }
- // divides this rational number by the one passed as a paremeter by multiplying by the recirprocal of the second rational
- public Rational divide (Rational op2)
- {
- return multiply (op2.reciprocal());
- }
- // determines if this rational number is equal to the one passed as a parameter. Assumes they are both reduced
- public boolean equals (Rational op2)
- {
- return (numerator == op2.getNumerator() && denominator == op2.getDenominator());
- }
- // returns this rational number as a string
- public String toString()
- {
- String result;
- if (numerator == 0)
- {
- result = "0";
- }
- else
- {
- if (denominator == 1)
- {
- result = numerator + "";
- }
- else
- {
- result = numerator + "/" + denominator;
- }
- }
- return result;
- }
- // reduces this rational number by dividing both the numerator and the denominator by their greatest common divisor
- private void reduce()
- {
- if (numerator != 0)
- {
- int common = gcd(Math.abs(numerator), denominator);
- numerator = numerator / common;
- denominator = denominator / common;
- }
- }
- // computes and returns the greatest common divisor of the two positive parameters. Use Euclid's algorithm
- private int gcd (int n1, int n2)
- {
- while (n1 != n2)
- {
- if (n1 > n2)
- {
- n1 = n1 - n2;
- }
- else
- {
- n2 = n2 - 1;
- }
- }
- return n1;
- }
- // calculates the exponent of this Rational and returns it as a double
- public double powDoub(Rational exp)
- {
- double a = exp.getNumerator() / exp.getDenominator();
- return (Math.pow(numerator, a) / Math.pow(denominator, a));
- }
- // caluculates the exponent of this Rational and returns it as a Rational
- public Rational powRat(int exp)
- {
- Rational result = new Rational();
- numerator = (int)Math.pow(numerator, exp);
- denominator = (int)Math.pow(denominator, exp);
- result.setRational(numerator, denominator);
- return result;
- }
- }
- -----------------------------------------------------------------------------------------------------------------------------
- // By: Sammy Samkough
- // Rational
- // Spec: to create a menu driven client program that utilizes the Rational Class
- import java.util.Scanner;
- public class RationalClient
- {
- public static void main(String args[])
- {
- Rational r1 = new Rational();
- Rational r2 = new Rational();
- Scanner sc = new Scanner(System.in);
- // we create this so we can use getChoice()
- int choice;
- int n, d;
- boolean running = true;
- // prompting the users
- System.out.println("* Hello and welcome to the Rational Program!");
- // telling the users to first put the numerator and then the denominator
- System.out.println("* When entering fractions, first put the numerator and then the denominator!");
- // explaining how to exit
- System.out.println("* And if you want to quit, just put in 8!");
- System.out.println("________________________________________________________________________________");
- // we initialize it to getChoice() so we can use it
- choice = getChoice();
- // if we they enter in
- while(running)
- {
- // entering the fractions
- if(choice == 1)
- {
- System.out.println("Please enter your 1st fraction (numerator then denominator):");
- System.out.print("Numerator: ");
- n = sc.nextInt();
- System.out.print("Denominator: ");
- d = sc.nextInt();
- r1.setRational(n, d);
- System.out.println("");
- System.out.println("Please enter your 2nd fraction (numerator then denominator):");
- System.out.print("Numerator: ");
- n = sc.nextInt();
- System.out.print("Denominator: ");
- d = sc.nextInt();
- r2.setRational(n, d);
- System.out.println("What would you like to do next (on the menu)?");
- choice = sc.nextInt();
- }
- // displaying the fractions
- else if (choice == 2)
- {
- System.out.print("Your 1st fraction is: " + r1 + "\n");
- System.out.print("Your 2nd fraction is: " + r2 + "\n");
- System.out.println("What would you like to do next (on the menu)?");
- choice = sc.nextInt();
- }
- // adding the fractions
- else if (choice == 3)
- {
- System.out.println("The sum of the fractions are: " + r1.add(r2));
- System.out.println("What would you like to do next (on the menu)?");
- choice = sc.nextInt();
- }
- // subtracting the fractions
- else if (choice == 4)
- {
- System.out.println("The difference of the fractions are: " + r1.subtract(r2));
- System.out.println("What would you like to do next (on the menu)?");
- choice = sc.nextInt();
- }
- // multiply the fractions
- else if (choice == 5)
- {
- System.out.println("The product of the fractions are: " + r1.multiply(r2));
- System.out.println("What would you like to do next (on the menu)?");
- choice = sc.nextInt();
- }
- // dividing the fractions
- else if (choice == 6)
- {
- System.out.println("The quotient of the fractions are: " + r1.divide(r2));
- System.out.println("What would you like to do next (on the menu)?");
- choice = sc.nextInt();
- }
- // powering the fractions
- else if (choice == 7)
- {
- System.out.println("The power double of the fractions are: " + r1.powDoub(r2));
- System.out.println("What would you like the power to be for the power rational?");
- int p = sc.nextInt();
- System.out.println("The power rational of the fractions are: " + r1.powRat(p));
- System.out.println("What would you like to do next (on the menu)?");
- choice = sc.nextInt();
- }
- // goodbye message
- else if (choice == 8)
- {
- System.out.println("Bye bye!");
- running = false;
- }
- // just in case they put something besides 1-8
- else
- {
- System.out.println("Please enter a number from 1-8.");
- choice = sc.nextInt();
- }
- }
- }
- public static int getChoice()
- {
- int choice;
- Scanner sc = new Scanner(System.in);
- System.out.println("1. Input Fractions");
- System.out.println("2. Display Fractions");
- System.out.println("3. Add Fractions");
- System.out.println("4. Subtract Fractions");
- System.out.println("5. Multiply Fractions");
- System.out.println("6. Divide Fractions");
- System.out.println("7. Power (A^B)");
- System.out.println("8. Quit");
- System.out.print("Enter choice: ");
- choice = sc.nextInt();
- return choice;
- }
- }
- /*
- * Hello and welcome to the Rational Program!
- * When entering fractions, first put the numerator and then the denominator!
- * And if you want to quit, just put in 8!
- ________________________________________________________________________________
- 1. Input Fractions
- 2. Display Fractions
- 3. Add Fractions
- 4. Subtract Fractions
- 5. Multiply Fractions
- 6. Divide Fractions
- 7. Power (A^B)
- 8. Quit
- Enter choice: 1
- Please enter your 1st fraction (numerator then denominator):
- Numerator: 1
- Denominator: 4
- Please enter your 2nd fraction (numerator then denominator):
- Numerator: 5
- Denominator: 8
- What would you like to do next (on the menu)?
- 2
- Your 1st fraction is: 1/4
- Your 2nd fraction is: 5/8
- What would you like to do next (on the menu)?
- 3
- The sum of the fractions are: 1
- What would you like to do next (on the menu)?
- 4
- The difference of the fractions are: -1/2
- What would you like to do next (on the menu)?
- 5
- The product of the fractions are: 1/6
- What would you like to do next (on the menu)?
- 6
- The quotient of the fractions are: 1/2
- What would you like to do next (on the menu)?
- 7
- The power double of the fractions are: 1.0
- What would you like the power to be for the power rational?
- 8
- The power rational of the fractions are: 1/65536
- What would you like to do next (on the menu)?
- 8
- Bye bye!
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement