COMP101 PRACTICAL EXERCISE 7 - QUADRATIC EQUATIONS

(Week 10 starting Monday 27 November 2006)

Hand-in date: one week after your scheduled tutorial session in week 10.

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.



1. REQUIREMENTS

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:

ax2 + bx + c = 0

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:

  1. If a and b are both zero there is no solution (this is referred to as the degenerate case):
    -8 = 0?
    a= 0, b = 0, c = -8
    (degenerate case)
    
  2. If a is zero and b is non zero the equation becomes a linear equation.

    2x - 8 = 0
    a= 0, b = 2, c = -8
    (Linear equation)
    
    root = -c/b = 8/2 = 4.0
    x = 4.0
    
  3. If the value for the term b2 - 4ac (the discriminant) is negative there is no solution (conventionally we cannot find the square root of a negative number!):
    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.
    
  4. If the discriminant is 0 then there are two identical solutions, i.e. only one solution (root) need be calculated:
    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.




2. REPORT

Your solution should comprise the following:

  1. A report in the form of a single Microsoft Word file.
  2. The Java source files of your implementation.

Your report should comprise the following sections:

  1. Requirements: (outline of the above)
  2. Analysis: Your analysis of the problem comprising: (i) a Class Diagram outlining the proposed class structure/hierarchy, and (ii) a set of summary tables describing the fields, constructors and methods for each class you intend to create.
  3. Design: Detailed designs for the methods you intend to include, described using Nassi-Shneiderman charts, and a high-level Activity Diagram describing the "paths" through your code.
  4. Implementation: A computer print out of your implementation.
  5. Testing: A set of appropriate Black box and White box test cases together with results and evidence of data validation. Given the significant amount of arithmetic testing to be done you might consider it appropriate to write a test harness. (Refer to your CFD when drawing up your path testing cases.)

All supporting documentation should be prepared as a single Microsoft Word file.




3. SUBMISSION

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.




4. MARK SCHEME

Marks will be awarded for:

  1. Analysis and design (30%)
  2. Implementation (30%)
  3. Testing (30%)
  4. Write up (10%)

With the total number of available marks distributed as indicated.

Remember:

  1. Guidance notes for the execution of COMP101 practicals.
  2. General guidance notes on COMP101 practicals and course work with respect to: the presentation of work, the COMP101 marking scheme and University's late submission policy.
  3. It is better to hand in an incomplete piece of work rather than nothing at all as this will result in some marks being awarded, while the latter option is guaranteed to result in a mark of 0!
  4. Although the exchange of ideas between students is encouraged, student collaboration should not extend to the submitting of identical, or near identical, pieces of work.



Created and maintained by Frans Coenen. Last updated 30 October 2006