Question

In: Computer Science

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

  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

#ifndef MONOMIAL_H
#define MONOMIAL_H


class Monomial
{
private:
float coefficient;
int power;

public:
Monomial(float coefficient=1.0, int power=1);
float GetCoefficient() const;
int GetPower() const;
void SetPower(int power);
void Add(const Monomial& obj);
void Multiply(float coeff);
void Multiply(const Monomial& obj);
void Exponent(int power);
void Print() const;
};

#endif

// end of Monomial.h

// Monomial.cpp

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

using namespace std;

// constructor to initialize coefficient and power
Monomial::Monomial(float coefficient, int power) : coefficient(coefficient), power(power)
{
if(this->power < 1) // power is not greater than 0, update power to 1
this->power = 1;
}

// function to return coefficient
float Monomial:: GetCoefficient() const
{
return coefficient;
}

// function to return the power
int Monomial:: GetPower() const
{
return power;
}

// function to update power
void Monomial:: SetPower(int power)
{
if(power >= 1) // input power >= 1, update power
this->power = power;
else // else set power to 1
this->power = 1;
}

// function to add monomials
void Monomial:: Add(const Monomial& obj)
{
if(power == obj.power) // if powers are equal, add coefficient of obj to this coefficient
coefficient += obj.coefficient;
else // display error message
cout<<"Cannot add monomials with different powers"<<endl;
}

// function to multiply Monomial with given coeff
void Monomial:: Multiply(float coeff)
{
coefficient *= coeff; // multiply coefficient with coeff
}

// function to multiply Monomial with given obj
void Monomial:: Multiply(const Monomial& obj)
{
coefficient *= obj.coefficient; // multiply coefficients
power += obj.power; // add the powers
}

// function to raise Monomial to power
void Monomial:: Exponent(int power)
{
if(power >= 1) // input power is positive
{
float coeff = 1;
// update the coefficient
for(int i=0;i<power;i++)
coeff *= coefficient;

coefficient = coeff;
// multiply powers
this->power *= power;
}else
cout<<"Can raise only to a positive power"<<endl;
}

// function to display Monomial
void Monomial:: Print() const
{
cout<<coefficient<<"x^"<<power<<endl;
}


//end of Monomial.cpp

//
// 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^3 = ";
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^3 * -3x^6 = ";
m3.Print();

// can raise monomial to the power
cout << "Monomial exponentiation" << endl;
m4.Exponent(3);
cout << "(-3x^6)^3 = "; // -27x^18
m4.Print();
cout << "(3x)^0 = "; // -27x^18
m1.Exponent(-10);
return 0;
}

// end of driver.cpp

Output:


Related Solutions

Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions: void setlength(float) to set the length data member void setwidth(float) to set the width data member float perimeter() to calculate and return the perimeter of the rectangle float area() to calculate and return the area of the rectangle void show() to display the length and width of the rectangle...
FOR C++ A friend wants you to start writing a video game. Write a class called...
FOR C++ A friend wants you to start writing a video game. Write a class called “Player” that includes the location of the player, her/his current score, and the ancestry of the player (e.g. “Elf”, “Goblin”, “Giant”, “Human”). A player will always start at location (0, 0) and have a score of 0. Write accessors/modifiers (or “setters/getters”) for each of those characteristics. Write an “appropriate” constructor for the class (based on what’s described above). Write methods moveNorth(), moveSouth(), moveEast() and...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
C# please! A friend wants you to start writing a video game. Write a class called...
C# please! A friend wants you to start writing a video game. Write a class called “Player” that includes the location of the player, her/his current score, and the ancestry of the player (e.g. “Elf”, “Goblin”, “Giant”, “Human”). A player will always start at location (0, 0) and have a score of 0. Always. Write accessors/modifiers (or “setters/getters”) for each of those characteristics. Write an “appropriate” constructor for the class (based on what’s described above). Write methods moveNorth(), moveSouth(), moveEast()...
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...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in last lab). A MedicalStaff has specialty (i.e. Orthopedic, cardiology, etc.). 2- Then write two classes: Doctor class has office visit fee. Nurse class has title (i.e. RN, NP, etc.) Both classes inherit from MedicalStaff. Be sure these are all complete classes, including toString method. 3- Write a tester to test these classes and their methods, by creating an array or ArrayList of Person and...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an int field called strength and a char field called type. You may make them either private or protected. The Tool class should also contain the function void setStrength(int), which sets the strength for the Tool. Create 3 more classes called Rock, Paper, and Scissors, which inherit from Tool. Each of these classes will need a default constructor that sets the strength to 1 and...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
Using Python 3, Write a class called GolfScore that keeps track of the score for a...
Using Python 3, Write a class called GolfScore that keeps track of the score for a person playing a round of golf. We will limit the round to 9 holes, just to make testing easier. As a reminder, the holes on the golf course are numbered 1 – 9. Create a class level variable named NUM_OF_HOLES set to 9. Methods to be written: The constructor – sets the score for all holes to -1 addScore (hole, score) – this method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT