|
Aims and Objectives: A more challenging problem (than those set previously) to allow students a further opportunity to make use of all the programming knowledge acquired so far including the provision of a "menu" mechanism to control the behaviour of the required software system.
Design and develop a Java program that continuously computes and displays value(s) for x, given quadratic equations (i.e. a second-order polynomials) of the form:
where the values for the coefficients a, b and c are supplied by the user, and are assumed to be integers within the range of -100 to 100. To control the loop use a menu interface. The menu should include two options Calculate quadratic and End. Note that to solve a quadratic equation we must calculate the roots. This can be done using the quadratic formula:
root 1 = (-b + sqrt(b2-4ac)) / 2a
root2 = (-b - sqrt(b2-4ac)) / 2a
Example:
x2 + 2x - 8 = 0 |
a= 1, b = 2, c = -8 |
roots = (-2 +or- sqrt(22-4x1x-8)) / 2x1 |
= (-2 +or- sqrt(4+32)) / 2 |
root1 = (-2 + 6)/2 = 4/2 = 2.0 |
root2 = (-2 - 6)/2 = -8/2 = -4.0 |
x = 2.0 or -4.0 |
However, there are certain special consideration to be taken into account:
-8 = 0? a= 0, b = 0, c = -8 (degenerate case)
2x - 8 = 0 a= 0, b = 2, c = -8 (Linear equation) root = -c/b = 8/2 = 4.0 x = 4.0
x2 + 2x + 8 = 0 a= 1, b = 2, c = 8 roots = (-2 +or- sqrt(22-4x1x8)) / 2x1 = (-2 +or- sqrt(4-32)) / 2 = (-2 +or- sqrt(-28)) Negative discriminant therefore no solution.
x2 + 4x + 4 = 0 a= 1, b = 4, c = 4 roots = (-4 +or- sqrt(42-4x1x4)) / 2x1 = (-4 +or- sqrt(16-16)) / 2 (Discriminant = 0, there fore only one solution) root = -4/2 = -2 x = -2.0
Output, where appropriate, should be accurate to at least several decimal places.
Note: For information concerning menu interfaces refer back to the lecture on menus. For details on how to control the input loop in connection with a menu interface refer back to the lecture where we discussed while loops with exits. Alternatively you could consider using a "do-while" loop construct and avoid the use of a break statement.
Your solution should comprise the following:
Your report should comprise the following sections:
All supporting documentation should be prepared as a single Microsoft Word file.
Once completed you should "up-load" your Java source files (extension .java) and your word document to the CS department's electronic "practical assignment submission" system.
Marks will be awarded for:
With the total number of available marks distributed as indicated.
Remember:
Created and maintained by Frans Coenen. Last updated 30 October 2006