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++ 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...

  • 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...

  • 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...

  • 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...

  • 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...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

  • In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

    In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator;    public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • I have provided a main method. Please add your fraction class. You need constructors, add, subtract,...

    I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your toString method needs to return the numerator followed by / followed by the denominator - NO spaces. DO NOT make any attempt to reduce the fractions (we shall do that later). Please add comments throughout. import java.util.Scanner; public class H4 { public static class Fraction { } public static void main(String[] args) { Fraction f=...

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
ADVERTISEMENT