Saturday, January 1, 2011

Program (and method!) to evaluate Quadratic Equation.

Problem: "In high-school algebra you learned that the standard quadratic equation (ax^2 + bx + c = 0) has two solutions given by the formula: x = (-b +/- sqrt(b^2 -4ac)) / 2a.

The first solution is obtained by using + in place of +/-. The second is obtained by using - in place of +/-.

Write a Java program that accepts values for "a", "b" and "c", and then calculates the two solutions. If the quantity under the square root sign is negative, the equation has no real solutions, and your program should display a message to that effect. You may assume that "a" is nonzero." (Roberts ch 5, problem 1).

What it looks like:



This chapter is all about methods: Understanding how methods work within the context of a bigger program, writing your own, and re-writing old homework problems to use methods. I think I'll cement a lot of what I've learned about what makes code efficient/flexible.

I solved the problem by calling a method that I write. The "run" main method is there to take in the user inputs and print out the two answers. The method "quadratic" actually does the calculation.

How long it took: 1.5 hrs

What made it difficult: As a straight-up program, this wouldn't be hard; it's just a lot of arithmetic and "println" methods.

However, making "quadratic" a method made things a bit more complicated. First, there are two solutions to any quadratic forumla, but a method only can return one result. Arnab suggested fixing this by taking in "sign" as an argument to the method, and so I wrote a program that calls the method twice, once by entering -1 and once by entering 1.

Second, if the chunk under the square root evaluates to negative, there are no real number solutions, so I have to print out a string "There are no real number solutions." However, a method has to have a type (string, integer, etc), and my method type is a double, so I couldn't return a string. Arnab suggested using null-- null is becoming quite my friend these days.

Lingering questions:

1) I don't like that the part which checks for the square root chunk being negative only inspects "plus_sign_answer", even though it works. Is there a more elegant way about this?
2) I don't like that the "int sign" argument MUST take in -1 or 1; if this method was used in a program that put in, say, -100, it would return an inaccurate answer. Is there a way to stop this?



/*

/*
* File: QuadradicEquation.java
* Name: Chu
* Section Leader: Prof. Arnab
* Description: This is a program to evaluate the quadradic equation, giving inputs a, b and c.
* The 'run' method takes in the user inputs, then it calls the private method 'quadradic'.
*/


package ch6_practice;
import acm.program.*;

public class QuadradicEquation extends ConsoleProgram {
    
    public void run (){
        println("Enter the coefficients for the quadradic equation:");
        int a = readInt("a:");
        int b = readInt("b:");
        int c = readInt("c:");
        
        Double plus_sign_answer = quadradic(a, b, c, 1);
        Double minus_sign_answer = quadradic(a, b, c, -1);
        if (plus_sign_answer != null) { //This isn't totally elegant because it only checks the 
         //first answer, even though both the first and second solutions check for null...
        
            println("The first solution is " + plus_sign_answer);
            println("The second solution is " + minus_sign_answer);
        }
        else {
            println("There are no real number solutions.");
        }
    }
        
    
    private Double quadradic(int a, int b, int c, int sign) { //'sign' takes -1 or 1
       
     
        double sqrt_part = Math.sqrt(b*b - 4*a*c);
        int neg_b = b*-1;
        int two_a = 2*a;        
        double solution = (neg_b + (sign * sqrt_part))/two_a;
        
        if (sqrt_part < 0) {
            return null;
            
        }
        
        else {
            return solution;

        }
        
    }

}

1 comment:

  1. Two smart people suggested that I have the method return a two-length array; that way, the println for answer with a plus sign could call the first value in the array, and the println for answer with a minus sign could call the second value in the array.

    ReplyDelete