Homework 04
Version 1

 

Due by noon on Saturday, September 25, 1999 / 8am Monday, September 27, 1999

In this assignment you will build a more full-featured version of the quadratic equation solver designed in the lecture notes.

Your submission will be in two parts. The program will be submitted in the usual way (and should be named Quadratic). You must also submit a top-down-design of the program of the sort we created in the lecture. This part should be submitted on paper. You should submit it by dropping it in the box outside Professor Fleck's door by noon on Saturday the 25th. (Note this is two days before the program is due.)

Since you have to do the top-down-design anyway, you might as well do it first!

To see how your program should behave, run our on-line simulator. Make your program mimic its behavior as closely as possible. If you want to try the simulator more than once (to test different behaviors) just use the "reload" option on the page that pops up for the program.

The Problem:

Prompt the user to enter the coefficients of the quadratic equation. Print the roots of the equation, taking into account the following issues:

  1. If the first coefficient is 0, the equation is not quadratic, but still has an easy solution. Inform the user that the equation is linear, and tell them the solution. (Be careful here. What happens if the second coefficient is also 0?)

  2. If the discriminant is 0, there is just one root (called a double root). Tell the user there is only a single double root and print its value.

  3. If the discriminant is negative, the roots of the equation are complex. Tell the user this and do not attempt to solve the equation. Make sure you don't try to take the square root of a negative number (even though Java will handle it gracefully, like division by zero, I want you to figure out how to avoid it entirely).

Here is some sample output from the sample solution:

[1:01pm] hodas@orion (~): java Quadratic

Enter the three coefficients a, b, c, of the equation ax^2 + bx + c = 0 0 2 3 That's a linear equation. The 0-crossing is at x = -1.5

[1:01pm] hodas@orion (~): java Quadratic

Enter the three coefficients a, b, c, of the equation ax^2 + bx + c = 0 0 0 3 Sorry, the equation 3.0 = 0 doesn't make sense!

[1:01pm] hodas@orion (~): java Quadratic

Enter the three coefficients a, b, c, of the equation ax^2 + bx + c = 0 0 0 0 That's exciting, you said 0 = 0!

[1:01pm] hodas@orion (~): java Quadratic

Enter the three coefficients a, b, c, of the equation ax^2 + bx + c = 0 2 3 4 I'm sorry, I can't handle equations with complex roots.

[1:01pm] hodas@orion (~): java Quadratic

Enter the three coefficients a, b, c, of the equation ax^2 + bx + c = 0 1 4 2 The roots of the equation are -0.5857864376269049 and -3.414213562373095

[1:01pm] hodas@orion (~): java Quadratic

Enter the three coefficients a, b, c, of the equation ax^2 + bx + c = 0 2 4 2 There is a double root at x = -1.0

[1:01pm] hodas@orion (~): java Quadratic

Enter the three coefficients a, b, c, of the equation ax^2 + bx + c = 0 1.2 4.3 2.5 The roots of the equation are -0.7301883997619171 and -2.853144933571416

Last modified Sept 16 for Fall 99 cs5 by fleck@cs.hmc.edu


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Tue, Sep 8, 1998 at 10:13:10 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_04/homework.html