Question

In: Computer Science

Objective: Upon completion of this program, you should gain experience with overloading basic operators for use...

Objective: Upon completion of this program, you should gain experience with overloading basic operators for use with a C++ class.

The code for this assignment should be portable -- make sure you test with g++ on linprog.cs.fsu.edu before you submit.

Task

Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files "mixed.h" and "mixed.cpp".

Details and Requirements

  1. Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 -- "three and one-half"). The Mixed class must allow for both positive and negative mixed number values. A zero in the denominator of the fraction part constitutes an illegal number and should not be allowed. You should create appropriate member data in your class. All member data must be private.
  2. There should be two constructors. One constructor should take in three parameters, representing the integer part, the numerator, and the denominator (in that order), used to initialize the object. If the mixed number is to be a negative number, the negative should be passed on the first non-zero parameter, but on no others. If the data passed in is invalid (negatives not fitting the rule, or 0 denominator), then simply set the object to represent the value 0. Examples of declarations of objects:
     Mixed m1(3, 4, 5);    // sets object to 3 4/5 
     Mixed m2(-4, 1, 2);   // sets object to -4 1/2 
     Mixed m3(0, -3, 5);   // sets object to -3/5 (integer part is 0). 
     Mixed m4(-1, -2, 4);  // bad parameter combination.  Set object to 0. 
    

    The other constructor should expect a single int parameter with a default value of 0 (so that it also acts as a default constructor). This constructor allows an integer to be passed in and represented as a Mixed object. This means that there is no fractional part. Example declarations:

     Mixed m5(4);  // sets object to 4 (i.e. 4 and no fractional part). 
     Mixed m6;     // sets object to 0 (default)
    
    Note that this last constructor will act as a "conversion constructor", allowing automatic type conversions from type int to type Mixed.
  3. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return a double, the others don't return anything. These functions have no parameters. The names must match the ones here exactly. They should do the following:
    • The Evaluate function should return the decimal equivalent of the mixed number.
    • The Simplify function should simplify the mixed number representation to lowest terms. This means that the fraction part should be reduced to lowest terms, and the fraction part should not be an improper fraction (i.e. disregarding any negative signs, the numerator is smaller than the denominator).
    • The ToFraction function should convert the mixed number into fraction form. (This means that the integer part is zero, and the fraction portion may be an improper fraction).
  4. Create an overload of the extraction operator >> for reading mixed numbers from an input stream. The input format for a Mixed number object will be:
     integer numerator/denominator 
    

    i.e. the integer part, a space, and the fraction part (in numerator/denominator form), where the integer, numerator, and denominator parts are all of type int. You may assume that this will always be the format that is entered (i.e. your function does not have to handle entry of incorrect types that would violate this format). However, this function should check the values that come in. In the case of an incorrect entry, just set the Mixed object to represent the number 0, as a default. An incorrect entry occurs if a denominator value of 0 is entered, or if an improper placement of a negative sign is used. Valid entry of a negative number should follow this rule -- if the integer part is non-zero, the negative sign is entered on the integer part; if the integer part is 0, the negative sign is entered on the numerator part (and therefore the negative sign should never be in the denominator). Examples:

     Valid inputs:     2 7/3 , -5 2/7  , 4 0/7  , 0 2/5  , 0 -8/3 
     Invalid inputs:   2 4/0 , -2 -4/5 , 3 -6/3 , 0 2/-3 
    
  5. Create an overload of the insertion operator << for output of Mixed numbers. This should output the mixed number in the same format as above, with the following exceptions: If the object represents a 0, then just display a 0. Otherwise: If the integer part is 0, do not display it. If the fraction part equals 0, do not display it. For negative numbers, the minus sign is always displayed to the left.
     Examples:   0  ,  2  ,  -5  ,  3/4  ,  -6/7  ,  -2 4/5  ,  7 2/3 
    
  6. Create overloads for all 6 of the comparison operators ( < , > , <= , >= , == , != ). Each of these operations should test two objects of type Mixed and return an indication of true or false. You are testing the Mixed numbers for order and/or equality based on the usual meaning of order and equality for numbers. (These functions should not do comparisons by converting the Mixed numbers to decimals -- this could produce round-off errors and may not be completely accurate).
  7. Create operator overloads for the 4 standard arithmetic operations ( + , - , * , / ) , to perform addition, subtraction, multiplication, and division of two mixed numbers. Each of these operators will perform its task on two Mixed objects as operands and will return a Mixed object as a result - using the usual meaning of arithmetic operations on rational numbers. Also, each of these operators should return their result in simplified form. (e.g. return 3 2/3 instead of 3 10/15, for example).
    • In the division operator, if the second operand is 0, this would yield an invalid result. Since we have to return something from the operator, return 0 as a default (even though there is no valid answer in this case). Example:
      Mixed m(1, 2, 3);               // value is 1 2/3
      Mixed z;                        // value is 0
      Mixed r = m / z;                // r is 0 (even though this is not good math)
      
  8. Create overloads for the increment and decrement operators (++ and --). You need to handle both the pre- and post- forms (pre-increment, post-increment, pre-decrement, post-decrement). These operators should have their usual meaning -- increment will add 1 to the Mixed value, decrement will subtract 1. Since this operation involves arithmetic, leave incremented (or decremented) object in simplified form (to be consistent with other arithmetic operations). Example:
      Mixed m1(1, 2, 3);            //  1 2/3
      Mixed m2(2, 1, 2);            //  2 1/2
      cout << m1++;                   //  prints 1 2/3, m1 is now 2 2/3
      cout << ++m1;                   //  prints 3 2/3, m1 is now 3 2/3
      cout << m2--;                   //  prints 2 1/2, m2 is now 1 1/2
      cout << --m2;                   //  prints 1/2  , m2 is now 0 1/2
    
  9. General Requirements
    • As usual, no global variables
    • All member data of the Mixed class must be private
    • Use the const qualifier whenever appropriate
    • The only libraries that may be used in the class files are iostream and iomanip
    • Do not use langauge or library features that are C++11-only
    • Since the only output involved with your class will be in the << overload (and commands to invoke it will come from some main program or other module), your output should match mine exactly when running test programs.

Solutions

Expert Solution

The main program for the given problem is -

#include <iostream>
#include "mixed.h"

using namespace std;

int main()
{

  // demonstrate behavior of the two constructors and the << overload

  Mixed x(3,20,6), y(-4,9,2), m1(0,-35,10), m2(-1,-2,4), m3(4), m4(-11), m5;
  char answer;
  cout << "Initial values: \nx = " << x << "\ny = " << y
       << "\nm1 = " << m1 << "\nm2 = " << m2 << "\nm3 = " << m3 
       << "\nm4 = " << m4 << "\nm5 = " << m5 << "\n\n";

  // Trying Simplify
  x.Simplify();
  m1.Simplify();
  cout << "x simplified: " << x << " and m1 simplified " << m1 << "\n\n";

  // Trying ToFraction
  x.ToFraction();
  y.ToFraction();
  m1.ToFraction();
  cout << "Values as fractions: \nx = " << x << "\ny = " << y
       << "\nm1 = " << m1 << "\n\n";
 
  // demonstrate >> overload

  cout << "Enter first number: ";
  cin >> x;
  cout << "Enter second number: ";
  cin >> y;

  cout << "You entered:\n";
  cout << "  x = " << x << '\n';
  cout << "  y = " << y << '\n';

  // demonstrate comparison overloads
  if (x < y) cout << "(x < y) is TRUE\n";
  if (x > y) cout << "(x > y) is TRUE\n";
  if (x <= y)        cout << "(x <= y) is TRUE\n";
  if (x >= y)        cout << "(x >= y) is TRUE\n";
  if (x == y)   cout << "(x == y) is TRUE\n";
  if (x != y)   cout << "(x != y) is TRUE\n";

  // demonstrating Evaluate
  cout << "\nDecimal equivalent of " << x << " is " << x.Evaluate() << '\n';
  cout << "Decimal equivalent of " << y << " is " << y.Evaluate() << "\n\n";

  // demonstrating necessory arithmetic overloads
  cout << "(x + y) = " << x + y << '\n';
  cout << "(x - y) = " << x - y << '\n';
  cout << "(x * y) = " << x * y << '\n';
  cout << "(x / y) = " << x / y << '\n';

  // demonstrating arithmetic that uses conversion constructor
  //  to convert the integer operand to a Mixed object
  cout << "(x + 10) = " << x + 10 << '\n';
  cout << "(x - 4) = " << x - 4 << '\n';
  cout << "(x * -13) = " << x * -13 << '\n';
  cout << "(x / 7) = " << x / 7 << '\n';

  return 0;
}

FOR SAMPLE RUNNING-

Let initial values: 
x = 3 20/6
y = -4 9/2
m1 = -35/10
m2 = 0
m3 = 4
m4 = -11
m5 = 0

x simplified: 6 1/3 and m1 simplified -3 1/2

Values as fractions: 
x = 19/3
y = -17/2
m1 = -7/2

Enter first number: 1 2/3
Enter second number: 2 3/4
You entered:
  x = 1 2/3
  y = 2 3/4
(x < y) is TRUE
(x <= y) is TRUE
(x != y) is TRUE

Decimal equivalent of 1 2/3 is 1.66667
Decimal equivalent of 2 3/4 is 2.75

(x + y) = 4 5/12
(x - y) = -1 1/12
(x * y) = 4 7/12
(x / y) = 20/33
(x + 10) = 11 2/3
(x - 4) = -2 1/3
(x * -13) = -21 2/3
(x / 7) = 5/21

YOU MAY TRY DIFFERENT SAMPLE RUNS.


Related Solutions

OBJECTIVE Upon completion of this exercise, you will have demonstrated the ability to: Use C# loops...
OBJECTIVE Upon completion of this exercise, you will have demonstrated the ability to: Use C# loops Construct conditions INTRODUCTION Your instructor will assign one of the following projects, each involves the user of loops and the construction of conditions to control them. CODING STANDARDS The following coding standards must be followed when developing your program: An opening documentation at the beginning of the source file describing the purpose, input, process, output, author, last modified date of the program. Write only...
Galen's first program outcome is "Safe, Patient-Centered Care." Upon completion of this program, you should be...
Galen's first program outcome is "Safe, Patient-Centered Care." Upon completion of this program, you should be competent and able to deliver safe, patient-centered care to all your patients. Every course you take in this program should help you towards those goals. Please address the questions below: What did you learn in Microbiology that will help you deliver safe, patient-centered care? Are there any patient care behaviors that you have changed in your nursing practice this quarter as a result of...
Objective The objective of this lab exercise will be for the student to gain experience conducting...
Objective The objective of this lab exercise will be for the student to gain experience conducting a wireless networking site survey. The objective is for each group to propose a wireless networking solution for a given space (such as a classroom, office rooms, home, etc.). Verify that you can establish link (by pinging) with another laptop in your network. (Also, you may demonstrate this by downloading the shared file on another computer in the same network). Required Each individual/group will...
Overloading the insertion (<<) and extraction (>>) operators for class use requires creating operator functions that...
Overloading the insertion (<<) and extraction (>>) operators for class use requires creating operator functions that use these symbols but have a parameter list that includes a class ____. object address reference data type Flag this Question Question 210 pts When overloading the insertion operator to process a Complex object, it’s important to understand that you’re overloading an operator in the ____ class. istream operator Complex ostream Flag this Question Question 310 pts ____ class variables are allocated memory locations...
Upon successful completion of the MBA program, imagine you work in the analytics department for a...
Upon successful completion of the MBA program, imagine you work in the analytics department for a consulting company. Your assignment is to analyze the following databases: Hospital Provide a detailed, four part, statistical report with the following sections: Part 1 - Preliminary Analysis Part 2 - Examination of Descriptive Statistics Part 3 - Examination of Inferential Statistics Part 4 - Conclusion/Recommendations Part 1 - Preliminary Analysis Generally, as a statistics consultant, you will be given a problem and data. At...
Scenario: Upon successful completion of the MBA program, imagine you work in the analytics department for...
Scenario: Upon successful completion of the MBA program, imagine you work in the analytics department for a consulting company. Your assignment is to analyze one of the following databases: Manufacturing Hospital Consumer Food Financial Select one of the databases based on the information in the Signature Assignment Options. Provide a 1,600-word detailed, four part, statistical report with the following sections: Part 1 - Preliminary Analysis Part 2 - Examination of Descriptive Statistics Part 3 - Examination of Inferential Statistics Part...
Upon the successful completion of this module, you should understand the following concepts: IN YOUR OWN...
Upon the successful completion of this module, you should understand the following concepts: IN YOUR OWN WORDS AND SHOULD BE AT LEAST 100 WORDS 1- Networking 2- Negotiations process 3- Ethics and influencing
Upon the successful completion of this module, you should understand the following concepts: IN YOUR OWN...
Upon the successful completion of this module, you should understand the following concepts: IN YOUR OWN WORDS AND SHOULD BE AT LEAST 100 WORDS 1- Different sources and appropriate uses of power 2- Political behaviour 3- Developing political skills 4- Networking 5- Negotiations process 6- Ethics and influencing
Upon the successful completion of this module, you should understand the following concepts: YOUR OWN WORDS:...
Upon the successful completion of this module, you should understand the following concepts: YOUR OWN WORDS: 1- Message receiving process 2- Importance of feedback 3- Providing coaching feedback 4- Performance formula 5- Mentoring 6- Conflict management styles 7- Conflict resolution model
Upon completion of this exercise, you will have demonstrated the ability to: Use C# loops Construct...
Upon completion of this exercise, you will have demonstrated the ability to: Use C# loops Construct conditions INTRODUCTION Your instructor will assign one of the following projects, each involves the user of loops and the construction of conditions to control them. CODING STANDARDS The following coding standards must be followed when developing your program: An opening documentation at the beginning of the source file describing the purpose, input, process, output, author, last modified date of the program. Write only one...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT