Lab 5: Creating, Testing and Debugging Functions Introduction: As we have already seen, functions can be used in Java to package code so it can be easily reused in our program or in different programs. This is a three step process: (1) we must decide what code to put into the function, (2) we must define the parameters and return type for this function, and (3) we must test and debug the function before using it in our program or distributing it to other programmers. In this lab we will be going over some techniques for creating, testing and debugging Java functions. Instructions: Consider the following Java program. The main program prompts the user to enter three coefficients for a quadratic equation of the form y = ax2 + bx + c. The program uses the quadratic formula to solve for the two roots of the equation (the x values where y = 0). import java.util.Scanner; public class Main { public static void main(String[] args) { // Get user input Scanner scnr = new Scanner(System.in); System.out.print("Enter x^2 coefficient: "); double a = scnr.nextDouble(); System.out.print("Enter x coefficient: "); double b = scnr.nextDouble(); System.out.print("Enter constant coefficient: "); double c = scnr.nextDouble(); // Find roots of quadratic equation System.out.println("Quadratic equation: "); System.out.println("y = "+a+"x^2"+" + "+b+"x"+" + "+ c); double discriminant = b * b - 4 * a * c; double root1 = (- b - Math.sqrt(discriminant)) / (2 * a); double root2 = (- b + Math.sqrt(discriminant)) / (2 * a); System.out.println("Root1 = " + root1); System.out.println("Root2 = " + root2); } } Step 1: Copy this program into your Java program editor, and compile it. Run the program and enter coefficient values 1 0 and -4 corresponding to y = x2 - 4. What does the program print? Does the answer look correct? You may recall that y = x2 - 4 can be factored as y = (x-2)(x+2) so the two roots of the equation should be -2 and 2. Step 2: Now let’s consider the factored quadratic y = (x-1)(x+3). When we expand this formula we get y = x2 + 3x - x - 3 = x2 + 2x - 3, so let’s try entering coefficients 1 2 -3 to see what we get. Hopefully we should see two roots -3 and 1. Do this again with another factored quadratic. Do you get the expected values? Step 3: If you look at the code that calculates the roots, you will see two calculations that should set off “alarm bells”. First of all, the sqrt() function is defined to work only for positive values. Hence if the discriminant is less than zero something “bad” should happen. Look at the formula to see if you can guess when the discriminant be less than zero? Run the program with 1 0 4 to see what the program prints. In Java “NaN” stands for “not a number”. This is what sqrt() returns for negative inputs. Step 4: Edit your program to add an if statement “if (discriminant >= 0)” around the root calculations. Now run the program with 1 0 4 to see what happens. Hopefully you will not see “NaN” again. Step 5: The second “dangerous calculation” occurs when we divide by (2 * a) when calculating the roots. When a equals zero our program will attempt to divide by zero. In some languages, this will cause the program to crash, but in Java it will return the special value “Infinity”. Try running your program with coefficients 0 1 2 to see what happens. You will see that sometimes Java will print “Infinity” and other times it will print “NaN” when you try to use “Infinity” in a formula. Step 6: Edit your program to add an if statement “if ((discriminant >= 0) && (a != 0))” around the root calculations. Now run the program with 0 1 2 to see what happens. Hopefully you will not see “NaN” again. In fact, you will not see any output. This is known as “failing silently”. It is much better to give the user an error message in this case. Edit the code again, and add an else statement that prints the message “Sorry no roots can be calculated”. Step 7: Create a function called “solve_quadratic” above the main function, and copy all of the code after the // Find roots comment into this function. When you compile this code, you will see error messages about the symbols a,b,c not being defined. To solve this, add three double parameters a,b,c to your function. Now when you compile, the error messages should go away. Step 8: Edit the main function, and add a call to your “solve_quadratic” function, and pass in the current values of a,b,c as parameters. When you run the program it should work as before. Step 9: To really stress test your function we should test it with lots of different input values. This can get very boring if we have to type in the inputs over and over again. This is where loops come in handy. Comment out the user input code and add the following loop inside the main program: for (double a = -2; a <= 2; a=a+1) for (double b = -2; b <= 2; b=b+1) for (double c = -2; c <= 2; c=c+1) solve_quadratic(a,b,c); Step 10: Now when you run the program, you should see several pages of output. The loops above should execute 5 times each, so the program will now test 5x5x5=125 quadratic equations. If you want to do even more testing, you can change the start and end values of the loops or the increment values. Step 11: Once you think your program is working correctly, upload your final program and a copy of your final program output into Blackboard for grading.