|
Outcomes: Upon
completion of the lab, the student will know how to |
|
|
|
· Create a Java class that
implements a quadratic equation in standard form. |
|
|
· Use if statements to
control the flow of execution. |
|
1 |
Download the following files to your J: drive from
Mr. Clark’s website: · Complex.java · QuadraticSolution.java · QuadraticException.java |
|
2 |
In
this lab you will create a Java class that implements quadratic equations and
their solutions. In particular, you
will write · Methods to construct
quadratic equation objects · A method to solve a
quadratic equation · A method that returns the
roots of a quadratic equation · A method that displays a
quadratic equation Here
is some background information about solving quadratic equations. An
equation of the form Of
course, this formula is shorthand notation for 2 equations, For
the example equation above, we have a = 1 b = -5 c = 6 Substituting
these values into the formula, we get which results in the 2 solutions 3 and 2. A quadratic equation may have complex solutions.
This occurs when the discriminant
This results in the 2 solutions |
|
3 |
Implement
a QuadraticEquation class that uses the following template: public class
QuadraticEquation public QuadraticEquation(double a, double b, double c) /* If a == 0, throw an exception with the message solutions to the equation using the method solve() */ } public QuadraticSolution getSolution() /* Return the solution to the equation */ public String toString() } private void solve() /* Solve the equation using the quadratic formula. } You
will save your definitions in the file QuadraticEquation.java. Approach the development of this class
incrementally: ·
First,
implement and test the constructor and getSolution methods (part 4) ·
Next,
implement and test the solve() method (part 5) ·
Finally,
implement and test the toString method (part 6) |
|
4 |
Implement
the constructor and getSolution methods for the QuadraticEquation class. Test the constructor with the following
main method (place at the bottom of the QuadraticEquation definitions). : public static void main(String[] args) try System.out.println(e); } Show
the execution of this main method to a lab instructor/assistant. Then, change the assignment statement in
the try block to qe = new QuadraticEquation(1, 2, 4); and
re-execute the main method. Have the
lab instructor/assistant check your constructor code and then proceed to part
(5). |
|
5 |
Implement
the solve() method, using the quadratic formula discussed above. Keep in mind the following things: · The value of a cannot be
0, since the constructor prevents this from occurring. · If the discriminant b*b –
4*a*c < 0, the solutions are complex. If you try to calculate the square
root of this negative expression, Java will throw an exception. Handle this case by using an if statement
to check the value of the discriminant to determine the nature of the roots. Once
you have implemented solve, test your implementation with the following main
method: public static void main(String[] args) { QuadraticEquation q1 = new QuadraticEquation(1, 5, 6); System.out.println("Solution to q1: " +
q1.getSolution()); QuadraticEquation q2 = new QuadraticEquation(1, 0, -4); System.out.println("Solution to q2: " +
q2.getSolution()); QuadraticEquation q3 = new QuadraticEquation(5, 4, 1); System.out.println("Solution to q3: " +
q3.getSolution()); } The
output for this main method should look like this: Solution to q1: Roots are
real: -2.0 and -3.0 Solution to q2: Roots are
real: 2.0 and -2.0 Solution to q3: Roots are
complex: (-0.4, 0.2) and (-0.4, -0.2) Show
the execution of this main method to the lab instructor/assistant and get
his/her approval before moving on to part (6) |
|
6 |
Implement
the toString method as described in the comment in the template in part (3)
above. You will need to use some if
statements to make the output string look good. Keep in mind: · The value of a will never be zero; that is, you
are guaranteed that the x2
term will be present. Thus, you
should start off the result string, say s, with the initial value s = a +
"x^2 ". · The coefficients b and c
may be zero. This is where the if statement proves to be useful. Here is some
pseudocode: if b is
not 0 then s
+= " + " + b + "x " else s
+= " – " + Math.abs(b) + "x " · Do something similar when
testing the value of c, Test
the implementation of toString with the following main method: public static void main(String[] args) { QuadraticEquation q1 = new QuadraticEquation(-1, 5, 6); System.out.println(q1); QuadraticEquation q2 = new QuadraticEquation(1, 0, -4); System.out.println(q2); QuadraticEquation q3 = new QuadraticEquation(5, -4, 0); System.out.println(q3); } The
output for this main method should look like this (note the spaces between
the terms and adding operators): -1.0x^2 + 5.0x + 6.0 = 0 1.0x^2 - 4.0 = 0 5.0x^2 - 4.0x = 0 Show
the execution of this main method to the lab instructor/assistant. Get
his/her approval before proceeding to part (7). |
|
7 |
Print
a listing of your QuadraticEquation class and have the lab
instructor/assistant sign it. Hand in
this printed listing. |