Question

In: Computer Science

Overview For this assignment, design and implement the methods for a class that can be used...

Overview

For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation.

int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp

All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file.

The Quadratic class

Data Members

The class contains three data members:

  • an integer that holds the value of the a-coefficient of the equation

  • an integer that holds the value of the b-coefficient of the equation

  • an integer that holds the value of the c-coefficient of the equation

Constructor

The constructor for the class is used to create a Quadratic object. It takes 3 arguments: an integer that holds the initial value for the a-coefficient, an integer that holds the initial value for the b-coefficient, and an integer that holds the initial value for the c-coefficient.

If the initial value for the a-coefficient is 0, initialize the a-coefficient data member to a value of 1. If the initial value for the a-coefficient is non-zero, use it to initialize the value of the a-coefficient data member. Use the initial values for the b and c-coefficients to initialize the respective data members.

Methods

The following methods are required for the Quadratic class.

void printEquation()

This method displays the quadratic equation in its standard form. It takes no arguments and returns nothing.

The equation should be displayed in the following format:

a-coefficient x^2 + b-coefficient x + c-coefficient = 0

If an object has an a-coefficient value of 1, b-coefficient value of 4, and c-coefficient value of -5, the equation will display as:

1x^2 + 4x + -5 = 0

double calcX()

This method calculates and returns the x-coordinate of the vertex of the quadratic equation. It takes no arguments and returns a double: the calculated x-coordinate.

The x-coordinate of the vertex is calculated as follows:

x-coordinate = - b-coefficient / ( 2 * a-coefficient)

double calcY()

This method calculates and returns the y-coordinate of the vertex of the quadratic equation. It takes no arguments and returns a double: the calculated y-coordinate.

The y-coordinate of the vertex is calculated as follows:

y-coordinate = a-coefficient * x-coordinate2 + b-coefficient * x-coordinate + c-coefficient

Call the calcX() method to get the value of the x-coordinate and then use it to calculate the value of the y-coordinate.

void printVertex()

This method displays the x and y-coordinates of the vertex of the quadratic equation. It takes no arguments and returns nothing.

This method should call the calcX() and calcY() methods to get the x and y-coordinates of the vertex, respectively. Those values should then be displayed in the following manner:

Vertex Coordinates: (x-coordinate, y-coordinate)

The coordinates should have exactly 4 digits after the decimal point. For example:

Vertex Coordinates: (-2.0000, -9.0000)

void printConcavity()

This method displays the direction that a parabola produced by the quadratic equation would open if it was graphed. It takes no arguments and returns nothing.

The a-coefficient is used to determine if the parabola opens upward or downward. If the a-coefficient is positive, then the parabola opens upward and:

The parabola opens UPWARD

should be displayed. If the a-coefficient negative, then the parabola opens downward and:

The parabola opens DOWNWARD

should be displayed.

void print()

This method displays everything related to the parabola. It takes no arguments and returns nothing.

If an object has an a-coefficient value of 1, b-coefficient value of 4, and c-coefficient value of -5, this will display:

1x^2 + 4x + -5 = 0
Vertex Coordinates: (-2.000, -9.000)
The parabola opens UPWARD

call the printEquation(), printVertex(), and printConcavity() methods to display the various values.

int main()

As specified earlier, int main() has already been coded for this assignment. It is available on Blackboard and at this link.

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp

The code in int main() is written to test the various methods that are required for the assignment. As the methods are being developed, the code may be commented out so that only one method is executing at a time. However, the program that is submitted MUST have the original int main().

Programming Requirements

  1. Each method MUST have a documentation box like a function.

  2. Hand in a copy of the source code using Blackboard.

Output

-------------------
-   Quadratic 1   -
-------------------
1x^2 + 4x + -5 = 0

The x-coordinate of the vertex is -2.0000
The y-coordinate of the vertex is -9.0000

Vertex Coordinates: (-2.0000, -9.0000)
The parabola opens UPWARD


-------------------
-   Quadratic 2   -
-------------------
-1x^2 + 2x + -1 = 0
Vertex Coordinates: (1.0000, 0.0000)
The parabola opens DOWNWARD


-------------------
-   Quadratic 3   -
-------------------
1x^2 + 0x + 25 = 0
Vertex Coordinates: (0.0000, 25.0000)
The parabola opens UPWARD



-------------------
-   Quadratic 4   -
-------------------
-12x^2 + 2x + 3 = 0
Vertex Coordinates: (0.0833, 3.0833)
The parabola opens DOWNWARD



-------------------
-   Quadratic 5   -
-------------------
12x^2 + 2x + 3 = 0
Vertex Coordinates: (-0.0833, 2.9167)
The parabola opens UPWARD


-------------------
-   Quadratic 6   -
-------------------
1x^2 + -3x + 0 = 0
Vertex Coordinates: (1.5000, -2.2500)
The parabola opens UPWARD

Extra Credit 1

For up to 5 points of extra credit, modify the code in the printEquation method so that it handles special cases for the three coefficients.

For the a-coefficient, the special cases are when the coefficient value is 1 or -1. If the a-coefficient is 1, no coefficient should be displayed (i.e. only x^2 should display). If the a-coefficient is -1, no coefficient should be displayed BUT the negative sign should display (i.e. only -x^2 should display).

For the b-coefficient, the special cases are when the coefficient value is 0 or 1. If the b-coefficient is 0, no coefficient or x should be displayed. If the b-coefficient is 1, no coefficient should be displayed (i.e. only x should display).

For the c-coefficient, the special case is when the coefficient value is 0. If the c-coefficient is 0, no coefficient should be displayed.

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don't take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.

Extra Credit 1 Output

-------------------
-   Quadratic 1   -
-------------------
x^2 + 4x + -5 = 0

The x-coordinate of the vertex is -2.0000
The y-coordinate of the vertex is -9.0000

Vertex Coordinates: (-2.0000, -9.0000)
The parabola opens UPWARD


-------------------
-   Quadratic 2   -
-------------------
-x^2 + 2x + -1 = 0
Vertex Coordinates: (1.0000, 0.0000)
The parabola opens DOWNWARD


-------------------
-   Quadratic 3   -
-------------------
x^2 + 25 = 0
Vertex Coordinates: (0.0000, 25.0000)
The parabola opens UPWARD



-------------------
-   Quadratic 4   -
-------------------
-12x^2 + 2x + 3 = 0
Vertex Coordinates: (0.0833, 3.0833)
The parabola opens DOWNWARD



-------------------
-   Quadratic 5   -
-------------------
12x^2 + 2x + 3 = 0
Vertex Coordinates: (-0.0833, 2.9167)
The parabola opens UPWARD


-------------------
-   Quadratic 6   -
-------------------
x^2 + -3x = 0
Vertex Coordinates: (1.5000, -2.2500)
The parabola opens UPWARD

Extra Credit 2

For up to 5 points of extra credit, add code that will calculate and display the roots of the quadratic equation.

This extra credit portion REQUIRES the addition of 3 extra methods to the Quadratic class and some calling statements at the end of int main().

double calcDiscrim()

This method calculates and returns the value of the discriminant. It takes no arguments and returns a double: the calculated discriminant.

The discrimant is calculated as follows:

discriminant = b-coefficient2 - 4 * a-coefficient * c-coefficient

int calcRoots( double &root1, double &root2 )

This method calculates and passes back the roots for the equation, if they exist, and returns the number of roots. It takes two arguments: a reference to a double to pass back the first root and a reference to a double to pass back the second root. It returns an integer: the number of roots.

The roots are the places where, if the equation is graphed, the resulting parabola intersects the x-axis. If the parabola is entirely above or below the x-axis, there are no real roots. If it just touches the x-axis, it has one root with a multiplicity of 2. Otherwise, it intersects the x-axis at 2 points.

To determine which case holds true, calculate the discriminant by calling the calcDiscrim() method and check its value. If the discriminat:

  • is positive, there are two real roots and those values should be calculated using both of the formulas from below and passed back via the two reference arguments. The method should also return the value 2 to reflect that there are two roots.

  • is 0, there is one real root with multiplicity 2. Use either one of the root formulas from below to calculate the root and then pass the value back via the first root argument. Return the value 1 to indicate there is only one root.

  • is negative, there are no real roots and the only thing the method should do is return 0.

Use the following equations to calculate the roots:

root 1 = ( -b-coefficient + sqrt( discriminant )) / ( 2 * a-coefficient )

root 2 = ( -b-coefficient - sqrt( discriminant )) / ( 2 * a-coefficient )

NOTE: make sure to add #include<cmath> to the top of the code so that the sqrt() function can be used in the code.

void printRoots()

This method displays the roots of the equation (if they exist). It takes no arguments and returns nothing.

This method should call the calcRoots() method to get the number of roots that the equation has and the values of those roots (if they exist). Depending upon how many roots exist, the method should display:

There are NO roots

or

There is one root with X-Coordinate root_1_value

or

There are two roots with X-Coordinates root_1_value and root_2_value

where root_1_value and root_2_value are replaced by the calculated values (i.e. the value(s) that are passed back by the calcRoots() method). The coordinates should have exactly 3 digits after the decimal point.

int main()

At the end of the int main() that is provided above, add code that will display the quadratic equation and the roots for all 6 Quadratic objects.

Extra Credit 2 Output

This is the extra output that will display AFTER the output that is shown above.

Extra Credit Output
x^2 + 4x + -5 = 0
There are TWO roots at 1.000 and -5.000

-x^2 + 2x + -1 = 0
There is ONE root at 1.000

x^2 + 25 = 0
There are NO roots

-12x^2 + 2x + 3 = 0
There are TWO roots at -0.424 and 0.590

12x^2 + 2x + 3 = 0
There are NO roots

x^2 + -3x = 0
There are TWO roots at 3.000 and 0.000

Solutions

Expert Solution

Working code implemented in C++ and appropriate comments provided for better understanding.

main.cpp:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>

using namespace std;

// Classes
class Parabola
{
   private:
   //Data_Types
   double a_coefficient, b_coefficient, c_coefficient;

   public:
   //Methods
   double calcDiscrim();
   int calcRoots( double&, double& );
   double calcX();
   double calcY();
   void printEquation();
   void printVertex();
   void printRoots();
   void printConcavity();
   void print();

   //Constructor
   Parabola( double a_coefficient, double b_coefficient, double c_coefficient );
};

int main()
{
   // Objects & Calculations (Methods Described Below)
   // Parabola 1
   cout << "*** The First Parabola ***" << endl;
   Parabola Parabola_One( 1, 4, -5 ); // (Values Are In The Order: a_coefficient, b_coefficient, c_coefficient)
   Parabola_One.print();
  
   // Parabola 2
   cout << endl << "*** The Second Parabola ***" << endl;
   Parabola Parabola_Two( 0, 0, 25 ); // (Values Are In The Order: a_coefficient, b_coefficient, c_coefficient)
   Parabola_Two.print();

   // Parabola 3
   cout << endl << "*** The Third Parabola ***" << endl;
   Parabola Parabola_Three( -1, 2, -1 ); // (Values Are In The Order: a_coefficient, b_coefficient, c_coefficient)
   Parabola_Three.printEquation();
   Parabola_Three.printConcavity();

   // Parabola 4
   cout << endl << "*** The Fourth Parabola ***" << endl;
   Parabola Parabola_Four( -12, -2, 3 ); // (Values Are In The Order: a_coefficient, b_coefficient, c_coefficient)
   Parabola_Four.printRoots();

   // Parabola 5
   cout << endl << "*** The Fifth Parabola ***" << endl;
   Parabola Parabola_Five( 12, 2, 3 ); // (Values Are In The Order: a_coefficient, b_coefficient, c_coefficient)
   Parabola_Five.printEquation();
   Parabola_Five.printVertex();

   return 0;
}

//***************************************************************************************************************
// Constructor
Parabola::Parabola(double inta_coefficient, double intb_coefficient, double intc_coefficient)
{
   // Error Checking (a_coefficient cannot be 0)
   if ( inta_coefficient != 0 )
   {
       a_coefficient = inta_coefficient;
   }
   else
   {
       a_coefficient = 1;
   }

   // Remaining Varialbes
   b_coefficient = intb_coefficient;
   c_coefficient = intc_coefficient;
}

//***************************************************************************************************************
double Parabola::calcDiscrim()
/*
Name Of Method: calcDiscrim
Number/Names of Arguments: None
Number/Names of Returns: 1 ( discriminant)
Basic Function: This method calculates and returns the value of the discriminant.
*/
{
   // Delcared Variables
   int discriminant;

   // Calculates discriminant
   discriminant = (b_coefficient * b_coefficient) - 4 * a_coefficient * c_coefficient;

   return discriminant;
}

//***************************************************************************************************************
int Parabola::calcRoots( double &Root1, double &Root2 )
/*
Name Of Method: calcRoots
Number/Names of Arguments: 2 (root1, root2)
Number/Names of Returns: 1 ( NumOfRoots )
Basic Function: This method calculates and passes back the roots for the parabola, if they exist,
                and returns the number of roots.
*/
{
   // Local Variables
   int NumOfRoots;
   double discriminant;

   // Call calcDiscrim
   discriminant = calcDiscrim();

   //Calculates Roots
   if ( discriminant > 0 )
   {
       Root1 = ( -b_coefficient + sqrt( discriminant )) / ( 2 * a_coefficient );
       Root2 = ( -b_coefficient - sqrt( discriminant )) / ( 2 * a_coefficient );
       NumOfRoots = 2;
   }
   else if ( discriminant == 0)
   {
       Root1 = ( -b_coefficient + sqrt( discriminant )) / ( 2 * a_coefficient );
       NumOfRoots = 1;
   }
   else
   {
       NumOfRoots = 0;
   }

   //Returns # Of Roots
   return NumOfRoots;
}

//***************************************************************************************************************
double Parabola::calcX()
/*
Name Of Method: calcX
Number/Names of Arguments: None
Number/Names of Returns: 1 (X-Coordinate)
Basic Function: This method calculates and returns the x-coordinate of the vertex of the parabola
*/
{
   //Local Variables
   double X_Coordinate;

   X_Coordinate = -b_coefficient / ( 2 * a_coefficient);

   return X_Coordinate;
}

//***************************************************************************************************************
double Parabola::calcY()
/*
Name Of Method: calcY
Number/Names of Arguments: None
Number/Names of Returns: 1 (Y-Coordinate)
Basic Function: This method calculates and returns the y-coordinate of the vertex of the parabola.
*/
{
   // Local Variables
   double Y_Coordinate, X_Coordinate;

   // Call calcX
   X_Coordinate = calcX();

   // Calculate Y_Coordinate
   Y_Coordinate = ( a_coefficient * (X_Coordinate * X_Coordinate) ) + ( b_coefficient * X_Coordinate ) + c_coefficient;

   // Returns Y_Coordinate
   return Y_Coordinate;
}

//***************************************************************************************************************
void Parabola::printEquation()
/*
Name Of Method: printEquation
Number/Names of Arguments: None
Number/Names of Returns: None
Basic Function: This method displays the parabola in the form of a quadratic equation.
*/
{
   cout << setiosflags( ios:: fixed ) << setprecision(1);
   cout << a_coefficient << "x^2 + " << b_coefficient << "x + " << c_coefficient << endl;
}

//***************************************************************************************************************
void Parabola::printVertex()
/*
Name Of Method: printVertex
Number/Names of Arguments: None
Number/Names of Returns: None
Basic Function: This method displays the x and y-coordinates of the vertex of the parabola.
*/
{
   //Local Variables
   double X_Coordinate, Y_Coordinate;

   // call calc x and calc y
   X_Coordinate = calcX();
   Y_Coordinate = calcY();
   cout << setiosflags( ios:: fixed) << setprecision(3);
   cout << "   Vertex Coordinates: (" << X_Coordinate << ", " << Y_Coordinate << ")" << endl;
}

//***************************************************************************************************************
void Parabola::printRoots()
/*
Name Of Method: printRoots
Number/Names of Arguments: None
Number/Names of Returns: None
Basic Function: This method displays the roots of the parabola (if they exist).
*/
{
   // calc roots method
   // replace root values with actual variables
   int NumOfRoots;
   double Root1, Root2;
  
   // Retreive # Of Roots
   NumOfRoots = calcRoots(Root1, Root2);

   // Display Roots
   cout << setiosflags( ios:: fixed ) << setprecision(3);
   if (NumOfRoots == 0)
   {
       cout << "   There Are No Real Roots." << endl;
   }
   else if (NumOfRoots == 1)
   {
       cout << "   There Is One Real Root With X-Coordinate " << Root1 << endl;
   }
   else
   {
       cout << "   There Are Two Real Roots With X-Coordinates " << Root1 << " and " << Root2 << endl;
   }
}

//***************************************************************************************************************
void Parabola::printConcavity()
/*
Name Of Method: printConcavity
Number/Names of Arguments: None
Number/Names of Returns: None
Basic Function: This method displays the direction that the parabola opens.
*/
{
   if ( a_coefficient > 0 )
   {
       cout << "   The Parabola Opens UPWARDS" << endl;
   }
   else
   {
       cout << "   The Parabola Opens DOWNWARDS" << endl;
   }
}

//***************************************************************************************************************
void Parabola::print()
/*
Name Of Method: print
Number/Names of Arguments: None
Number/Names of Returns: None
Basic Function: This method displays everything related to the parabola.
*/
{
   printEquation();
   printVertex();
   printConcavity();
   printRoots();
}

Sample Output Screenshots:


Related Solutions

Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Implement a VotingMachine class that can be used for a simple election. Include the methods to...
Implement a VotingMachine class that can be used for a simple election. Include the methods to voteForDemocrat and voteForRepublican. Add a method to clear out all votes. Additionally, add a method to print the results. Write the driver code to simulate the voting. in java
Problem 2 (C++): Design and implement a class complexType, that can be used to process complex...
Problem 2 (C++): Design and implement a class complexType, that can be used to process complex numbers. A number of the form a +ib, in which i2 = -1 and a and b are real numbers, is called a complex number. We call a the real part and b the imaginary part of a + ib. Complex numbers can also be represented as ordered pairs (a, b). The class you will design should have the following features. Constructor Your class...
Array Selector Problem Overview This assignment focuses on implementing the methods of a class much like...
Array Selector Problem Overview This assignment focuses on implementing the methods of a class much like java.util.Arrays. The Selector.java file defines a class with static methods designed to provide useful functionality on arrays, with the common theme of selecting values in an array with particular properties. Each method of Selector is clearly specified, is independent of the other methods in the class, and is designed to provide relatively simple functionality. So, this is a great context in which to practice...
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
Overview In this assignment you are required to implement binary code comparator using Xilinx that it...
Overview In this assignment you are required to implement binary code comparator using Xilinx that it is compatible with the MXK Seven Segment Displays. You will draw your digital logic circuit using Xilinx and then simulate it to verify the functionality of your design. Software Requirements ? Xilinx ISE 10.1 or higher Specifications Binary Code Comparator The binary code comparator is to be implemented and made compatible with the seven 7-segment displays of the board. Represent the first five digits...
: Design and implement class Radio to represent a radio object. The class defines the following...
: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. A private variable of type int named station to represent a station number. Set to A private variable of type int named volume to represent the volume setting. Set to 1. A private variable of type boolean named on to represent the radio on or off. Set to...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT