Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.

Similar presentations


Presentation on theme: "1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from."— Presentation transcript:

1 1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from the “well- known” formula: Depending on the value of the discriminant we have 3 cases: 1. two real values for x 2. one real value for x 3. no real values for x

2 2 Flow diagram b 2 – 4ac > 0 b 2 – 4ac < 0 x 1 = – b/2a False No solutions True

3 3 Flow diagram, version 2 d > 0 d < 0 x 1 = – b/2a False No solutions True d = b 2 – 4ac True

4 4 Structure of nested if statements discriminant = b * b - 4.0 * a * c; if ( discriminant > 0 ) { // We have two real solutions } else { if ( discriminant < 0 ) { // We have no real solutions. } else { // We have one real solution. }

5 5 Adding the Complex case Suppose we now want to solve for real or complex values of x, for given values of a, b, and c. In this case, we can still use the formula but when we have we will need to be careful to avoid taking a square root of a negative number. If we take the square root of the absolute value of the discriminant, we have the complex coefficient

6 6 Flow diagram d > 0 d < 0 x 1 = – b/2a False True d = b 2 – 4ac True

7 7 Testing for Equality with Floating Point Values Because of the possibility of round-off error, testing for equality (or inequality) of float or double values is NOT recommended. Instead, test that the value is “close enough” to the desired value by using a tolerance value, and checking that the absolute value of the difference is less than the tolerance: double EPSILON = 1.0e-10; double x;... if ( x == 37.0 ) // not recommended... if ( Math.abs( x – 37.0 ) < EPSILON ) // better

8 8 The switch statement Allows for multi-way branches You can only use case statements when the test is on an integer, Boolean, or character variable. Idea: –List various “cases” for specific values of the tested variable, plus one default case for when there is no match. –Provide a statement block for each case. You MUST end each statement block with break; or the program will go to the next following case.

9 9 The switch statement case 1 case 2 case n statement block 1 true statement block 2 true statement block n true default statement block false break;

10 10 Example System.out.println(“Enter an integer from 1 to 3: “); int a = Keyboard.readInt(); switch( a ) { case 1: { System.out.println(“You entered a 1.”); break; } case 2: { System.out.println(“You entered a 2.”); break; } case 3: { System.out.println(“You entered a 3.”); break; } default: { System.out.println(“Incorrect input.”); break; } System.out.println(“After switch.”); }

11 11 Missing break statement case 1 case 2 case n statement block 1 true statement block 2 true statement block n true default statement block false break;


Download ppt "1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from."

Similar presentations


Ads by Google