Question

In: Computer Science

CPP Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that...

CPP Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that will allow creation and handling of univariate monomials, as described below.

Monomial Description

Each monomial object has following properties:

  • Each monomial has a coefficient and a power.
  • Monomial power is always positive.
  • Monomial power is always integer.
  • Monomials cannot be added if they have different powers

Class Details

  1. The single constructor for the Monomial class should have 2 parameters: an floating point coefficient value (optional, with a default value of 1.0); and an integer power value (optional, with a default value of 1). If the power value is less then 1, set the power to 1. The class will need to provide internal storage for any member data that must be kept track of.

  2. There should be member functions GetCoefficient, and GetPower, which will return the monomial’s coefficient value, and monomial’s power value, respectively. The GetPower method should return integer results. The GetCoefficient function should return its result as a float.

  3. There should be member functions SetPower, which will set the monomial’s power to the arbitrary positive value. If the parameter is not positive then set the power to the default value 1.

  4. There should be member function Add, which adds the one monomial to the other. The parameter of this member function need to be a constant reference to the other monomial object. The monomials can only be added if they have the same power, otherwise error message must be printed. The result of the addition is saved in the caller object.

  5. There should be two member functions Multiply (overrides), which each allow to multiply a monomial to a number and to another monomial. The first member function need to accept floating point parameter that is used to perform multiplication of the real number on the the monomial object. The second member function need to accept a constant reference to the other monomial object. Use monomials to coefficients and powers to perform their multiplication. The result of the multiplication is saved in the caller object.

  6. There should be a member function called Exponent that perform exponentiation (raise to power) of the monomial to the specified power value. You only can raise monomial to the positive power, if the power parameter is not positive print error message. The result of the exponentiation is saved in the caller object.

  7. A sample driver program (called monomial-driver.cpp) is provided. It uses objects of type Monomial and illustrates sample usage of the member functions.

  8. Your class declaration and definition files must work with my main program from the test driver, as-is (do not change my program to make your code work!). You are encouraged to write your own driver routines to further test the functionality of your class, as well. Most questions about the required behavior of the class can be determined by carefully examining my driver program and the sample execution. Keep in mind, this is just a sample. Your class must meet the requirements listed above in the specification - not just satisfy this driver program. (For instance, I haven’t tested every illegal fill character in this driver program - I’ve just shown a sample). Your class will be tested with a larger set of calls than this driver program represents.

General Requirements

  • No global variables, other than constants!
  • All member data of your class must be private
  • You can only use the <iostream> library for output. !!! No other libraries are allowed !!!.
  • When you write source code, it should be readable and well-documented.
  • Here are some general notes on style guidelines
  • Your monomial.h file should contain the class declaration only. The monomial.cpp file should contain the member function definitions.

Test Driver:

//
// driver.cpp -- driver program to demonstrate the behavior of
//               the Monomial class

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

using namespace std;

int main()
{
    // create some monomial: x, 2x, 12.5x³, -3xâ¶
    Monomial m1, m2( 2.0 ), m3( 12.5, -1 ), m4( -3.0 , 6);
    // display monomial
    cout << "Monomial `m1` has the coefficient " << m1.GetCoefficient()
         << " and power " << m1.GetPower() << ": ";
    m1.Print();
    cout << "Monomial `m2` has the coefficient " << m2.GetCoefficient()
         << " and power " << m2.GetPower() << ": ";
    m2.Print();
    cout << "Monomial `m3` has the coefficient " << m3.GetCoefficient()
         << " and power " << m3.GetPower() << ": ";
    m3.Print();
    cout << "Monomial `m4` has the coefficient " << m4.GetCoefficient()
         << " and power " << m4.GetPower() << ": ";
    m4.Print();

    // Change monomial power
    cout << "Monomial `m3` power wasn't changed: ";
    m3.SetPower(-1);
    m3.Print();
    cout << "Monomial `m3` power was changed: ";
    m3.SetPower(3);
    m3.Print();

    // can add monomials with the same powers
    cout << "Monomial addition" << endl;
    m1.Add(m2);
    cout << "x + 2x = ";
    m1.Print();
    // cannot add monomials with different powers
    cout << "x³ + 12.5x³ = ";
    m2.Add(m3);

    // can multiply monomial to a number
    cout << "Monomial multiplication by a number" << endl;
    m2.Multiply(2.5);
    cout << "2x * 2.5 = ";
    m2.Print();

    // can multiply monomials
    cout << "Monomial multiplication by a monomial" << endl;
    m3.Multiply(m4);
    cout << "12.5x³ * -3xⶠ= ";
    m3.Print();

    // can raise monomial to the power
    cout << "Monomial exponentiation" << endl;
    m4.Exponent(3);
    cout << "(-3xâ¶)³ = "; // -27x^18
    m4.Print();
    cout << "(3x)â° = "; // -27x^18
    m1.Exponent(-10);

    return 0;
}

Test Driver Output

Monomial `m1` has the coefficient 1 and power 1: 1x^1
Monomial `m2` has the coefficient 2 and power 1: 2x^1
Monomial `m3` has the coefficient 12.5 and power 1: 12.5x^1
Monomial `m4` has the coefficient -3 and power 6: -3x^6
Monomial `m3` power wasn't changed: 12.5x^1
Monomial `m3` power was changed: 12.5x^3
Monomial addition
x + 2x = 3x^1
x³ + 12.5x³ = Cannot add monomials with different powers
Monomial multiplication by a number
2x * 2.5 = 5x^1
Monomial multiplication by a monomial
12.5x³ * -3x⁶ = -37.5x^9
Monomial exponentiation
(-3x⁶)³ = -27x^18
(3x)⁰ = Can raise only to a positive power

Solutions

Expert Solution

monomial.h - Declaration file

monomial.cpp - Definition file

1.

2.

3.

The monomial.h and monomial.cpp was run via monomial-driver.cpp and passed all tests successfully.

Here are the results (given code for monomial-driver.cpp above was copied as is) :

P.S - The file monomial.cpp is broken into three parts just for the sake of uploading clear images, but they represent one single file.


Related Solutions

CPP Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that...
CPP Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that will allow creation and handling of univariate monomials, as described below. Monomial Description Each monomial object has following properties: Each monomial has a coefficient and a power. Monomial power is always positive. Monomial power is always integer. Monomials cannot be added if they have different powers Class Details The single constructor for the Monomial class should have 2 parameters: an floating point coefficient value...
C++ Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that...
C++ Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that will allow creation and handling of univariate monomials, as described below. Monomial Description Each monomial object has following properties: Each monomial has a coefficient and a power. Monomial power is always positive. Monomial power is always integer. Monomials cannot be added if they have different powers Class Details The single constructor for the Monomial class should have 2 parameters: an floating point coefficient value...
Cplusplus Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that...
Cplusplus Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that will allow creation and handling of univariate monomials, as described below. Monomial Description Each monomial object has following properties: Each monomial has a coefficient and a power. Monomial power is always positive. Monomial power is always integer. Monomials cannot be added if they have different powers Class Details The single constructor for the Monomial class should have 2 parameters: an floating point coefficient value...
Task CPP Create a class called Mixed. Objects of type Mixed will store and manage rational...
Task CPP 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 Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return...
Task You will write a class called Grid, and test it with a couple of programs....
Task You will write a class called Grid, and test it with a couple of programs. A Grid object will be made up of a grid of positions, numbered with rows and columns. Row and column numbering start at 0, at the top left corner of the grid. A grid object also has a "mover", which can move around to different locations on the grid. Obstacles (which block the mover) and other objects (that can be placed or picked up)...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT