Question

Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations. Some, I completed.

Upon finishing MixedRational use the main method provided to test your class. Then copy your Executable and MixedRational class and turn this in through Schoology. We need to get going on this project right away. Put the games and chatting aside until the project is done!

Main Method:

public static void main(String[] args) {

    //MixedRational [] mix = new MixedRational[4];   

    MixedRational mix0 = new MixedRational(-1,6,5);

    MixedRational mix1 = new MixedRational (2,3,4);

    MixedRational mix2 = new MixedRational (-2,1, 3);

    MixedRational mix3 = new MixedRational (3,1,4);

    System.out.println("Was the constructor properly made?");

    System.out.println(mix0);

    System.out.println("Checking out the add, sub, mult, and div methods");

    System.out.println(mix1.subtract(mix1));

    System.out.println(mix0.subtract(mix3));

    System.out.println(mix0.multiply(mix2));

    System.out.println(mix3.divide(mix2));

    System.out.println(mix1.add(mix2));

   

    }

Rational Class:

public class Rational {

private int numerator, denominator;

   //-----------------------------------------------------------------

   // Sets up the rational number by ensuring a nonzero denominator

   // and making only the numerator signed.

   //-----------------------------------------------------------------

   public Rational (int numer, int denom)

   {

      if (denom == 0)

      denom = 1;

      // Make the numerator "store" the sign

      if (denom < 0)

      {

      numer = numer * -1;

      denom = denom * -1;

      }

      numerator = numer;

      denominator = denom;

reduce();

}

   //-----------------------------------------------------------------

   // Returns the numerator of this rational number.

   //-----------------------------------------------------------------

   public int getNumerator ()

   {

      return numerator;

   }

   //-----------------------------------------------------------------

   // Returns the denominator of this rational number.

   //-----------------------------------------------------------------

   public int getDenominator ()

   {

      return denominator;

   }

   //-----------------------------------------------------------------

   // Returns the reciprocal of this rational number.

   //-----------------------------------------------------------------

   public Rational reciprocal ()

   {

      return new Rational (denominator, numerator);

   }

   //-----------------------------------------------------------------

   // Adds this rational number to the one passed as a parameter.

   // A common denominator is found by multiplying the individual

   // denominators.

   //-----------------------------------------------------------------

   public Rational add (Rational op2)

   {

      int commonDenominator = denominator * op2.getDenominator();

      int numerator1 = numerator * op2.getDenominator();

      int numerator2 = op2.getNumerator() * denominator;

      int sum = numerator1 + numerator2;

      return new Rational (sum, commonDenominator);

   }

   //-----------------------------------------------------------------

   // Subtracts the rational number passed as a parameter from this

   // rational number.

   //-----------------------------------------------------------------

   public Rational subtract (Rational op2)

   {

      int commonDenominator = denominator * op2.getDenominator();

int numerator1 = numerator * op2.getDenominator();

      int numerator2 = op2.getNumerator() * denominator;

      int difference = numerator1 - numerator2;

      return new Rational (difference, commonDenominator);

   }

   //-----------------------------------------------------------------

   // Multiplies this rational number by the one passed as a

   // parameter.

   //-----------------------------------------------------------------

   public Rational multiply (Rational op2)

   {

      int numer = numerator * op2.getNumerator();

      int denom = denominator * op2.getDenominator();

      return new Rational (numer, denom);

   }

   //-----------------------------------------------------------------

   // Divides this rational number by the one passed as a parameter

   // by multiplying by the reciprocal 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. Uses Euclid's algorithm.

   //-----------------------------------------------------------------

   private int gcd (int num1, int num2)

   {

      while (num1 != num2)

         if (num1 > num2)

            num1 = num1 - num2;

         else

            num2 = num2 - num1;

      return num1;

   }   

   

}

Mixed Rational Class:

public class MixedRational extends Rational {

       

       private int whole;

   

    //My constructor! We will use the constructor from Rational to consruct

       //some of the object..... This constructor

       //should fix issues as in 1-5/4 to be 2-1/4!!!!! Enjoy!

      

    public MixedRational(int w, int n, int d)

    {

        super(n%d, d); //super refers to the parent class or what some call the superclass!

        if(w < 0)

              whole = -1*(-1*w + n/d);

        else

              whole = w + n/d;

    }

   

   

    //This method Changes an MixedRational to a Rational and Returns it

    public Rational toImproper()

    {  

    }

   

    //This Static method changes a rational number to MixedRational to return

   

    public static MixedRational toMixed(Rational val)

    {

       

    }

   

    //This method adds two MixedRational's and returns a MixedRational

   

public MixedRational add(MixedRational m2)

    {

       

    }

    //This method Mult. two MixedRational's and returns a MixedRational

   

public MixedRational multiply(MixedRational m2)

{

}

    //This method adds two MixedRational's and returns a MixedRational

public MixedRational subtract(MixedRational m2)

{

}

    //This method divides two MixedRational's and returns a MixedRational

   

public MixedRational divide(MixedRational m2)

{

}

       

   //Write a toString method that will return a string as the following.

    //    2-3/4

   

    public String toString ()

    {       

    }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Rational {

        private int numerator, denominator;

        // -----------------------------------------------------------------

        // Sets up the rational number by ensuring a nonzero denominator

        // and making only the numerator signed.

        // -----------------------------------------------------------------

        public Rational(int numer, int denom)

        {

                if (denom == 0)

                        denom = 1;

                // Make the numerator "store" the sign

                if (denom < 0)

                {

                        numer = numer * -1;

                        denom = denom * -1;

                }

                numerator = numer;

                denominator = denom;

                reduce();

        }

        // -----------------------------------------------------------------

        // Returns the numerator of this rational number.

        // -----------------------------------------------------------------

        public int getNumerator()

        {

                return numerator;

        }

        // -----------------------------------------------------------------

        // Returns the denominator of this rational number.

        // -----------------------------------------------------------------

        public int getDenominator()

        {

                return denominator;

        }

        // -----------------------------------------------------------------

        // Returns the reciprocal of this rational number.

        // -----------------------------------------------------------------

        public Rational reciprocal()

        {

                return new Rational(denominator, numerator);

        }

        // -----------------------------------------------------------------

        // Adds this rational number to the one passed as a parameter.

        // A common denominator is found by multiplying the individual

        // denominators.

        // -----------------------------------------------------------------

        public Rational add(Rational op2)

        {

                int commonDenominator = denominator * op2.getDenominator();

                int numerator1 = numerator * op2.getDenominator();

                int numerator2 = op2.getNumerator() * denominator;

                int sum = numerator1 + numerator2;

                return new Rational(sum, commonDenominator);

        }

        // -----------------------------------------------------------------

        // Subtracts the rational number passed as a parameter from this

        // rational number.

        // -----------------------------------------------------------------

        public Rational subtract(Rational op2)

        {

                int commonDenominator = denominator * op2.getDenominator();

                int numerator1 = numerator * op2.getDenominator();

                int numerator2 = op2.getNumerator() * denominator;

                int difference = numerator1 - numerator2;

                return new Rational(difference, commonDenominator);

        }

        // -----------------------------------------------------------------

        // Multiplies this rational number by the one passed as a

        // parameter.

        // -----------------------------------------------------------------

        public Rational multiply(Rational op2)

        {

                int numer = numerator * op2.getNumerator();

                int denom = denominator * op2.getDenominator();

                return new Rational(numer, denom);

        }

        // -----------------------------------------------------------------

        // Divides this rational number by the one passed as a parameter

        // by multiplying by the reciprocal 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. Uses Euclid's algorithm.

        // -----------------------------------------------------------------

        private int gcd(int num1, int num2)

        {

                while (num1 != num2)

                        if (num1 > num2)

                                num1 = num1 - num2;

                        else

                                num2 = num2 - num1;

                return num1;

        }

}

public class MixedRational extends Rational {

        private int whole;

        // My constructor! We will use the constructor from Rational to consruct
        // some of the object..... This constructor
        // should fix issues as in 1-5/4 to be 2-1/4!!!!! Enjoy!
        public MixedRational(int w, int n, int d)
        {

                super(n % d, d); // super refers to the parent class or what some call the superclass!
                if (w < 0)
                        whole = -1 * (-1 * w + n / d);
                else
                        whole = w + n / d;
        }

        // This method Changes an MixedRational to a Rational and Returns it

        public Rational toImproper() {
                return new Rational(super.getNumerator() + whole * super.getDenominator(), super.getDenominator());
        }

        // This Static method changes a rational number to MixedRational to return
        public static MixedRational toMixed(Rational val) {
                int n = val.getNumerator();
                int d = val.getDenominator();
                int w = 0;
                if(Math.abs(n) > d) {
                        w = Math.abs(n / d);
                        n = n - w*d;
                }
                //System.out.println(val.getNumerator()
                                //+ "," + val.getDenominator() + " " + val + " to mixed: " + new MixedRational(w, n, d));
                return new MixedRational(w, n, d);
        }

        // This method adds two MixedRational's and returns a MixedRational
        public MixedRational add(MixedRational m2)      {
                return toMixed(m2.toImproper().add(this.toImproper()));
        }

        // This method Mult. two MixedRational's and returns a MixedRational

        public MixedRational multiply(MixedRational m2) {
                return toMixed(m2.toImproper().multiply(this.toImproper()));

        }

        // This method adds two MixedRational's and returns a MixedRational
        public MixedRational subtract(MixedRational m2) {
                return toMixed(this.toImproper().subtract(m2.toImproper()));

        }

        // This method divides two MixedRational's and returns a MixedRational

        public MixedRational divide(MixedRational m2) {
                return toMixed(this.toImproper().divide(m2.toImproper()));
        }

//Write a toString method that will return a string as the following.

        // 2-3/4

        public String toString() {
                if(whole == 0) {
                        return super.toString();
                }
                String s = whole + "";
                if(getNumerator() != 0) {
                        s += " " + super.toString();
                }
                return s;
        }

        public static void main(String[] args) {

                // MixedRational [] mix = new MixedRational[4];

                MixedRational mix0 = new MixedRational(-1, 6, 5);
                MixedRational mix1 = new MixedRational(2, 3, 4);
                MixedRational mix2 = new MixedRational(-2, 1, 3);
                MixedRational mix3 = new MixedRational(3, 1, 4);

                System.out.println("Was the constructor properly made?");

                System.out.println(mix0);

                System.out.println("Checking out the add, sub, mult, and div methods");

                System.out.println(mix1.subtract(mix1));

                System.out.println(mix0.subtract(mix3));

                System.out.println(mix0.multiply(mix2));

                System.out.println(mix3.divide(mix2));

                System.out.println(mix1.add(mix2));

        }
}

Add a comment
Know the answer?
Add Answer to:
Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ I am using visual studio for it. Make a copy of the Rational class you...

    C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...

  • c++ Write a rational number class. A rational number is a number that can be written...

    c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...

  • public class Rational { // PUT PRIVATE DATA FIELDS HERE /** * The default constructor for...

    public class Rational { // PUT PRIVATE DATA FIELDS HERE /** * The default constructor for objects of class Rational. Creates the rational number 1. */ public Rational() { // ADD CODE TO THE CONSTRUCTOR } /** * The alternate constructor for objects of class Rational. Creates a rational number equivalent to n/d. * @param n The numerator of the rational number. * @param d The denominator of the rational number. */ public Rational(int n, int d) { // ADD...

  • Adapt your Rational class : public class Rational { private int num; private int denom; public...

    Adapt your Rational class : public class Rational { private int num; private int denom; public Rational() { num = 0; denom = 1; } public Rational(int num, int denom) { this.num = num; this.denom = denom; } int getNum() { return num; } int getDenom() { return denom; } public Rational add(Rational rhs) { return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom); } public Rational subtract(Rational rhs) { return new Rational(num * rhs.denom - rhs.num...

  • Create a fraction class. This will have two attributes, a numerator and a denominator, both int....

    Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...

  • When we test your Fraction.java we will use the given FractionTester.java file. Your Fraction.java file -MUST-...

    When we test your Fraction.java we will use the given FractionTester.java file. Your Fraction.java file -MUST- work perfectly with the supplied FractionTester.java file. You may NOT modify the FractionTester to make your Fraction class work right. Starter Fraction File: Fraction.java ADD METHODS TO THIS FILE. HAND IN THIS FILE ONLY. Given/Completed Tester File: FractionTester.java DO NOT MODIFY. DO NOT HAND IN. You are to add the following methods to the given Fraction.java file public Fraction add( Fraction other) returns a...

  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • Rational Number *In Java* A rational number is one that can be expressed as the ratio...

    Rational Number *In Java* A rational number is one that can be expressed as the ratio of two integers, i.e., a number that can be expressed using a fraction whose numerator and denominator are integers. Examples of rational numbers are 1/2, 3/4 and 2/1. Rational numbers are thus no more than the fractions you've been familiar with since grade school. Rational numbers can be negated, inverted, added, subtracted, multiplied, and divided in the usual manner: The inverse, or reciprocal of...

  • given the following two classes Within a file named Rational.java, define a public class named Rational...

    given the following two classes Within a file named Rational.java, define a public class named Rational such that … the Rational class has two private instance variables, numerator and denominator, both of type int the Rational class defines two public accessor methods, numerator() and denominator() the Rational class defines a constructor that has one String parameter a throws clause indicates that this constructor could potentially throw a MalformedRationalException this constructor throws a MalformedRationalException in the following circumstances … When the...

  • Give answer according to fill in the blanks. 1.Define a class called Fraction . This class...

    Give answer according to fill in the blanks. 1.Define a class called Fraction . This class is used to represent a ratio of two integers.Include mutator methods that allow the user to set the numerator and the denominator. Also include a method that returns the value of numerator divided by denominator as a double . Include an additional method that outputs the value of the fraction reduced to lowest terms (e.g., instead of outputting 20/60, the method should output 1/3)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT