Tuesday, January 14, 2014

Short Update and Task Idea

1/14/14
Not much coding today. Still have much hatred in my heart for the quadratic formula. I mean really, if someone gave me this piece of code:

public class Quadratic {

    public static void main(String[] args) {
        double b = Double.parseDouble(args[0]);
        double c = Double.parseDouble(args[1]);

        double discriminant = b*b - 4.0*c;
        double sqroot =  Math.sqrt(discriminant);

        double root1 = (-b + sqroot) / 2.0;
        double root2 = (-b - sqroot) / 2.0;

        System.out.println(root1);
        System.out.println(root2);
    }
}

Then, told me to “an appropriate error message if the discriminant is negative, and behaves appropriately (avoiding division by zero) if a is zero.” I would do this:

public class Quadratic {

    public static void main(String[] args) {
        double b = Double.parseDouble(args[0]);
        double c = Double.parseDouble(args[1]);

        double discriminant = b*b - 4.0*c;
       
        if (discriminant > 1) System.out.println("Discriminant is a negative number");
        else
        {
          double sqroot =  Math.sqrt(discriminant);

          double root1 = (-b + sqroot) / 2.0;
          double root2 = (-b - sqroot) / 2.0;

          System.out.println(root1);
          System.out.println(root2);
        }
    }
}

Then, tell them there is no “a” and that the only division in this code is by 2.0, which means there is no cases possible of division by zero. Is that so wrong?

Other programming task ideas: programming a widget that tracks how many hours I spend programming.

No comments: