CS 214 Project 6: Subprograms


This week's project is to write a subprogram to compute the roots of a quadratic equation in each of our four languages. Given an equation of the form:

   y = ax^2 + bx + c
one root of the equation is given by
   root1 = (-b + sqrt(b^2 - 4ac))/2a
while the other root is given by
   root1 = (-b - sqrt(b^2 - 4ac))/2a
Of course, the coefficient a cannot be zero, and the expression b^2 - 4ac must be nonnegative.

A subprogram to compute these roots might be written in C++ as follows:

   #include <cmath>
   using namespace std;

   bool quadraticRoots(double a, double b, double c,
                        double & root1, double & root2)
   {
      if (a != 0) {
         double arg = pow(b, 2.0) - 4 * a * c;
         if (arg >= 0) {
            root1 = (-b + sqrt(arg))/(2*a);
            root2 = (-b - sqrt(arg))/(2*a);
            return true;
         } else {
            cerr << "\n*** quadraticRoots(): b^2 - 4ac is negative!" << endl;
            root1 = root2 = 0.0;
            return false;
         }
      } else {
         cerr << "\n*** QuadraticRoots(): a is zero!" << endl;
         root1 = root2 = 0.0;
         return false;
      }
   }

Your project is to implement this subprogram or its equivalent (and a main "driver" program to call it) in Java, Ada, Clojure, and Ruby. Your driver program should only display the roots if this method's return-value indicates the roots are valid.

Pesky Details:

Turn in. Using an approach like what we did in the lab, make a single script file named proj06-results, in which you list each program, show that it builds without any syntax errors or warnings, and show some executions that demonstrate its correctness. Then submit your project by copying that single file into your personal folder in /home/cs/214/current/:

   cp proj06-results /home/cs/214/current/yourUserName
replacing yourUserName with your login name. The grader will access and grade your project results from there, using the criteria from this grade sheet.


Calvin > CS > 214 > Projects > 06


This page maintained by Joel Adams.