Lab Assignment 6

Fall Semester, 2006

 

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 , where , is a quadratic equation in standard form. An example of such an equation is . This equation has 2 solutions, x = 2 and x = 3. The solutions of a quadratic equation in standard form may be found by using the well-known formula:

 

         

 

Of course, this formula is shorthand notation for 2 equations,

 

          and

 

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 . An example of such an equation is . Substituting a=5, b=4 and c=1 into the quadratic formula gives   

 

.

 

This results in the 2 solutions   and , or in decimal form, -0.4 + 0.2i and -0.4 – 0.2i. Using the Complex class discussed in lecture, these would display as (-0.4, 0.2) and (-0.4, -0.2).

 

3

Implement a QuadraticEquation class that uses the following template:

 

public class QuadraticEquation
{
  private double a, b, c;  /* quadratic coefficients */
  private QuadraticSolution solution;  /* solution(s) to the
                                          equation */

 

  public QuadraticEquation(double a, double b, double c)
     throws QuadraticException
  {

    /* If a == 0, throw an exception with the message
       "Equation not quadratic, a = 0" */
       Otherwise, initialize the coefficients and find the

       solutions to the equation using the method solve() */

  }

 

  public QuadraticSolution getSolution()
  {

    /* Return the solution to the equation */
  }

 

  public String toString()
  {
    /* Return a string displaying the equation in the form
       ax^2 + bx + c = 0.  Here are some examples for some
       given values of a, b, and c.

       a=1, b=3, c=6  =>    1.0x^2 + 3.0x + 6.0 = 0
       a=-2, b=0, c=5 =>    -2.0x^2 + 5.0 = 0
       a=4, b=-1, c=0 =>    4.0x^2 - 1.0x  = 0        */

  }

 

  private void solve()
  {

    /* Solve the equation using the quadratic formula.
       This method should set the value of the instance
       variable solution.   */
  }

}

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)
  {
     QuadraticEquation qe;

     try
     {
       qe = new QuadraticEquation(0, 2, 4);
       System.out.println("QuadraticEquation was created");
     }
     catch (QuadraticException e)
     {

       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)
     throws QuadraticException

  {

    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
       if b is positive then

          s += " + " + b + "x "

       else

          s += " – " + Math.abs(b) + "x "

·       Do something similar when testing the value of c,  
and finish the result string off by concatenating the string " = 0".

 

Test the implementation of toString with the following main method:

 

  public static void main(String[] args)
    throws QuadraticException

  {

    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.