Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Class description of the class Rational
- * The class Rational allows the user to do basic operations between two fractions
- *
- * @author Ryan Norng
- * @version version 1.0
- * @since 2020-01-26*/
- public class Rational
- {
- private int numerator;
- private int denominator;
- /**
- * Default constructor. Creates a Rational object with a numerator and denominator of 1*/
- public Rational()
- {
- numerator = 1;
- denominator = 1;
- }
- /**
- * Constructor. Creates a Rational object with specified numerator and denominator
- * @param num An integer specifying the numerator
- * @param den An integer specifying the denominator*/
- public Rational(int num, int den)
- {
- numerator = num;
- denominator = den;
- }
- /** Gets the Rational object's numerator
- * @return Returns the integer representing the numerator*/
- public int getNumerator()
- {
- return numerator;
- }
- /** Gets the Rational object's denominator
- * @return Returns the integer representing the denominator*/
- public int getDenominator()
- {
- return denominator;
- }
- /** Sets the Rational object's numerator
- * @param value An integer representing the value to set the numerator to */
- public void setNumerator(int value)
- {
- numerator = value;
- }
- /** Sets the Rational object's denominator
- * @param value An integer representing the value to set the denominator to*/
- public void setDenominator(int value)
- {
- denominator = value;
- }
- /** Allows us to print the Rational object as a String
- * @return A String of the Rational object in the format of numerator/denominator*/
- public String toString()
- {
- String numstring = String.valueOf(numerator);
- String denstring = String.valueOf(denominator);
- return numstring + "/" + denstring;
- }
- /** Asks the user for inputs to set values of a Rational object. */
- public void inputRational()
- {
- Scanner ui = new Scanner(System.in);
- int userNum = 1;
- int userDen = 1;
- boolean numflag = false;
- boolean denflag = false;
- //input validation for non-zero integers (numerator)
- do // only allows int as an input
- {
- System.out.println("Please enter the numerator.");
- while (!ui.hasNextInt())
- {
- System.out.println("Invalid input. Please enter an integer.");
- ui.next();
- }
- userNum = ui.nextInt();
- numflag = true;
- }
- while (numflag == false || userNum == 0);
- //input validation for non-zero integers (denominator)
- do // only allows int as an input
- {
- System.out.println("Please enter the denominator.");
- while (!ui.hasNextInt())
- {
- System.out.println("Invalid input. Please enter an integer.");
- ui.next();
- }
- userDen = ui.nextInt();
- denflag = true;
- }
- while (denflag == false || userDen == 0);
- // ui.close();
- int gcd = 1;
- gcd = gcd(userNum,userDen);
- numerator = userNum / gcd;
- denominator = userDen / gcd;
- }
- /** Finds gcd of two integers. Used to simply fractions of Rational objects
- * @return An integer representing the gcd of two integers*/
- private int gcd(int m, int n)
- {
- int r;
- while(n!=0)
- {
- r = m % n;
- m = n;
- n = r;
- }
- return m;
- }
- /** Adds two Rational objects
- * @param r1 A Rational object
- * @param r2 A Rational object*/
- public void add(Rational r1, Rational r2)
- {
- int gcd, tnum, tden;
- tnum = (r1.numerator * r2.denominator) + (r1.denominator * r2.numerator);
- tden = r1.denominator * r2.denominator;
- gcd = gcd(tnum,tden);
- numerator = tnum / gcd;
- denominator = tden / gcd;
- }
- /** Subtracts a Rational object from another Rational object, and returns a Rational object
- * @param r A Rational object (the value being subtracted)
- * @return The Rational resulting from the subtraction*/
- public Rational sub(Rational r)
- {
- Rational tRational = new Rational();
- int gcd, tnum, tden;
- tnum = (numerator * r.denominator) - (denominator * r.numerator);
- tden = denominator * r.denominator;
- gcd = gcd(tnum,tden);
- tRational.numerator = tnum / gcd;
- tRational.denominator = tden / gcd;
- return tRational;
- }
- /** Multiplies two Rational objects
- * @param r1 A Rational object
- * @param r2 A Rational object*/
- public void mul(Rational r1, Rational r2)
- {
- int gcd, tnum, tden;
- tnum = r1.numerator * r2.numerator;
- tden = r1.denominator * r2.denominator;
- gcd = gcd(tnum,tden);
- numerator = tnum / gcd;
- denominator = tden / gcd;
- }
- /** Divides a Rational object from another Rational object, and returns a Rational object
- * @param r A Rational object (the value used for division)
- * @return The Rational resulting from the division*/
- public Rational div(Rational r)
- {
- Rational tRational = new Rational();
- int gcd, tnum, tden;
- tnum = numerator * r.denominator;
- tden = denominator * r.numerator;
- gcd = gcd(tnum,tden);
- tRational.numerator = tnum / gcd;
- tRational.denominator = tden / gcd;
- return tRational;
- }
- /** Performs division between two Rational objects, and returns a decimal value as a double
- * @param r1 A Rational object
- * @param r2 a Rational object
- * @return A double representing the resulting value of the division between two Rational objects*/
- public static double divToDouble(Rational r1, Rational r2)
- {
- double tnum, tden;
- double realValue;
- tnum = r1.numerator * r2.denominator;
- tden = r1.denominator * r2.numerator;
- realValue = tnum / tden;
- return realValue;
- }
- }
- public class CECS277Lab1 {
- public static void main(String[] args)
- {
- Rational r1 = new Rational();
- Rational r2 = new Rational();
- Rational r3 = new Rational();
- r1.inputRational();
- r2.inputRational();
- r3.add(r1, r2);
- System.out.println(r1 + " + " + r2 +" = " + r3);
- r3 = r1.sub(r2);
- System.out.println(r1 + " - " + r2 +" = " + r3);
- r3.mul(r1, r2);;
- System.out.println(r1 + " * " + r2 +" = " + r3);
- r3 = r1.div(r2);
- System.out.println(r1 + " / " + r2 +" = " + r3);
- double d3 = Rational.divToDouble(r1, r2);
- System.out.println(r1 + " + " + r2 +" = " + d3);
- r1.setNumerator(2);
- System.out.println("The numerator of Rational r1 has been set to 2");
- r2.setDenominator(5);
- System.out.println("The denominator of Rational r2 has been set to 5");
- System.out.println("Numerator: " + r1.getNumerator());
- System.out.println("Denominator: " + r2.getDenominator());
- //System.out.println(r3);
- //System.out.print(d1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement