CSCI240 Program 8 Classes

$30.00

Category: Tags: , , , , You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (12 votes)

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